使用 php header 位置在 POST 页面之间交换消息

Using php header Location to exchange messages between pages with POST

我已经在使用这个代码:

<?php
// [...]
ELSE IF(isset($_GET['msg'])){
    echo "<div id='noticePanel' class='panel panel-success'><div class='panel-heading'>
    <h2 class='panel-title'>Good job!</h2></div>
    <div class='panel-body'><h4>".addslashes(strip_tags(trim(@$_GET[msg])))."</h4></div></div>";
    }
//[...]
?>

如果某些操作顺利,从其他页面调用:

  if ($result){
      header("Location:index.php?msg=DB updated succesfully");
      exit;
  }

但是 如您所想,它并不干净。工作,但不是最好的。

谁能告诉我是否应该将其移至 POST 请求,或者此代码是否 "widely" 用于在页面之间交换状态消息?

并且,如果是 POST,有人可以写下一些代码或将我重定向到对此有很好解释的页面吗?当然,我已经搜索过了,我发现 THIS from mozilla, THIS 和 jquery,并且通常在 google 上搜索 "send post php header"。 但是我就是不明白该怎么做。

非常感谢你们!

我想我会这样做:

<?php
 session_start();
 $id = $user_profile['id'];
 $_SESSION['id'] = $id;
 header('Location: checkIfExsists.php');
?>

然后在 checkIfExsists.php 页面上,检索变量:

<?php
 session_start();
 $id = $_SESSION['id'];
?>

新密码

session_start()
// [...]
ELSE IF(isset($_GET['msg'])){
    echo "<div id='noticePanel' class='panel panel-success'><div class='panel-heading'>
    <h2 class='panel-title'>Good job!</h2></div>
    <div class='panel-body'><h4>".addslashes(strip_tags(trim(@$_SESSION['msg'])))."</h4></div></div>";
    }

还有..

session_start();
if ($result){
          $_SESSION['msg'] = "DB updated!";
          header("Location:index.php?msg");
          exit;
      }

因为你更喜欢保持你的代码纯净

我只是建议你不要在GET/POST

中发送消息

只需发送

之类的状态
index.php?status=success

然后检查结果文件中的状态

if(isset($_GET['status']) AND $_GET['status'] == success){
   echo "bla bla";
}elseif(isset($_GET['status']) AND $_GET['status'] == fail){
   echo "bla bla";
}else{
   //do something
}

编辑

如果你有很多消息,那么你可以创建数组

$message = [
  'success' => 'success message here',
  'fail' => 'failmessage here',
  'notFound' => 'not found message'
];

$status = ( isset($_GET['status']) AND 
            isset($message[$_GET['status']]) )?$_GET['status']:'notFound';

echo $message[$status];