PHP $ _SESSION消息不起作用

时间:2016-12-24 14:36:11

标签: php

我在下面的代码中使用了打印会话值。但它在echo步骤之前直接销毁会话因此消息没有显示。请帮帮我。

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

1 个答案:

答案 0 :(得分: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']);
}
?>

你需要添加到exit();或标题重定向函数后的die()函数调用。出于这个原因,它取消了你的$ _SESSION [&#39; msg&#39;];