PHP 个会话未按预期工作
PHP sessions not working as expected
所以我正在创建一个应用程序(对会话来说是新的)并且一直在尝试使用会话创建一个简单的错误处理语句。
为了简化事情,让我描述一下基础知识。
- 用户在第 1 页输入查询
- 用户按下提交
- 第 2 页已加载以检查查询的值
- 如果设置了查询,则为运行,结果显示在第3页
- 如果它是空的,则会捕获错误,设置错误并将用户重定向回第 1 页
我关心的是最后一步,因为出于某种原因我无法让它工作。
这是与错误相关的代码:
第1页相关代码:
<?php
session_start();
$ERROR = $_SESSION['error'];
if($ERROR) {
echo $ERROR;
}
?>
第 2 页:
<?
session_start();
---------------- And as we go down the file a bit ----------------
if(trim($getQuery == "")){
$ERROR = "no search criteria entered";
$_SESSION['error'] = $ERROR;
if(!isset($_SESSION['error'])) {
die("the session error was not set for some reason");
}
$url = "localhost:8000/mysite"; //index.php is page 1 in this case so I just redirect to the parent directory as index is loaded by default obviously in that case
header("Location:" . $url);
}
?>
$getQuery 是在第 1 页的查询框中捕获的值,并通过 post 方法发送到第 2 页,正如您自然而然地假设的那样。
但是当我在查询框中不输入任何内容然后发送查询时,页面会刷新(当页面 2 意识到查询为空并且 header 位置重新加载页面时应该刷新)但没有错误显示,它应该考虑我在第 2 页检查它是否已设置。
有什么想法吗?
干杯,
-- 标准差
有时浏览器重定向(您的 header("Location"))可能比服务器更快。
您应该在重定向之前放置
session_write_close()
只是为了确保下次写 session。
您在 if(trim($getQuery == "")) { ... }
中有错字,应该是 if(trim($getQuery) == "") { ... }
,因为您只想 trim $getQuery
变量,而不是整个条件。如果你改变这个,那么它就会起作用。
这是一个最低限度的工作示例
<?php // 1.php
session_start();
$ERROR = $_SESSION['error'];
if($ERROR) {
echo $ERROR;
}
?>
<?php // 2.php
$getQuery = ""; // This is empty so it will redirect to 1 and show error message
session_start();
if(trim($getQuery) == ""){
$ERROR = "no search criteria entered";
$_SESSION['error'] = $ERROR;
if(!isset($_SESSION['error'])) {
die("the session error was not set for some reason");
}
$url = "1.php"; //index.php is page 1 in this case so I just redirect to the parent directory as index is loaded by default obviously in that case
header("Location:" . $url);
}
?>
所以我正在创建一个应用程序(对会话来说是新的)并且一直在尝试使用会话创建一个简单的错误处理语句。
为了简化事情,让我描述一下基础知识。
- 用户在第 1 页输入查询
- 用户按下提交
- 第 2 页已加载以检查查询的值
- 如果设置了查询,则为运行,结果显示在第3页
- 如果它是空的,则会捕获错误,设置错误并将用户重定向回第 1 页
我关心的是最后一步,因为出于某种原因我无法让它工作。
这是与错误相关的代码:
第1页相关代码:
<?php
session_start();
$ERROR = $_SESSION['error'];
if($ERROR) {
echo $ERROR;
}
?>
第 2 页:
<?
session_start();
---------------- And as we go down the file a bit ----------------
if(trim($getQuery == "")){
$ERROR = "no search criteria entered";
$_SESSION['error'] = $ERROR;
if(!isset($_SESSION['error'])) {
die("the session error was not set for some reason");
}
$url = "localhost:8000/mysite"; //index.php is page 1 in this case so I just redirect to the parent directory as index is loaded by default obviously in that case
header("Location:" . $url);
}
?>
$getQuery 是在第 1 页的查询框中捕获的值,并通过 post 方法发送到第 2 页,正如您自然而然地假设的那样。
但是当我在查询框中不输入任何内容然后发送查询时,页面会刷新(当页面 2 意识到查询为空并且 header 位置重新加载页面时应该刷新)但没有错误显示,它应该考虑我在第 2 页检查它是否已设置。
有什么想法吗?
干杯, -- 标准差
有时浏览器重定向(您的 header("Location"))可能比服务器更快。
您应该在重定向之前放置
session_write_close()
只是为了确保下次写 session。
您在 if(trim($getQuery == "")) { ... }
中有错字,应该是 if(trim($getQuery) == "") { ... }
,因为您只想 trim $getQuery
变量,而不是整个条件。如果你改变这个,那么它就会起作用。
这是一个最低限度的工作示例
<?php // 1.php
session_start();
$ERROR = $_SESSION['error'];
if($ERROR) {
echo $ERROR;
}
?>
<?php // 2.php
$getQuery = ""; // This is empty so it will redirect to 1 and show error message
session_start();
if(trim($getQuery) == ""){
$ERROR = "no search criteria entered";
$_SESSION['error'] = $ERROR;
if(!isset($_SESSION['error'])) {
die("the session error was not set for some reason");
}
$url = "1.php"; //index.php is page 1 in this case so I just redirect to the parent directory as index is loaded by default obviously in that case
header("Location:" . $url);
}
?>