2

我的网站上有会员服务。目前,当有人注销时,他们会被重定向到 logout.php,上面有以下代码:

<?php

        //check if the login session does no exist
        if(strcmp($_SESSION['uid'],”) == 0){
            //if it doesn't display an error message
            echo "<center>You need to be logged in to log out!</center>";
        }else{
            //if it does continue checking

            //update to set this users online field to the current time
            mysql_query("UPDATE `users` SET `online` = '".date('U')."' WHERE `id` = '".$_SESSION['uid']."'");

            //destroy all sessions canceling the login session
            session_destroy();

            //display success message
            echo "<center>You have successfully logged out!<br><a href = '/review-pratt/index.php' class='icon-button star'>Return Home</button></center>";
        }

        ?>

而不是让用户被带到“logout.php”并查看一个说他们退出的无聊页面。我希望它们被重定向到 index.php。这部分很简单,我知道。

我希望在顶部出现一个通知栏,通知他们他们已成功注销。我以前曾尝试过这样做,但从未有任何工作。任何帮助或建议将不胜感激!

更新

我已将 logout.php 代码更改为:

<?php

        //check if the login session does no exist
        if(strcmp($_SESSION['uid'],”) == 0){
            //if it doesn't display an error message
            echo "<center>You need to be logged in to log out!</center>";
        }else{
            //if it does continue checking

            //update to set this users online field to the current time
            mysql_query("UPDATE `users` SET `online` = '".date('U')."' WHERE `id` = '".$_SESSION['uid']."'");

            //destroy all sessions canceling the login session
            session_destroy();

            //Redirect with success message
            header('Location: /index.php?msg=' . urlencode("You have been successfully logged out!"));
        }

        ?>

并将以下代码添加到我的 index.php 中:

    <?php

    if ($_GET['msg'])
{
       echo '<div class="success_message">' . base64_decode(urldecode($_GET['msg'])) . '</div>';
}

?>

当我注销时,我收到此错误:

Warning: Cannot modify header information - headers already sent by (output started at /home/content/38/10473938/html/review-pratt/business_profiles/logout.php:19) in /home/content/38/10473938/html/review-pratt/business_profiles/logout.php on line 35
4

4 回答 4

5

你可以这样做:

header('location: index.php?status=loggedout');

并在您的 index.php 文件中查看状态是否不为空,并显示具有如下状态的 div:

<?php 
   if(!empty($_GET['status'])){
          echo '<div>You have been logged out!</div>';
   }
?>

同样在 if 语句中,您也可以清除用户会话..

于 2013-03-24T03:27:09.040 回答
3

有很多解决方案,但几乎所有解决方案都需要 logout.php 来传递消息,并且 index.php 需要代码来显示消息。

我首选的方法是将消息作为 URL 参数传递。header用于重定向,使用base64_encode缩短 url 中的文本,使用url_encode确保 URL 不会被垃圾。

//Redirect with success message
header('Location: /index.php?msg=' . urlencode(base64_encode("You have been successfully logged out!")));

然后,在您的 index.php 页面上

if ($_GET['msg'])
{
       echo '<div class="success_message">' . base64_decode(urldecode($_GET['msg'])) . '</div>';
}

编辑:如果您的标头已经发出(您是否echo在这些上方的一行中删除了一些文本?),您可以使用 Javascript 进行重定向。

替换header('Location: ')为:echo '<meta http-equiv="Refresh" content="0;url=http://example.com/index.php?msg=' . urlencode(base64_encode('You have been successfully logged out!')) . '">';

于 2013-03-24T03:26:26.403 回答
1

您可以使用“Noty”插件在您的网络应用程序上启用通知。见这里: http: //needim.github.com/noty/

实现应该是这样的:

  1. 将用户重定向到 index.php?logout=1
  2. 使用查询字符串参数填充隐藏字段。
  3. 页面加载时使用 noty 显示隐藏字段值。

这是一个代码示例:

<?php 
   if(!empty($_GET['logout'])){
          echo '<input id="logoutMsg" value="You have been logged out!"  />';
   }
?>

<script>
   var logoutMsg = $('#logoutMsg').val();
   var noty = noty({text: logoutMsg });
</script>
于 2013-03-24T03:27:56.970 回答
0

如果您想在成功消息后立即重定向,请使用以下代码:-

    <?php

        //check if the login session does no exist
        if(strcmp($_SESSION['uid'],”) == 0){
            //if it doesn't display an error message
            echo "<center>You need to be logged in to log out!</center>";
        }else{
            //if it does continue checking

            //update to set this users online field to the current time
            mysql_query("UPDATE `users` SET `online` = '".date('U')."' WHERE `id` = '".$_SESSION['uid']."'");

            //destroy all sessions canceling the login session
            session_destroy();

            //display success message
            echo "<center>You have successfully logged out!
            echo '<meta http-equiv="Refresh" content="0;url=http://url.which.you.want.to.be.redirected.to">';
}
         }
?>
于 2013-03-24T03:30:39.827 回答