2013-03-24 396 views
1

我在我的网站上有会员服务。目前,当有人登录了,他们将被重定向到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 
+0

顺便说一句,出于安全原因,确保在将它们插入数据库时​​转义变量。查看“SQL注入”和“mysql_real_escape_string()”http://php.net/manual/en/function.mysql-real-escape-string.php – 2013-03-24 03:55:44

+0

在顶部添加:<?php ob_start(); ?> – Andres 2013-03-24 04:19:58

回答

1

对此有很多解决方案,但几乎所有的解决方案都需要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!')) . '">';

+0

我用我的问题更新了我的问题。谢谢你 – user2109152 2013-03-24 03:43:58

+0

@ user2109152我更新了我的答案 – 2013-03-24 03:51:13

+0

好吧,工作重定向到index.php,但你会如何得到消息? – user2109152 2013-03-24 03:59:35

2

你可以做这样的事情:

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

,并在您的index.php文件只是看看,如果状态不为空,并显示有一个div这样的状态:

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

也在里面,如果语句,您可以清除用户会话藏汉..

1

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

实施应该是类似的东西:

  1. 将用户重定向到的index.php?注销= 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> 
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">'; 
} 
     } 
?>