您的位置:首页 > 编程语言 > PHP开发

ref.session.php

2015-10-28 10:33 483 查看
SESSION函数的详细介绍

session_abort — Discard session array changes and finish session

session_cache_expire — 返回当前缓存的到期时间

session_cache_limiter — 读取/设置缓存限制器

session_commit — session_write_close 的别名

session_decode — 解码会话数据

session_destroy — 销毁一个会话中的全部数据

session_encode — 将当前会话数据编码为一个字符串

session_get_cookie_params — 获取会话 cookie 参数

session_id — 获取/设置当前会话 ID

session_is_registered — 检查变量是否在会话中已经注册

session_module_name — 获取/设置会话模块名称

session_name — 读取/设置会话名称

session_regenerate_id — 使用新生成的会话 ID 更新现有会话 ID

session_register_shutdown — 关闭会话

session_register — Register one or more global variables with the current session

session_reset — Re-initialize session array with original values

session_save_path — 读取/设置当前会话的保存路径

session_set_cookie_params — 设置会话 cookie 参数

session_set_save_handler — 设置用户自定义会话存储函数

session_start — 启动新会话或者重用现有会话

session_status — Returns the current session status

session_unregister — Unregister a global variable from the current session

session_unset — Free all session variables

session_write_close — Write session data and end session


a example of php session

simple session test

<?php
/* [EDIT by danbrown AT php DOT net:
The author of this note named this
file tmp.php in his/her tests. If
you save it as a different name,
simply update the links at the
bottom to reflect the change.] */

session_start();

$sessPath   = ini_get('session.save_path');
$sessCookie = ini_get('session.cookie_path');
$sessName   = ini_get('session.name');
$sessVar    = 'foo';

echo '<br>sessPath: ' . $sessPath;
echo '<br>sessCookie: ' . $sessCookie;

echo '<hr>';

if( !isset( $_GET['p'] ) ){
// instantiate new session var
$_SESSION[$sessVar] = 'hello world';
}else{
if( $_GET['p'] == 1 ){

// printing session value and global cookie PHPSESSID
echo $sessVar . ': ';
if( isset( $_SESSION[$sessVar] ) ){
echo $_SESSION[$sessVar];
}else{
echo '[not exists]';
}

echo '<br>' . $sessName . ': ';

if( isset( $_COOKIE[$sessName] ) ){
echo $_COOKIE[$sessName];
}else{
if( isset( $_REQUEST[$sessName] ) ){
echo $_REQUEST[$sessName];
}else{
if( isset( $_SERVER['HTTP_COOKIE'] ) ){
echo $_SERVER['HTTP_COOKIE'];
}else{
echo 'problem, check your PHP settings';
}
}
}

}else{

// destroy session by unset() function
unset( $_SESSION[$sessVar] );

// check if was destroyed
if( !isset( $_SESSION[$sessVar] ) ){
echo '<br>';
echo $sessName . ' was "unseted"';
}else{
echo '<br>';
echo $sessName . ' was not "unseted"';
}

}
}
?>
<hr>
<a href=tmp.php?p=1>test 1 (printing session value)</a>
<br>
<a href=tmp.php?p=2>test 2 (kill session)</a>

/*----------------------------------------------华丽分割线----------------------*/
<?php
function getSessionData ($session_name = 'PHPSESSID', $session_save_handler = 'files') {
$session_data = array();
# did we get told what the old session id was? we can't continue it without that info
if (array_key_exists($session_name, $_COOKIE)) {
# save current session id
$session_id = $_COOKIE[$session_name];
$old_session_id = session_id();

# write and close current session
session_write_close();

# grab old save handler, and switch to files
$old_session_save_handler = ini_get('session.save_handler');
ini_set('session.save_handler', $session_save_handler);

# now we can switch the session over, capturing the old session name
$old_session_name = session_name($session_name);
session_id($session_id);
session_start();

# get the desired session data
$session_data = $_SESSION;

# close this session, switch back to the original handler, then restart the old session
session_write_close();
ini_set('session.save_handler', $old_session_save_handler);
session_name($old_session_name);
session_id($old_session_id);
session_start();
}

# now return the data we just retrieved
return $session_data;
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: