ref.session.php

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;
}
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值