解决方案一:
利用ini_set函数来设置cookie作用域:
1 | define( 'COOKIE_DOMAIN' , '.6xcc.com' ); |
2 | ini_set ( "session.cookie_domain" , COOKIE_DOMAIN); |
3 | session_start(); |
利用setcookie来设置cookie作用域:
1 | define( 'COOKIE_DOMAIN' , '.sinxxx.com' ); |
2 | $s_name = session_name(); |
3 | if (isset( $_COOKIE [ $s_name ])){ |
4 | $s_id = $_COOKIE [ $s_name ]; |
5 | session_id( $s_id ); |
6 | } |
7 | session_start(); |
8 | setcookie ( $s_name , session_id(), time() + 3*24*3600, '' ,COOKIE_DOMAIN); |
我们来看看上面的两个解决方案在浏览器是怎么工作的?直接使用header输出吧:
1 | header( 'Cookie:PHPSESSID=9k0u3fhd19qovnrfpleqplq0s1; expires=Mon, 09-Apr-2012 02:25:05 GMT; domain=.s869.com' ); |
01 | define( 'COOKIE_DOMAIN' , '.6xcc.com' ); |
02 |
03 | $s_name = session_name(); |
04 | if (isset( $_COOKIE [ $s_name ])){ |
05 | $s_id = $_COOKIE [ $s_name ]; |
06 | session_id( $s_id ); |
07 | } |
08 | session_start(); |
09 |
10 | $expires = gmdate ( "D, d M Y H:i:s" ,time()+3*30*24*3600); |
11 | header( "Cookie:" . $s_name . "=" .session_id(). "; expires=$expires GMT; domain=" .COOKIE_DOMAIN); |