当前位置:编程学习 > php >>

session 的工作原理与session用法

session 的工作原理与session用法

先来看一个session实例

function getsessiondata ($session_name = 'php教程sessid', $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;
}


 

再看session原理
一直在使用session存储数据,一直没有好好总结一下session的使用方式以及其工作原理,今天在这里做一下梳理。
这里的介绍主要是基于php语言,其他的语言操作可能会有差别,但基本的原理不变。

1.在php中如何操作session:
session_start();   //使用该函数打开session功能
$_session       //使用预定义全局变量操作数据
使用unset($_session['key']) //销毁一个session的值
简单地操作,一切都是由服务器实现;由于处理在后台,一切看起来也很安全。但是session采用什么样机制,又是怎样被实现,并且如何来保持会话的状态的呢?

2.session实现与工作原理
浏览器和服务器采用http无状态的通讯,为了保持客户端的状态,使用session来达到这个目的。然而服务端是怎么样标示不同的客户端或用户呢?
这里我们可以使用生活中的一个例子,假如你参加一个晚会,认识了很多人,你会采取什么方式来区分不同的人呢!你可能根据脸型,也有可能根据用户的名字,
或者人的身份证,即采用一个独一无二的标示。在session机制中,也采用了这样的一个唯一的session_id来标示不同的用户,不同的是:浏览器每次请求都会带上
由服务器为它生成的session_id.
简单介绍一下流程:当客户端访问服务器时,服务器根据需求设置session,将会话信息保存在服务器上,同时将标示session的session_id传递给客户端浏览器,
浏览器将这个session_id保存在内存中(还有其他的存储方式,例如写在url中),我们称之为无过期时间的cookie。浏览器关闭后,这个cookie就清掉了,它不会存在用户的cookie临时文件。
以后浏览器每次请求都会额外加上这个参数值,再服务器根据这个session_id,就能取得客户端的数据状态。
如果客户端浏览器意外关闭,服务器保存的session数据不是立即释放,此时数据还会存在,只要我们知道那个session_id,就可以继续通过请求获得此session的信息;但是这个时候后台的session还存在,但是session的保存有一个过期
时间,一旦超过规定时间没有客户端请求时,他就会清除这个session。
下面介绍一下session的存储机制,默认的session是保存在files中,即以文件的方式保存session数据。在php中主要根据php.ini的配置session.save_handler
来选择保存session的方式。
这里顺便说明一下,如果要做服务器的lvs,即多台server的话,我们一般使用memcached的方式session,否则会导致一些请求找不到session。
一个简单的memcache配置:
session.save_handler = memcache
session.save_path = "tcp://10.28.41.84:10001"
当然如果一定要使用files文件缓存,我们可以将文件作nfs,将所有的保存session文件定位到一个地方。
刚才讲返回给用户的session-id最终保存在内存中,这里我们也可以设置参数将其保存在用户的url中。

3.实例问题
现有系统a,b; 假设a系统是可以独立运行的web系统,即可以和浏览器直接处理session, b系统是基于mobile的,需要调用a系统的功能接口,
在保持a不改变的情况下,即登陆验证,session存储都不变的情况下,b系统能处理前端用户的请求。
这里提供的方案是使用php实现
在用户登陆成功后,将保存的session的session-id返回给b系统,然后B系统每次请求其他接口都带session_id。
A系统在session_start前加上session_id(session_id);
这样b系统就能安全的调用a


session函数还有

session_cache_expire — return current cache expire
session_cache_limiter — get and/or set the current cache limiter
session_commit — alias of session_write_close
session_decode — decodes session data from a string
session_destroy — destroys all data registered to a session
session_encode — encodes the current session data as a string
session_get_cookie_params — get the session cookie parameters
session_id — get and/or set the current session id
session_is_registered — find out whether a global variable is registered in a session
session_module_name — get and/or set the current session module
session_name — get and/or set the current session name
session_regenerate_id — update the current session id with a newly generated one
session_register — register one or more global variables with the current session
session_save_path — get and/or set the current session save path
session_set_cookie_params — set the session cookie parameters
session_set_save_handler — sets user-level session storage functions
session_start — initialize session data
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

 

补充:Php教程,Php入门 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,