1. 配置cookie作用域名,及有效时间
$currentCookieParams = session_get_cookie_params();
$rootDomain = '.study.com';
session_set_cookie_params(
$currentCookieParams["lifetime"],
$currentCookieParams["path"],
$rootDomain,
$currentCookieParams["secure"],
$currentCookieParams["httponly"]
);
session_name('mysessionname');
session_start();
setcookie('zhangsan', 12314, time() + 3600, '/', $rootDomain);
print_r($_COOKIE);
2.配置文件
session.auto_start | Off | O |
session.cache_expire | 180 | 有效时间 |
session.cache_limiter | nocache | nocache |
session.cookie_domain | .study.com | 作用域
|
session.cookie_httponly | Off | 禁止jS获取cookie |
session.cookie_lifetime | 0 | 以秒数指定了发送到浏览器的 cookie 的生命周期。值为 0 表示“直到关闭浏览器”。默认为 0 |
session.cookie_path | / | session存储路径 |
session.cookie_secure | Off | 指定是否仅通过安全连接发送 cookie。默认为 off |
session.gc_divisor | 1000 | 垃圾回收机制相关配置 |
session.gc_maxlifetime | 1440 | 垃圾回收机制相关配置 |
session.gc_probability | 1 | 垃圾回收机制相关配置 |
session.lazy_write | On | On |
session.name | MY_SESSID | 模块名称
|
session.referer_check | no value | no value |
session.save_handler | files | 数据储存方式 |
session.save_path | /tmp/session | 数据存储的位置
|
session.serialize_handler | php_serialize | session存储数据的,数据格式
|
session.sid_bits_per_character | 5 | 5 |
session.sid_length | 26 | 26 |
session.upload_progress.cleanup | On | On |
session.upload_progress.enabled | On | On |
session.upload_progress.freq | 1% | 1% |
session.upload_progress.min_freq | 1 | 1 |
session.upload_progress.name | PHP_SESSION_UPLOAD_PROGRESS | PHP_SESSION_UPLOAD_PROGRESS |
session.upload_progress.prefix | upload_progress_ | upload_progress_ |
session.use_cookies | On | 指定是否在客户端用 cookie 来存放会话 ID |
session.use_only_cookies | On | On |
session.use_strict_mode | Off | 是否开启严格模式 |
session.use_trans_sid |
| 指定是否启用透明 SID |
3.多个会话同时切换
备注:实现原理是在开启
1)定义不同的key保存sesson_id
2) 在调用另一个会话的时候,关闭上一次会话
<?php
// Get the private context
session_name('Private');
session_start();
$private_id = session_id();
$b = $_SESSION['pr_key'];
session_write_close();
// Get the global context
session_name('Global');
session_id('TEST');
session_start();
$a = $_SESSION['key'];
session_write_close();
// Work & modify the global & private context (be ware of changing the global context!)
?>
<html>
<body>
<h2>Test 2: Global Count is: <?=++$a?></h2>
<h2>Test 2: Your Count is: <?=++$b?></h2>
<h2>Private ID is <?=$private_id?></h2>
<h2>Gloabl ID is <?=session_id()?></h2>
<pre>
<?php print_r($_SESSION); ?>
</pre>
</body>
</html>
<?php
// Store it back
session_name('Private');
session_id($private_id);
session_start();
$_SESSION['pr_key'] = $b;
session_write_close();
session_name('Global');
session_id('TEST');
session_start();
$_SESSION['key']=$a;
session_write_close();
?>