384   PHP

PHP5中使用session时,会将session_id通过cookie传送给浏览器。
session_id默认的cookie名为PHPSESSID,这个可以在php.ini文件中配置,同时还包括cookie的参数设置,如下

; Name of the session (used as cookie name).
; http://php.net/session.name
session.name = PHPSESSID

; Initialize session on request startup.
; http://php.net/session.auto-start
session.auto_start = 0

; Lifetime in seconds of cookie or, if 0, until browser is restarted.
; http://php.net/session.cookie-lifetime
session.cookie_lifetime = 0

; The path for which the cookie is valid.
; http://php.net/session.cookie-path
session.cookie_path = /

; The domain for which the cookie is valid.
; http://php.net/session.cookie-domain
session.cookie_domain =

; Whether or not to add the httpOnly flag to the cookie, which makes it inaccessible to browser scripting languages such as JavaScript.
; http://php.net/session.cookie-httponly
session.cookie_httponly =

如果不想修改php.ini,可以通过PHP函数来实现,如下

unset($_COOKIE['my_name']);
ini_set('session.name', 'my_name');
session_start();
$key = ini_get('session.name');
$val = session_id();

其中$key是cookie的名;$val是cookie的值,session_id就是通过这个cookie来传输和保存的,
这个cookie的配置可以通过PHP函数来实现,如下:

session_set_cookie_params($time,'/','',false,true);

具体参数意义,请查看这里




Leave a Reply

Your email address will not be published. Required fields are marked *