什么是Session?

来源:互联网 发布:socket编程用什么软件 编辑:程序博客网 时间:2024/05/16 08:13
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

 

首先解释一下什么是Session,如果你不知道的话。当一个访问者来到你的网站的时候一个Session就开始了,当他离开的时候Session就结束了。本质是来说,cookie是和浏览器有关系,而Session变量就可以存一些资源变量在服务器上面。PHP4用文件存储Session变量,但理论上可以用数据库或共享内存来做这件事。

所有的页面都用PHP4Session必须用Session_start()功能函数来告诉PHP4引擎来取有关的Session到内存中

。函数Session_start()可以在cookie域里或请求的参数中取得Session_id为了响应http请求。如果不能找到

SessionID就新建一个Session

什么是Session变量?
Session变量是个有规律的全局变量,当一个Session变量被注册,用PHP4可以在所有的页面上得到Session

的值。用Session_register("variable_name")可以注册一个Session变量。在所有并发的的用Session就使用

Session_start()函数,变量的值将作为一个Session变量注册为Session

我们能作什么?
通常有很多的方法来管理SessionSession变量,我将给你个例子。说你将建一个商业站点,象我这样的,

你可能想保持已经被承认的用户当前的名字,或有多少的新消息用户已经得到。为了不在从数据库里读取,你

有两个方法可以做:
1.1.你可以用三个cookie
authenticated_user - 当前的用户名称
num_messages - 他得到的信息的数量
expire_time - 何时重新读取信息数量
2.2.Sessions和新建三个Session变量
第一个方法安全性不好,一些人可以得到cookie进入他人的领域。
Sessions用户仅得到一个cookie,安全的多。

缺点

Session给了你自由,过度的用Session会影响脚本语言的使用。虽然PHP4Session有些限制,如你不能存对象

Session里。

自己试一试吧!<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击