php - 注销时重定向并显示 "You have successfully logged out!"

标签 php html

我的网站上有成员(member)服务。目前,当有人注销时,他们会被重定向到 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

最佳答案

你可以这样做:

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

然后在你的 index.php 文件中查看状态是否不为空,并显示一个状态如下的 div:

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

同样在 if 语句中你也可以清除用户 session ..

关于php - 注销时重定向并显示 "You have successfully logged out!",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15594807/

相关文章:

PHP min 和 max 带数组,靠谱吗?

python - 错误消息: 'NoneType' object has no attribute 'split'

javascript - 如何使用javascript将html中的水印添加到pdf导出器

html - 结合两个 CSS 网格的布局

java - Keycloak 在重置密码电子邮件中添加公司 Logo

php - Codeigniter 子查询

php - 我可以在 PHP 中混合使用 MySQL API 吗?

php - Cakephp 通过 CakeEmail 发送电子邮件后未重定向

php - 如何检测 Firefox、Opera 和 IE 中的浏览器插件?

javascript - 仅隐藏那些为空或没有文本的跨度