Home Backend Development PHP Tutorial php中session的处置机制 (转)

php中session的处置机制 (转)

Jun 13, 2016 pm 12:33 PM
cookie php session string windows

php中session的处理机制 (转)

1。
????? PHP服务端默认的session存储是文件存放方式,在Windows上PHP默认的Session服务端文件存放在C:\WINDOWS\Temp下,可用session_save_path ('./t/');指定具体存放目录。

2。
????? SESSION 的实现中采用COOKIE技术,SESSION会在客户端保存一个包含session_id(SESSION编号)的COOKIE;在服务器端保存其他 session变量,比如session_name等等。当用户请求服务器时也把session_id一起发送到服务器,通过session_id提取所保存在服务器端的变量,就能识别用户是谁了。同时也不难理解为什么SESSION有时会失效了。
???? 当客户端禁用COOKIE时(点击IE中的“工具”―“Internet选项”,在弹出的对话框里点击“安全”―“自定义级别”项,将“允许每个对话 COOKIE”设为禁用),session_id将无法传递,此时SESSION失效。不过php5在linux/unix平台可以自动检查cookie 状态,如果客户端设置了禁用,则系统自动把session_id附加到url上传递。windows主机则无此功能

3。
Session_start() :开始一个会话或者返回已经存在的会话。
在使用Session_start()之前浏览器不能有任何输出,否则会发生以下错误。
你可以在php.ini里启动session.auto_start=1,这样就无需每次使用session之前都要调用session_start()。

4。
如果在session.auto_start=1,会让session_save_path ('./t/');变的无效。因为后一条语句须放前面。

5。
保存在服务器上的session文件,没有遇到session_destroy();,不会被删除。
即使客户端浏览器已关闭。
不过服务器端每次产生的session文件都能保证session文件名的随机性和唯一性。

6。http://www.toplee.com/blog/300.html

增加PHP的Session存储和处理能力

可能很多PHPer都用到了PHP提供的Session功能,可以方便的进行会话功能处理,PHP服务端默认的session存储是文件存放方式,在Windows上PHP默认的Session服务端文件存放在C:\WINDOWS\Temp下,*NIX下默认存放在/tmp下,如果说并发访问很大或者session建立太多,在这两个目录下就会存在大量类似sess_xxxxxx的session文件,同一个目录下文件数过多会导致性能下降,并且可能导致受到攻击最终出现文件系统错误。针对这样的情况,PHP本身体提供了比较好的解决办法。
  不少朋友可能都没有注意到php.ini里面Session设置部分中有这样一项:
;???? session.save_path = "N;MODE;/path"


  这项设置提供给我们可以给session存放目录进行多级散列,其中“N”表示要设置的目录级数,“MODE”表示目录的权限属性,默认为600,在 WINDOWS上基本是不用设置的,*NIX上也可以不用设置,后面的“/path”表示session文件存放的根目录路径,比如我们设置为下面的格式
session.save_path = "2;/tmp/phpsession"

  上面的设置表示我们把/tmp/phpsession目录作为php的session文件存放根目录,在该目录下进行两级目录散列,每一级目录分别是0-9和a-z共36个字母数字为目录名,这样存放session的目录可以达到36*36个,相信作为单台服务器来说,这是完全够用了,如果说您的系统架构设计为多台服务器共享session数据,可以把目录级增加到3级或者更多。
  需要注意的是,php自己并不会自动创建子目录,需要您自己动手去创建,网上找到这样的自动创建目录的代码,大家可以做个参考。下面的代码自动创建3级子目录,可以自己动手根据需要进行修改。
set_time_limit(0);
$string = '0123456789abcdefghijklmnopqrstuvwxyz';
$length = strlen($string);
function makeDir($param)
{
??? if(!file_exists($param)) {
??????? makeDir(dirname($param));
??????? mkdir($param);
??? }
}
for($i = 0; $i ??? for($j = 0; $j ??????? for($k = 0; $k ??????????? makeDir($string[$i].'/'.$string[$j].'/'.$string[$k]);
??????? }
??? }
}
?>

  大家可能注意到前面的文字中提到了有关多服务器共享php的SESSION,这是很多应用都会遇到的问题,网上也有不少相关的资源,大家可以去google一下,Michael这里只提一下大概的思路。
  一般来说我们用到最多的方法有两种:
  1、NFS或者Samba共享的方法,让各个服务器上存放session文件的磁盘共享,这种方法简单可行。
  2、集中存储到数据库中,这是比较多的实现方法,通过php提供的session_set_save_handler()函数来重定义session函数,推荐使用这种方法。

转自:http://www.phpweblog.net/fuyongjie/archive/2009/06/09/6687.html

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1267
29
C# Tutorial
1239
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

How to solve complex BelongsToThrough relationship problem in Laravel? Use Composer! How to solve complex BelongsToThrough relationship problem in Laravel? Use Composer! Apr 17, 2025 pm 09:54 PM

In Laravel development, dealing with complex model relationships has always been a challenge, especially when it comes to multi-level BelongsToThrough relationships. Recently, I encountered this problem in a project dealing with a multi-level model relationship, where traditional HasManyThrough relationships fail to meet the needs, resulting in data queries becoming complex and inefficient. After some exploration, I found the library staudenmeir/belongs-to-through, which easily installed and solved my troubles through Composer.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

See all articles