Home php教程 php手册 [PHP]PHP的session机制,配置与高级应用

[PHP]PHP的session机制,配置与高级应用

Jun 06, 2016 pm 07:49 PM
php session application mechanism Configuration advanced

--------------------------------------------------------------------------------------------------------- 一. PHP的session原理: 1.开启session_start(),服务器端产生session文件,将相关信息存储到这个文件中,文件以"sess_"为前缀,连接session_i

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

一. PHP的session原理:

1.开启session_start(),服务器端产生session文件,将相关信息存储到这个文件中,文件以"sess_"为前缀,连接session_id()值来命名(内容是由特定算法生成的用户的明文信息,大型项目则须进行加密处理),同时将sessionid保留到客户端浏览器的cookie中。

2.第二次开启session_start(),浏览器判断cookie中是否有sessionid,如果有,不再创建文件,而是直接使用这个sessionid访问服务器,通过sessionid找对应的session文件,读取session中内容存储到$_SESSION数组中。

 

注意:session_start()前不能有任何输出,此函数向客户端发一个session_id保存在cookie中。

 

二. PHP机制使用session:

  1.开启session

    session_start();

  2.存取session

    $_SESSION['uname'] = 'chenwei';

  3.销毁session

   unset($_SESSION['uname']);         //删除内存中由session数组保存的变量;$_SESSION = array()清楚所有session变量

   setCookie(session_name(), '', time()-1, '/');  //删除客户端cookie中的sessionid, session_name()默认是PHPSESSID

   session_destory();               //销毁服务器端session文件

 

三. PHP的session配置选项

查看session配置的最直接方式就是phpinfo(); 然后根据需要配置/usr/local/php/etc/php.ini文件里的session选项,其实一般默认就可以。

可以考虑更改的配置选项有:

  session.cookie_lifetime = 0;     //设置cookie保存时间(秒),相当于session的过期时间,0表示直到浏览器重启

    session.save_handler = files;     //保存和取回数据的方式,默认为文件; 涉及到存储机制,此处不作延伸

当然也可以不修改配置文件,使用系统函数,达到同样的效果:

  setCookie(session_name(), session_id(), time()+1440, '/'); //设置cookie有效时间为1440秒(24’),解决关闭浏览器session丢失的现象;session在服务器上存的最长时间为24’,重新连接后才不会过期。(即保持登录功能)

 

四. PHP的session自动回收机制(启动概率 = session.gc_probability / session.gc_divisor)

  session.gc_probability = 1     //启动几率

  session.gc_divisor = 1000     //被除数,因子

  session.gc_maxlifetime = 1440  //设置session的最大过期时间,默认1440''

只要客户端存在操作,session过期时间就会保持不减;当超过过期时间未操作,回收机制就会启动,将服务端session删除。

  

五. 基于URL传递session的id(解决客户端禁用cookie的情况)

  1. url传参:

    DEMO

  服务端处理:

    $sid = !empty($_GET[session_name()]) ? $_GET[session_name()] : '';

    session_id($sid);  //设置用已有的sid开启会话,在session_start()之前

    session_start();

  2. 如何自动判断是否开启了cookie,并使用对应的传递方式?(使用系统自带SID常量,自动判断)

    DEMO

    $sid = !empty($_GET[session_name()]) ? $_GET[session_name()] : '';

    if($sid != ''){

      session_id($sid);

    }

  3. 使URL自动加session的id?

    enable_trans_uid配置选项,和运行时选项session.use_trans_sid都被激活时,当客户端禁用cookie时,相对的URL将自动被修改为包含sessionid,如果没有配置或使用Windows服务器,可以使用常量SID。

 

Link: http://www.cnblogs.com/farwish/p/3909087.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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

See all articles