元刷新内容不会返回正确的页面

时间:2014-12-17 07:59:48

标签: html meta-tags meta

我有点困惑这项工作以及如何使用它。我有一个页面,点击按钮后加载。当用户被删除时,我想回到users.php页面。到目前为止我尝试过但没有工作的是:

<META HTTP-EQUIV="Refresh" Content="2; URL=admin/users.php">

也是这样:

<META HTTP-EQUIV="Refresh" Content="2; URL=users.php">

我收到404.php错误。文件位于同一目录中。我也有这个

<base href="http://example.com/app/admins/">

users.php的路径为example.com/app/admins/admin/users.php

修改: Flash会话消息

userdelete.php

            if (isset($_POST)) {
               $_SESSION['postIsSet'] = 'Deleted!!';
            } else {
                $_SESSION['postIsSet'] = false;
            }
            header('Location: users.php');

users.php

       if (isset($_SESSION['postIsSet'])) {
           if ($_SESSION['postIsSet'] == true) {
               echo $_SESSION['postIsSet'];
                    unset($_SESSION['postIsSet']);
           } else {
               echo "Post is not set - Flash Message";
           }
        }
        else {}

4 个答案:

答案 0 :(得分:2)

在PHP中,您可以使用header()重定向到其他页面。

header('Location: http://example.com');

但是你不能在标题之前输出任何内容。由于您也想输出一些消息,您可以使用以下代码:

    header("refresh: 2; http://www.example.com/");
    echo <<< MESSAGE
<html>
    <head></head>
    <body>
        Some message to show
    </body>
</html>
MESSAGE;

它将在2秒后重定向用户,同时显示一些代码。如果您需要更复杂的代码,可以使用ob_*函数来收集任何输出,然后输出它。


Flash消息示例

[doStuff.php]

session_start();

if (isset($_POST)) {
    $_SESSION['postIsSet'] = true; // or some string
} else {
    $_SESSION['postIsSet'] = false;
}

header('Location: http://example.com/user.php');

[user.php]

session_start();

if (isset($_SESSION['postIsSet'])) {
   if ($_SESSION['postIsSet'] == true) {
       // If you set message in session, you can do `echo $_SESSION['postIsSet'];`
       echo "Post is set - Flash Message"; 
   } else {
       echo "Post is not set - Flash Message";
   }
   unset($_SESSION['postIsSet']);
} else {
   // Flash message is not set yet.
}

答案 1 :(得分:1)

Works fine for me

<!DOCTYPE html>
<html>
<head>
  <base href="http://example.com/app/admins/">
  <meta charset="utf-8">
  <META HTTP-EQUIV="Refresh" Content="2; URL=admin/users.php">
  <title>JS Bin</title>
</head>
<body>

</body>
</html>

答案 2 :(得分:1)

试试这个会起作用:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <META HTTP-EQUIV="Refresh" Content="2; URL=http://example.com/app/admins/admin/users.php">
  <title>JS Bin</title>
</head>
<body>

</body>
</html>

答案 3 :(得分:0)

如果您仍然想要使用header location而不是刷新,那么在我阅读您的评论后,您可以做什么。这对我很好: 在userDelete.php

header('Location: users.php?msg=deleted');

在您的users.php中

if(isset($_GET['msg'])){
   echo $_GET['msg'];
}

工作得很好

相关问题