Home php教程 php手册 【转载】PHP.INI配置:Session配置详细说明教程

【转载】PHP.INI配置:Session配置详细说明教程

Jun 06, 2016 pm 07:43 PM
php.ini session Tutorial detailed illustrate Configuration

网上有很多PHP.INI文件配置的中文说明,但是对于PHP初学者来说在进行PHP运行环境搭建配置时还是容易一头雾水,今天换一种角度来分享如何进行php.ini配置,以求达到解决实际问题的效果,开篇以 PHP教程 方式详细介绍如何通过php.ini来配置Session,以实现基本

网上有很多PHP.INI文件配置的中文说明,但是对于PHP初学者来说在进行PHP运行环境搭建配置时还是容易一头雾水,今天换一种角度来分享如何进行php.ini配置,以求达到解决实际问题的效果,开篇以PHP教程方式详细介绍如何通过php.ini来配置Session,以实现基本的Session应用。

  我们知道在利用PHP进行购物车、用户登录等交互式网站开发时,Session是一种很好的解决方法,如果采用XAMPP,AppServ等PHP安装包,一般情况下,PHP Session设置系统都会配置如果采用手动配置PHP运行环境,就需要我们通过php.ini来对Session进行配置,下面详细介绍如何进行Session配置。

  PHP运行环境说明

  我采用的是DedeAMPZ,PHP版本5.2.4,如果你是手动安装PHP运行环境,你需要将php.ini-dist或者php.ini-recommended重命名为php.ini,并将其复制在windows目录下。

  php.ini中的session配置说明

  下面介绍能让session运行的必要配置步骤

  手动配置PHP运行环境时,最容易遗忘的一项是服务器端session文件的存储目录配置工作,打开php.ini文件,搜索Session,找到session.save_path,默认值为/tmp,代表session文件保存在c:/tmp目录下,默认tmp目录并没有创建,你可以在c盘下创建tmp目录,或者创建一个其他目录,比如leapsoulcn,再修改session.save_path的值,并去掉;,即

  session.save_path = ‘/leapsoulcn’;

  注意事项

  1、一般为了保证服务器的安全,session.save_path值最好设置为外网无法访问的目录,另外如果你是在linux服务器下进行session配置,请务必同时配置此目录为可读写权限,否则在执行session操作时会报错。

  2、在使用session变量时,为了保证服务器的安全性,最好将register_globals设置为off,以保证全局变量不混淆,在使 用session_register()注册session变量时,你可以通过系统全局变量$_SESSION来访问,比如你注册了leapsoulcn 变量,你可以通过$_SESSION['leapsoulcn']来访问此变量。

  session.save_path配置其他说明事项,从php.ini配置文件翻译而来

  你可以使用”N;[MODE;]/path”这样模式定义该路径,N是一个整数,表示使用N层深度的子目录,而不是将所有数据文件都保存在一个目录下。

  [MODE;]可选,必须使用8进制数,默认600(=384),表示每个目录下最多保存的会话文件数量。[MODE;]并不会改写进程的 umask。php不会自动创建这些文件夹结构。可使用ext/session目录下的mod_files.sh脚本创建。如果该文件夹可以被不安全的用 户访问(比如默认的”/tmp”),那么将会带来安全漏洞。当N>0时自动垃圾回收将会失效,具体参见下面有关垃圾搜集的部分。

  如果你服务器上有多个虚拟主机,建议针对每个不同的虚拟主机分别设置各自不同的目录。

  至此最基本的session配置就完成了,你只要保存php.ini,并重启apache,即可使用session功能。

  其他session配置说明

  session.save_handler = ”files”

  默认以文件方式存取session数据,如果想要使用自定义的处理器来存取session数据,比如数据库,用”user”。

  session.use_cookies = 1

  是否使用cookies在客户端保存会话sessionid,默认为采用cookies

  session.use_only_cookies = 0

  是否仅仅使用cookie在客户端保存会话sessionid,这个选项可以使管理员禁止用户通过URL来传递id,默认为0,如果禁用的话,客户端如果禁用Cookie将使session无法工作。

  session.name = “PHPSESSID”

  当做cookie name来使用的session标识名

  session.auto_start = 0

  是否自动启动session,默认不启动,我们知道在使用session功能时,我们基本上在每个php脚本头部都会通过 session_start()函数来启动session,如果你启动这个选项,则在每个脚本头部都会自动启动session,不需要每个脚本头部都以 session_start()函数启动session,推荐关闭这个选项,采用默认值。

  session.cookie_lifetime = 0

  传递sessionid的Cookie有效期(秒),0表示仅在浏览器打开期间有效。

  session.gc_probability = 1

  session.gc_divisor = 100

  定义在每次初始化会话时,启动垃圾回收程序的概率。计算公式如 下:session.gc_probability/session.gc_divisor,比如1/100,表示有1%的概率启动启动垃圾回收程序,对 会话页面访问越频繁,概率就应当越小。建议值为1/1000~5000。

  session.gc_maxlifetime = 1440

  设定保存的session文件生存期,超过此参数设定秒数后,保存的数据将被视为’垃圾’并由垃圾回收程序清理。判断标准是最后访问数据的时间 (对于FAT文件系统是最后刷新数据的时间)。如果多个脚本共享同一个session.save_path目录但 session.gc_maxlifetime不同,将以所有session.gc_maxlifetime指令中的最小值为准。

  如果你在session.save_path选项中设定使用子目录来存储session数据文件,垃圾回收程序不会自动启动,你必须使用自己编写的shell脚本、cron项或者其他办法来执行垃圾搜集。

  比如设置”session.gc_maxlifetime=1440″ (24分钟):

  cd /path/to/sessions; find -cmin +24 | xargs rm

  以上是一些常用的session配置选项说明,更多的session配置选项说明你可以参考php.ini文件中的说明。

  至此,在php.ini配置文件中对session进行配置的PHP教程就介绍完毕了,通过上面的步骤实践与学习,基本的session功能都可以使用,至于session性能等其他方面则需要根据服务器环境和需求进行微调了,这个得自己体会。

点击我。

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 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)

Tutorial on how to use Dewu Tutorial on how to use Dewu Mar 21, 2024 pm 01:40 PM

Dewu APP is currently a very popular brand shopping software, but most users do not know how to use the functions in Dewu APP. The most detailed usage tutorial guide is compiled below. Next is the Dewuduo that the editor brings to users. A summary of function usage tutorials. Interested users can come and take a look! Tutorial on how to use Dewu [2024-03-20] How to use Dewu installment purchase [2024-03-20] How to obtain Dewu coupons [2024-03-20] How to find Dewu manual customer service [2024-03-20] How to check the pickup code of Dewu [2024-03-20] Where to find Dewu purchase [2024-03-20] How to open Dewu VIP [2024-03-20] How to apply for return or exchange of Dewu

In summer, you must try shooting a rainbow In summer, you must try shooting a rainbow Jul 21, 2024 pm 05:16 PM

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

Understand Linux Bashrc: functions, configuration and usage Understand Linux Bashrc: functions, configuration and usage Mar 20, 2024 pm 03:30 PM

Understanding Linux Bashrc: Function, Configuration and Usage In Linux systems, Bashrc (BourneAgainShellruncommands) is a very important configuration file, which contains various commands and settings that are automatically run when the system starts. The Bashrc file is usually located in the user's home directory and is a hidden file. Its function is to customize the Bashshell environment for the user. 1. Bashrc function setting environment

Tutorial on how to turn off the payment sound on WeChat Tutorial on how to turn off the payment sound on WeChat Mar 26, 2024 am 08:30 AM

1. First open WeChat. 2. Click [+] in the upper right corner. 3. Click the QR code to collect payment. 4. Click the three small dots in the upper right corner. 5. Click to close the voice reminder for payment arrival.

What software is photoshopcs5? -photoshopcs5 usage tutorial What software is photoshopcs5? -photoshopcs5 usage tutorial Mar 19, 2024 am 09:04 AM

PhotoshopCS is the abbreviation of Photoshop Creative Suite. It is a software produced by Adobe and is widely used in graphic design and image processing. As a novice learning PS, let me explain to you today what software photoshopcs5 is and how to use photoshopcs5. 1. What software is photoshop cs5? Adobe Photoshop CS5 Extended is ideal for professionals in film, video and multimedia fields, graphic and web designers who use 3D and animation, and professionals in engineering and scientific fields. Render a 3D image and merge it into a 2D composite image. Edit videos easily

How to configure and install FTPS in Linux system How to configure and install FTPS in Linux system Mar 20, 2024 pm 02:03 PM

Title: How to configure and install FTPS in Linux system, specific code examples are required. In Linux system, FTPS is a secure file transfer protocol. Compared with FTP, FTPS encrypts the transmitted data through TLS/SSL protocol, which improves Security of data transmission. In this article, we will introduce how to configure and install FTPS in a Linux system and provide specific code examples. Step 1: Install vsftpd Open the terminal and enter the following command to install vsftpd: sudo

Experts teach you! The Correct Way to Cut Long Pictures on Huawei Mobile Phones Experts teach you! The Correct Way to Cut Long Pictures on Huawei Mobile Phones Mar 22, 2024 pm 12:21 PM

With the continuous development of smart phones, the functions of mobile phones have become more and more powerful, among which the function of taking long pictures has become one of the important functions used by many users in daily life. Long screenshots can help users save a long web page, conversation record or picture at one time for easy viewing and sharing. Among many mobile phone brands, Huawei mobile phones are also one of the brands highly respected by users, and their function of cropping long pictures is also highly praised. This article will introduce you to the correct method of taking long pictures on Huawei mobile phones, as well as some expert tips to help you make better use of Huawei mobile phones.

PHP Tutorial: How to convert int type to string PHP Tutorial: How to convert int type to string Mar 27, 2024 pm 06:03 PM

PHP Tutorial: How to Convert Int Type to String In PHP, converting integer data to string is a common operation. This tutorial will introduce how to use PHP's built-in functions to convert the int type to a string, while providing specific code examples. Use cast: In PHP, you can use cast to convert integer data into a string. This method is very simple. You only need to add (string) before the integer data to convert it into a string. Below is a simple sample code

See all articles