Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当我重定向到同一个脚本时,为什么会话变量被自动取消设置?_Php - Fatal编程技术网

Php 当我重定向到同一个脚本时,为什么会话变量被自动取消设置?

Php 当我重定向到同一个脚本时,为什么会话变量被自动取消设置?,php,Php,我在testmysql.php中设置了一个名为foo的会话变量,并将脚本重定向到同一个脚本,在这里我打印会话变量,然后取消设置。。然而,当我重定向时,会话变量似乎已经被取消设置。这是为什么?如何解决 <?php session_start(); if(empty($_GET)) { $_SESSION['foo'] = 'bar'; header("location:testmysql.php?redirect". '=' . 'bar'); } var_dump(

我在testmysql.php中设置了一个名为foo的会话变量,并将脚本重定向到同一个脚本,在这里我打印会话变量,然后取消设置。。然而,当我重定向时,会话变量似乎已经被取消设置。这是为什么?如何解决

<?php 

session_start();

if(empty($_GET)) {
    $_SESSION['foo'] = 'bar';
    header("location:testmysql.php?redirect". '=' . 'bar');
}

var_dump( $_SESSION);
unset ($_SESSION['foo']);

if
替换为
if-else

if(empty($_GET)) {
    $_SESSION['foo'] = 'bar';
    header("location:testmysql.php?redirect". '=' . 'bar');
}
else
{
    var_dump( $_SESSION);
    unset ($_SESSION['foo']);
}

设置重定向时您没有中止脚本,因此总是执行
unset($\u SESSION['foo'])

<?php 

session_start();

if(empty($_GET)) {
    $_SESSION['foo'] = 'bar';
    header("location:testmysql.php?redirect". '=' . 'bar');
    exit; // <--- Missing
}

var_dump( $_SESSION);
unset ($_SESSION['foo']);

位置HTTP头只接受绝对URI,不接受相对URI。不要依赖浏览器错误恢复。