2013-03-24 382 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">'; 
} 
     } 
?>