session到底是什么?25号结贴

mike_hao_1980 2002-07-22 12:13:30
有谁能好好讲讲什么是session吗?他与cookie有什么不同呢?25号结贴。
...全文
104 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
_Shakespeare 2002-07-23
  • 打赏
  • 举报
回复
我觉得session就是为服务器和客户端之间的“交流”提供了一种安全而方便的手段。它会在客户端保留特殊的sessionID,当登陆时服务器验证这个ID---但是这也有不安全的因素,就是只有那些合法注册的用户才能得到sessionID,所以,有些人可以通过输入sessionID来达到看到它不应该看到的东西。以下是关于session的一些东西
Session对象
Session对象用于存储和接受有关某一特殊用户进程的信息。
集合
Session.Contents(Key)
所有在Session水平定义的,没有使用<OBJECT>的数据和对象的集合。Key是Session条目的名称。
Session.StaticObjects(Key)
所有使用<OBJECT>在Session水平定义的数据和对象的集合。Key是返回的属性。
方法
Session.Abadon
用于取消一个用户Session。破坏包含在用户Session中所有的数据和对象。
属性
Session.CodePage
指明当显示动态内容时,所使用的服务器代码页。
Session.LCTD
指明当显示动态内容时,所使用的局部标识。
Session.SessionID
一个用户session的唯一标识。
Session.TimeOut
在一个空闲session自动结束之前所等待的时间,该属性的缺省值为20分钟。
关于cookie :
在互联网上从来没有一种技术象cookie这么有争议,它是服务器在客户端保存的一些和用户有关的一些信息,以次来跟踪用户在互联网上的行为
session默认的变量就是cookie---全局变量--在php4以后实现了跨窗口的传递。当用户的浏览器禁止session时,session就用querystring来传递变量---就是平常看到的url中?后面的东西

浅妄薄见,望与斟酌
fminhua 2002-07-23
  • 打赏
  • 举报
回复
session比cookie更安全,它的信息保存在服务器上。cookie的信息保存在客户端。
_Shakespeare 2002-07-22
  • 打赏
  • 举报
回复
看看session的发明人是怎么说的
PHP4 adds some session management functions that make our life easier when dealing with sessions. The ones we are interested in are:


--------------------------------------------------------------------------------

session_start();
session_register();


--------------------------------------------------------------------------------

session_start() is used to start up PHP4's session management capabilities; you need to call it before you use any of the other session functions. session_register() is used to tell PHP which variables to track in the session. A typical call to these functions would look like this:

session_register("SESSION");

This tells PHP to start up the session manager, and tells PHP that the variable called SESSION is a session variable. You can register as many session variables as you like, but I prefer to only register one session variable called SESSION, and anything I need persistent I put into this variable. For example, I like to say:



--------------------------------------------------------------------------------

session_register("SESSION");
$SESSION["var1"] = 5;
$SESSION["var2"] = 6;


--------------------------------------------------------------------------------

instead of


--------------------------------------------------------------------------------

session_register("var1");
session_register("var2");
$var1 = 5;
$var2 = 6;


--------------------------------------------------------------------------------
because after you register lots of session variables, you tend to forget what they were, well, at least I do :).
Anyhow, by now you probably want to see some code in action, so create a script called session_test.php somewhere accessible, and put into it:



--------------------------------------------------------------------------------

<?
session_start();
session_register("SESSION");

if (! isset($SESSION)) {
$SESSION["count"] = 0;
echo "<li>Counter initialized, please reload this page to see it increment";
} else {
echo "<li>Waking up session $PHPSESSID";
$SESSION["count"]++;
}
echo "<li>The counter is now $SESSION[count] ";
?>


--------------------------------------------------------------------------------

Fire that up in your browser, the first time you hit the page, it should say " Counter initialized, please reload this page to see it increment". Each time you reload it, the counter value should increment by one. You will also see the session ID. If it does, then hurray, your PHP4 session manager works :)

So how does this work? Well, when you call session_start(), PHP4 determines a unique session ID for the client. This session ID is an MD5 hash of something (not sure what), and it is stored as a cookie on the client's PC.

Now each time that client makes a request, PHP4 will read this session ID and load up the data for the session. When you call session_register(), you are telling PHP4 which variables you want kept in the session. Each page that loads up, the previous values for the registered variables will be reloaded, and each time the page ends PHP4 will save the values of the registered variables.

By default, PHP keeps track of the sessions in temporary files in the /tmp directory, take a listings and see for yourself:

You will see something like this:



--------------------------------------------------------------------------------

-rw------- 1 apache web 10 May 7 15:27 sess_6dd9ea8e61cd49cd3ad6de8c8b8885e8
-rw------- 1 apache web 10 May 7 19:49 sess_7d7f97afb6759948f554b00272494e52
-rw------- 1 apache web 6 May 9 01:00 sess_8ab78830e151add9d79b628958ce4eb9
-rw------- 1 apache web 31 May 9 11:41 sess_a3058a6bb1baf57f565c3844c8810f4b
-rw------- 1 apache web 30 May 9 11:42 sess_c379faad83ad3dc8ab6d22c14dbab3b4
-rw------- 1 apache web 6 May 8 01:00 sess_cd68a5054241aff1a8157c289683e869
-rw------- 1 apache web 34 May 7 15:17 sess_cd97e41912b28c44cc0481b7d978cb61
-rw------- 1 apache web 42 May 9 11:23 sess_d1285edd0c951c70b1aec17a5f602fc0
-rw------- 1 apache web 30 May 9 11:42 sess_da93f6e19b6be01257d7a6453766a23d
-rw------- 1 apache web 42 May 7 21:26 sess_e837123c1af78c538e89b47030fde337


--------------------------------------------------------------------------------

Each one of those files is a session, let's take a look at one of them (note, you probably have to su to root to peek inside a session file). Tip: don't just cut and paste the following commands, you need to specify the name of a real file:



--------------------------------------------------------------------------------

# more /tmp/sess_a3058a6bb1baf57f565c3844c8810f4b


--------------------------------------------------------------------------------

You will see something like this:



--------------------------------------------------------------------------------

SESSION|a:1:{s:5:"count";i:234;}


--------------------------------------------------------------------------------

Does that look familiar? It should if you've ever used the serialize() and unserialize() functions in PHP. If not, don't worry about it. Anyhow, I just wanted to illustrate how sessions were stored. You can rewrite the PHP session handlers to store sessions into a database or whatever else, but that's beyond the scope of this tutorial (but it's not hard at all).

另外,这里有一些php的不错的东西
http://unix.cdnet.edu.cn/LinuxDoc/LDP/HOWTO/PHP-HOWTO.html

浅妄薄见,望与斟酌
spgoal 2002-07-22
  • 打赏
  • 举报
回复
特殊的COOKIE,生存期始于用户访问网站页面,终于用户离开网站
利用SESSION可以存储跨网页的全局变量
可乐加水 2002-07-22
  • 打赏
  • 举报
回复
其实就是在服务器上保存的登陆用户的个人信息!
用来确定该用户资料的
Jade 2002-07-22
  • 打赏
  • 举报
回复
我是这样理解的:
1.是http这种无状态的协议的补充,可以保存一定的信息,用来实现与用户的交互.这个信息保存与服务器中.每个用户不相同.
2.有一定的时间限制,与用户上网的情况相吻合.一般从登录开始到一定的时间.
3.cookie保存在客户端,在安全不及session.

The_east_key 2002-07-22
  • 打赏
  • 举报
回复
我对session理解不深,一点浮浅的理解,有不对的地方,也请指教
session的作用:session在用户访问时产生(当然,程序里使用session的情况下),它在服务器端通过web服务器,创建该用户专用的session空间,用来存储与该用户个人相关的信息。如登陆信息等。这种状态仅与该用户相关,与别的任何用户,甚至说,与该用户的别的浏览器都没有关系,这是安全上的实现。当用户进程的破弃发生时,该session执行两种任务,1是马上破弃,清除用户信息等。2是保留状态,一般是第二种情况,服务器一般设定一个session过期时间,如1分钟或半小时。之后,session破弃。在我们使用浏览器进行浏览时,通常会[碰到这样的情况,打开浏览器以后,有一段时间没有去进行任何工作,这时候,服务器端session破弃,启动清除session存储区,此时在进行任何工作,均会得到session过期的提示消息,可是参照hotmail

session可以用于全局控制,如安全信息的控制。及时进行用户验证等工作。
再一个,如需要实现购物小车,在线购物之类功能的程序的时候,session将会起到很大的作用。可以将需要的信息,及时地存储在session区,动态的进行访问等。
在开发一些较大规模的网站的时候,session就显得很有必要。

有什么不明白的地方,可以再联系。
Nginx的反向代理 http://www.cnblogs.com/zhrxidian/p/5491285.html tomcat+nginx+redis实现均衡负载、session共享(二) 今天我们接着说上次还没完成session共享的部分,还没看过上一篇的朋友可以先看下上次内容,http://www.cnblogs.com/zhrxidian/p/5432886.html。 1.redis简介及下载安装 作为这次的主角,相信大家对redis应该都一定印象,redis是一款开源的高性能key-value数据库,拥有丰富的键值储存类型,并提供多种语言的API。 与一般数据库不同,redis是使用内存作为主存,而使用硬盘来实现数据持久化,而且redis是周期性的将数据写到硬盘上。这就意味着一旦我们服务器出现断电、重启之类的情况,我们很可能会出现数据丢失的情况,所以不建议使用redis来存放关键的数据。当然,也正因为redis读写数据都使用内存,所以它的速度是非常快的,很适合我们来存放一些临时性的数据。 此外,redis能实现的作用很多,诸如队列、缓存之类的,但我也还没使用过,无法在这里为大家说明,但不影响我们今天的session共享功能。 首先我们先下载redis,这是windows版本的下载地址 https://github.com/ServiceStack/redis-windows。 可以点击右边下载全部文件,但感觉没必要,而且下载速度偏慢,建议进入downloads里面下载我们所需的redis包即可。 redis在windows下是免安装的,下载完成后,解压,将文件夹复制到自己某个盘中就好了。解压后是这个样子的。 一开始redis是默认不需要密码,如果想要设置密码,可以进入redis.windows.conf文件下找到requirepass,删除前面的#,在其后面便可以设置密码,我这里设成了123456。 下面我们打开redis。首先打开cmd,进入我们redis目录下,输入redis-server.exe redis.windows.conf。出现下面界面,则打开成功。(不可偷懒想双击redis-server.exe完事,这样虽然也能打开,但不会加载配置文件)。 另外和别的数据库一样,我们需要安装一个辅助的可视化工具Redis Desktop Manager,这是下载地址:http://redisdesktop.com/download。 我们下载windows版本,安装完成后,还没有任何连接对象,那就让我们给它添加一个。点击下方的connect to redis server,出现个弹出框。然后Name我们可以随便输,Host添我们redis服务器的ip地址,本地可以直接填写localhost,端口默认为6379,Auth就是密码,不是必填项,如果没设置密码可不填,点击OK,建立完成。 至此,我们的准备工作都已经完成,下面开始我们的spring与redis的整合之旅。 2.Spring与Redis的整合之旅 这是上篇文章结束时我的项目目录。 其实Spring本身就有提供对redis的支持,就是spring-session,我们只需将这个在pom.xml添加如下代码,maven便会自己下载所需的jar包及依赖包。 1 <dependency>2 <groupId>org.springframework.session</groupId>3 <artifactId>spring-session-data-redis</artifactId>4 <version>1.1.1.RELEASE</version>5 <type>pom</type>6 </dependency> 随后我们在resources文件夹中新建一个redis.properties,往里面添加如下内容。 再新建一个spring-redis.xml,往里面添加我们redis相关的配置。其中maxInactiveIntervalInSeconds是设置session有效时间,以秒为单位,但实际上无论怎么设,session真实有效时间还是会比我们设置的稍微长一些。 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http:/

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧