php php重定向到带有消息的页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11988061/
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 02:34:06  来源:igfitidea点击:

php redirect to page with message

phpredirectmessage

提问by user1386906

i want to redirect to a page and then display a message:

我想重定向到一个页面,然后显示一条消息:

what i have is:

我所拥有的是:

if (mysqli_affected_rows($link) == 1) 
{
   //succes         
    $message = 'succes';
    redirect_to('index.php');
}

on the index page i have:

在索引页上我有:

if (!empty($message)) {
    echo '<p class="message"> '.$message.'</p>';
}

The redirect function is working fine:

重定向功能工作正常:

function redirect_to( $location = NULL ) {
        if ($location != NULL) {
            header("Location: {$location}");
            exit;
        }
    }

But it won't display my message. Its empty.

但它不会显示我的消息。它是空的。

回答by Andreas Wong

By the time the redirect happens and the PHP script depicted by $locationis executed, $messagevariable would have been long gone.

到重定向发生并$location执行所描述的 PHP 脚本时,$message变量早就消失了。

To tackle this, you need to pass your message in your location header, using GETvariable:

要解决此问题,您需要使用GET变量在位置标头中传递消息:

header("Location: $location?message=success");

header("Location: $location?message=success");

And

if(!empty($_GET['message'])) {
    $message = $_GET['message'];
// rest of your code

You could also have a look into sessions

您还可以查看会话

session_start();
$_SESSION['message'] = 'success';
header("Location: $location");

then in the destination script:

然后在目标脚本中:

session_start();
if(!empty($_SESSION['message'])) {
   $message = $_SESSION['message'];
   // rest of your code

回答by deceze

Variables cease to exist after the script ends. Each separate request, each separate PHP script invocation is an entirely new context with no data from any other invocation.

脚本结束后变量不再存在。每个单独的请求、每个单独的 PHP 脚本调用都是一个全新的上下文,没有来自任何其他调用的数据。

Use sessionsto persist data.

使用会话来持久化数据。

回答by Mihai Iorga

you can use sessions

你可以使用会话

if (mysqli_affected_rows($link) == 1) 
{
   //succes         
    $_SESSION['message'] = 'succes';
    redirect_to('index.php');
}

and on index

并在索引上

if (!empty($_SESSION['message'])) {
    echo '<p class="message"> '.$_SESSION['message'].'</p>';
    unset($_SESSION['message']);
}

回答by Jocelyn

Since you are running header("Location: {$location}");the value of $location(set in the first file) is lost when index.php is loaded.

由于您正在运行(在第一个文件中设置)header("Location: {$location}");的值,因此$location在加载 index.php 时会丢失。

回答by Anoop

use the following code....

使用以下代码....

header("Location: index.php?message=success");

in index.php

在 index.php 中

$msg=$_GET['message'];
echo $msg;

回答by Snake Plissken

you can avoid redirect function and use this code in the page

您可以避免重定向功能并在页面中使用此代码

header("Location: $locationpage?message=success")

index.php

索引.php

if(!empty($_GET['message'])) {
$message = $_GET['message'];
 echo '<p class="message"> '.$message.'</p>';
}