PHP会话变量,检查然后删除

时间:2011-11-13 05:35:18

标签: php

代码:     

    if(!$picture) $error = $error."<div>This is a photo site, we require you select a photo.</div>";
    if(!$imgname) $error = $error."<div>You must enter a name for your photo.</div>";
    if(!$description) $error = $error."<div>You must enter a description for your photo.</div>";
    else if(strlen($description) > 500) $error = $error."<div>Your descriptions to long. 500 characters max.</div>";

    if(!$error) {
        $case = 2;

        $max_width_new = 190;

        $make_thumb=1; $make_medium=1; $make_original=1;

        $imagename = "picture";

        $folders = "accounts/".$dbid."/";

        if(!file_exists($folders)) 
            mkdir($folders);

        include("scripts/php/uploadpicture.php");

        if($file) {
        $connect = mysql_connect("localhost","headinth_admin","adobe1234");
        mysql_select_db("headinth_core");
        mysql_query("INSERT INTO pictures (id, uploader, name, description, location, picture, date) VALUES('','$dbid','$imgname','$description','$location','$file_name','$date')");
        $_SESSION['uploaded'] = 1;

        header("location: ?upload");
        }
    } else echo "<div class='green f18 fl coolvetica' style='text-align:left;'>".$error."</div><div class='c20'></div>";
}
if(isset($_SESSION['uploaded'])) {
    echo "<div class='white f18 fl coolvetica' style='text-align:left;'>Your picture was uploaded.</div><div class='c20'></div>";
    unset($_SESSION['uploaded']);
}
?>

所以它通过,如果一切成功,它宣布$ _SESSION ['uploaded'] = 1.有道理。由于它是一个标题,它刷新页面。然后我检查$ _SESSION ['uploaded']是否存在,看看是否刚刚上传了一张照片。如果是,那么它应该发送消息“你的照片已上传”。然后它必须删除$ _SESSION ['uploaded']变量,以便在刷新页面或进行无效提交时不会再次显示该变量。

问题是它只是跳过if(isset($ _ SESSION ['uploaded']))行并且只是取消设置会话。所以它不能回应成功的信息。如果我删除未设置它有效,只会一直出现,直到整个会话被销毁。所以目标是在页面刷新时携带一个变量,然后将其删除,然后将其删除。

2 个答案:

答案 0 :(得分:2)

header()不会停止执行您的脚本。在header()命令之后,脚本继续到您的isset()/unset()块。解决方案是die之后的header()

header("location: ?upload");
die;

答案 1 :(得分:0)

我认为这只是你正在进行输出和取消设置的顺序问题。

$_SESSION['test'] = 1;
echo "test session variable is: {$_SESSION['test']} <br />";
unset($_SESSION['test']);
echo "test session variable is: {$_SESSION['test']}";

将输出:

test session variable is: 1
test session variable is: