php 在重定向到其他页面之前显示消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18305258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:19:04  来源:igfitidea点击:

Display message before redirect to other page

php

提问by user2649074

I have a simple question but yet i don't know. I would like to display message first before redirect to other page, but my code just directly redirect the page without display the message, or the display time is too short am not sure.. my code as below.. pls advise. thank much.

我有一个简单的问题,但我不知道。我想在重定向到其他页面之前先显示消息,但是我的代码只是直接重定向页面而不显示消息,或者显示时间太短我不确定..我的代码如下..请建议。非常感谢。

echo "Please Log In First";
header("location: login6.php");

回答by Sunry

You can't do it that way. PHP header must sent out before any content, so the correct order is:

你不能那样做。PHP header 必须在任何内容之前发出,所以正确的顺序是:

header("location: login6.php");
echo "Please Log In First";

But these codes will redirect instantly and wouldn't let you see the content. So I would do the redirection by JavaScript:

但是这些代码会立即重定向,不会让您看到内容。所以我会通过 JavaScript 进行重定向:

echo "Please Log In First";
echo "<script>setTimeout(\"location.href = 'http://www.example.com';\",1500);</script>";

回答by Konsole

You can use header refresh. It will wait for specified time before redirecting. You can display your message then.

您可以使用标题刷新。它将在重定向之前等待指定的时间。然后您可以显示您的消息。

header( "refresh:5; url=login.php" ); //wait for 5 seconds before redirecting

回答by federico-t

HTTP refresh redirect to wait 5 seconds:

HTTP 刷新重定向等待 5 秒:

header('Refresh:5; url=login6.php');
echo 'Please Log In First';

回答by SteAp

A redirect is a redirect ;-)

重定向是重定向 ;-)

If you tell a browser by using a Location:header to redirect to another page, the browser does just this: It redirects to the other page. And the browser does this immediately- without any delay.

如果您使用Location:标头告诉浏览器重定向到另一个页面,浏览器会这样做:它重定向到另一个页面。而浏览器做到这一点立即-没有任何延迟。

Therefore, your echowon't display at all.

因此,您echo根本不会显示。

Set headers first

首先设置标题

Furthermore, you need to set headers before each other output operation (as pointed out by Fred -ii-):

此外,您需要在每个其他输出操作之前设置标题(如Fred -ii-所指出的):

// First, echo headers
header("location: login6.php");

// Then send any other HTML-output
echo "Please Log In First";

Better not use auto-redirects

最好不要使用自动重定向

Quite likely, you won't show a message and then - after a certain amount of time - automatically redirect to another page. People might get confused by this process.Better do this:

很可能,您不会显示一条消息,然后 - 在一定时间后 - 自动重定向到另一个页面。人们可能会对这个过程感到困惑。最好这样做:

Show the login-page andpresent a user-hinter or error-message on this page.

显示登录页面在此页面上显示用户提示或错误消息。

General solution

通用解决方案

Prepare a component in the user's session, that contains information to be displayed at the next script instance. This component might be a list of messages like this:

在用户会话中准备一个组件,其中包含要在下一个脚本实例中显示的信息。这个组件可能是一个像这样的消息列表:

$_SERVER[ 'sys$flashMessagesForLater' ] = array(
  "Sorry, userID and password didn't match.",
  "Please login again."
);

Each time a script request comes in, check if $_SERVER[ 'sys$flashMessagesForLater' ]is a non-empty array.

每次脚本请求进来时,检查是否$_SERVER[ 'sys$flashMessagesForLater' ]为非空数组。

If this is the case, emit these values to a well-defined locatedon the generated HTML-page. A well-defined location would always be at the same location, somewhere at the top of each page. You might probably wish to add a read box around error messages.

如果是这种情况,请将这些值发送到位于生成的 HTML 页面上的明确定义的位置。明确定义的位置将始终位于同一位置,位于每页顶部的某个位置。您可能希望在错误消息周围添加一个阅读框。

You might want to prepare a class like this:

你可能想要准备一个这样的类:

class CFlashMessageManager {

  static public function addFlashMessageForLater( $message ) {

    $_SERVER[ 'sys$flashMessagesForLater' ][] = $message;

  }

  static public function flashMessagesForLaterAvailable() {

    return isset( $_SERVER[ 'sys$flashMessagesForLater' ] )
        && is_array( $_SERVER[ 'sys$flashMessagesForLater' ] )
        && ( 0 < count( $_SERVER[ 'sys$flashMessagesForLater' ] ))
        ;

  }

  static public function getFlashMessageForLaterAsHTML() {

    return implode( '<br />', $_SERVER[ 'sys$flashMessagesForLater' ] );

  }

} // CFlashMessageManager

回答by Arslan Tabassum

Working example in php.

php 中的工作示例。

echo "<script>";
echo " alert('Import has successfully Done.');      
        window.location.href='".site_url('home')."';
</script>";

回答by Alabate

You can do a html redirection : put this in you head

你可以做一个 html 重定向:把这个放在你的头上

<meta http-equiv="refresh" content="5; url=http://example.com/">

And the page will be redirected after 5 seconds (change the 5 to the number of seconds you want)

并且页面将在 5 秒后重定向(将 5 更改为您想要的秒数)

回答by chinna_82

try sleep function

试试睡眠功能

<?php
sleep(10);//seconds to wait..
echo "Please Log In First";
header("location: login6.php");
?>