会话是有状态的,PHP 用户代码不是 [英] Sessions are stateful, PHP user code is not
问题描述
在验证用户输入并存储登录凭据后,我通过调用 session::start 启动会话,然后通过调用 general::reload 重新加载 index.php 文件.我使用 session::is_start() 来确定从索引文件加载哪个页面.
After validating user input and storing login credentials I start a session by calling session::start and then reload the index.php file by calling general::reload. The I use session::is_start() to determine which page to load from the index file.
我认为这不能正常工作,因为我总是加载相同的页面 - b1e.htm.
I don't think this is working correctly as I always get the same page loaded - b1e.htm.
我担心的是,我的静态类会话在 AJAX/PHP 调用和重新加载 index.php 调用之间没有保持其值.
My concern is that my static class session does not maintain its value between the AJAX/PHP call and the reload index.php call.
类似的帖子 - 此处
index.php
include 'b2.php';
if(session::is_start())
{
include 'b2e.htm'; // user is logged in
}
else
{
include 'b1e.htm'; // user is not logged it
}
代码片段 - session:start() 和 session::is_start();
Snippet - session:start() and session::is_start();
class session
{
protected static $ses_id ="";
public static function start()
{
self::$ses_id = session_start();
}
public static function is_start()
{
return self::$ses_id;
}
public static function finish()
{
self::$ses_id = 0;
$_SESSION=array();
if (session_id() != "" || isset($_COOKIE[session_name()]))
{
setcookie(session_name(), '', time()-2592000, '/');
}
session_destroy();
}
}
代码段 - 常规::reload()
Snippet - general::reload()
class general
{
public static function reload()
{
$uri = 'http://';
$uri .= $_SERVER['HTTP_HOST'];
header('Location: '.$uri.'/host_name');
}
推荐答案
您需要调用您的 session_start();
以在每个页面上实际启动会话.
You need to call your session_start();
to actually start the session on each page.
这篇关于会话是有状态的,PHP 用户代码不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!