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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15594807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:32:24  来源:igfitidea点击:

Redirect on logout and display "You have successfully logged out!"

phphtml

提问by user2109152

I have a membership service on my website. Currently when someone logs out they are redirected to logout.php that has this code on it:

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

        ?>

Instead of having the users be taken to "logout.php" and viewing a boring page that says they logged out. I want them to be redirected to index.php. That part is easy, I know.

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

I am wanting a notification bar across the top to appear notifying them that they successfully logged out. I have tried to do this before and never got anything to work. Any help or suggestions would be appreciated!

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

Update

更新

I have changed the logout.php code to:

我已将 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!"));
        }

        ?>

and added the following code to my index.php:

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

    <?php

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

?>

And when I log out I receive this error:

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

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

回答by Andres

you could do something like this:

你可以做这样的事情:

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

and in your index.php file just look to see if status is not empty, and show a div with the status like this:

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

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

also inside that if statement you can clear user session aswell..

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

回答by Luke Shaheen

There are many solutions to this, but almost all of them require logout.php to pass the message, and index.php to have code to display the message.

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

My preferred method is to pass the message as a URL parameter. Use headerto re-direct, use base64_encodeto shorten the text in the url, and url_encodeto make sure that the URL doesn't get junked up.

我的首选方法是将消息作为 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!")));

Then, on your index.php page

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

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

Edit:If your headers have already been sent out (have you echoed out some text on a line above these?), you can use Javascript to do the redirection.

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

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

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

回答by Mortalus

You could use "Noty" plugin to enable notifications on your web-app. see here: http://needim.github.com/noty/

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

Implementation should look something like that:

实现应该是这样的:

  1. Redirect the user to index.php?logout=1
  2. Use the Query String parameter to populate a hidden field.
  3. Use noty to display the hidden field value when page loads.
  1. 将用户重定向到 index.php?logout=1
  2. 使用查询字符串参数填充隐藏字段。
  3. 使用 noty 在页面加载时显示隐藏字段值。

Here is a code example:

这是一个代码示例:

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

回答by surfine

If you want to redirect right after the success message, then use the following code:-

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

    <?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">';
}
         }
?>