注销时重定向并显示“您已成功注销!”

时间:2013-03-24 03:20:46

标签: php html

我的网站上有会员服务。目前,当有人注销时,会将其重定向到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 个答案:

答案 0 :(得分:3)

你可以这样做:

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

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

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

也在if语句中你可以清除用户会话..

答案 1 :(得分:2)

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

我首选的方法是将邮件作为URL参数传递。使用header重定向,使用base64_encode缩短网址中的文字,使用url_encode确保网址不会被废弃。

//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进行重定向。< / p>

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!')) . '">';

答案 2 :(得分:1)

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

实现应该是这样的:

  1. 将用户重定向到index.php?logout = 1
  2. 使用“查询字符串”参数填充隐藏字段。
  3. 使用noty在页面加载时显示隐藏的字段值。
  4. 这是一个代码示例:

    <?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>
    

答案 3 :(得分: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">';
}
         }
?>