【发布时间】:2013-12-22 05:30:40
【问题描述】:
我在自定义启动会话时遇到问题。出于安全原因,我决定寻找一种在启动会话时安全的方法,我遇到了这个tutorial 并实现了与启动会话相关的方法。
问题是,每当我启动一个新的会话变量并重定向到另一个期望来自初始化会话的值的页面时,我之前初始化的所有会话变量都会被破坏,迫使用户注销。下面是我的我用来启动会话的函数:
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 一切都很完美。
【问题讨论】:
-
您应该始终启动会话
session_start()(并且应该在任何输出之前),而不是在没有会话时。 -
我在一个页面上添加了
sec_session_start函数,并在每个页面上调用这个页面