刷新页面PHP后会话到期

时间:2013-12-05 10:16:28

标签: php session

我遇到了自定义启动会话的问题。出于安全原因,我决定在启动会话时查找一个安全的方法,并且遇到了这个tutorial并实现了与启动会话相关的方法。 / p>

问题在于,每当我启动一个新的会话变量并重定向到另一个期望来自初始化会话的值的页面时,我之前初始化的所有会话变量都会被破坏,迫使用户注销.Below是my我用来启动会话的功能:

 function sec_session_start(){
$session_name = 'sec_session_id';//set a custom session Name
$secure = false;//true if are using https
$httponly = true; //this stops javascript from accessing session id 

ini_set('session.use_only_cookies', 1);//FORCES session to only use cookies
$cookie_params  = session_get_cookie_params();//Get current cookie params
session_set_cookie_params($cookie_params['lifetime'],$cookie_params['path'],$cookie_params['domain']
        ,$secure,$httponly);
session_name($session_name);//set the session name to the one set above
if (!isset($_SESSION)){session_start();}//start the php session
session_regenerate_id();//regenerate new session id and delete the old one THIS IS TO  PREVENT SESSION HIJACK

}   

我找不到运气的问题答案,请帮帮我。

N.B - 当我使用默认的session_start时   一切都很完美。

2 个答案:

答案 0 :(得分:0)

您应该开始会话,而不是在未设置$_SESSION时。

if (!isset($_SESSION)){session_start();}//start the php session
session_regenerate_id();//regenerate new session id and delete the old one THIS IS TO  PREVENT SESSION HIJACK

应该是

session_start();//Start new or resume existing session
session_regenerate_id();//regenerate new session id and delete the old one THIS IS TO  PREVENT SESSION HIJACK

参考:session_regenerate_id

答案 1 :(得分:0)

尝试将session_start()放在php代码的顶部,作为第一条指令。

相关问题