2016-12-24 99 views
-2

我正在使用以下代碼打印會話值。但是它在回聲步驟之前直接銷燬會話,因此消息未顯示。請幫幫我。

<?php 
    if(isset($_GET['del'])){ 
     $delete = mysql_query("DELETE FROM favourite_tbl WHERE fid='".$_GET['del']."'"); 
     $_SESSION['msg']="Your Favourite Post Has Been Unlisted !"; 
     header('location:my-favorites.php'); 
    } 


    if(isset($_SESSION['msg'])) 
    { 
     echo $_SESSION['msg']; 
     unset($_SESSION['msg']); 
    } 
?> 
+0

你確定它正在銷燬嗎?您需要在此頁面上開始會話。我猜你永遠不會輸入那個條件。 – chris85

+0

你如何設置會話? – yogur

+0

我已經開始session_start(); ,它不會打印會話值。 – Siddharth

回答

0

你需要這樣做。

<?php 
session_start(); 
if (isset($_GET['del'])) 
{ 
    $delete = mysql_query("DELETE FROM favourite_tbl WHERE fid='" . $_GET['del'] . "'"); 
    $_SESSION['msg'] = "Your Favourite Post Has Been Unlisted !"; 
    header('location:my-favorites.php'); 
    exit(); 
} 


if (isset($_SESSION['msg'])) 
{ 
    echo $_SESSION['msg']; 
    unset($_SESSION['msg']); 
} 
?> 

您需要添加到退出();或者在頭重定向函數之後調用die()函數。因爲這個原因,它取消了你的$ _SESSION ['msg'];