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