Php. INI Configuration: Session Configuration details Tutorial

Source: Internet
Author: User
Tags garbage collection php session php script php tutorial sessions

There are a lot of php.ini file configuration in Chinese, but for PHP beginners in the PHP operating environment to set up the configuration is still easy to confused, today to share a different perspective on how to php.ini configuration, in order to achieve the effect of solving practical problems, the beginning of the PHP tutorial way to detail how to pass PHP.ini To configure the session to achieve basic session application.

We know that in the use of PHP for shopping carts, user login and other interactive web site development, session is a good solution, if the use of Xampp,appserv and other PHP installation package, in general, PHP Session settings system will be configured if the use of manual configuration of the PHP operating environment, we need to pass php.ini to configure the session, the following details how to configure the session.

  PHP Operating Environment Description

I am using the dedeampz,php version 5.2.4, if you are installing the PHP running environment manually, you need to rename php.ini-dist or php.ini-recommended to php.ini and copy it to the Windows directory.

  Session Configuration instructions in php.ini

The following are the necessary configuration steps to enable the session to run

Manually Configuring the PHP runtime Environment, the most easily forgotten is the server-side session file storage directory configuration work, open php.ini file, search session, find Session.save_path, default value is/tmp, On behalf of the session file saved in the C:/tmp directory, the default TMP directory is not created, you can create a TMP directory under C disk, or create a different directory, such as LEAPSOULCN, and then modify the Session.save_path value, and remove;

Session.save_path = '/LEAPSOULCN ';

   Precautions:

1, generally in order to ensure the security of the server, the Session.save_path value is best set to the external network can not access the directory, in addition if you are under the Linux Server for session configuration, please be sure to configure this directory for read and write permissions, Otherwise, an error occurs when performing a session operation.

2, in the use of session variables, in order to ensure the security of the server, it is best to set the register_globals to off, to ensure that global variables are not confused, in the use of Session_register () Registration session variable, you can pass the system global variables $_session to visit, for example, you register a LEAPSOULCN variable, you can access this variable by $_session[' LEAPSOULCN '.

  Session.save_path Configure additional documentation to be translated from the php.ini configuration file

You can use "N;" [MODE;] /path "This pattern defines the path, and N is an integer that represents a subdirectory with an n-level depth, rather than keeping all data files in one directory.

[MODE;] Optionally, you must use the 8 number and the default 600 (=384) to represent the maximum number of session files saved per directory. [MODE;] Does not overwrite the umask of the process. PHP does not automatically create these folder structures. Can be created using the mod_files.sh script in the Ext/session directory. If the folder can be accessed by insecure users (such as the default "/tmp"), a security vulnerability will be created. Automatic garbage collection will fail when n>0, as detailed in the garbage collection section below.

If you have multiple virtual hosts on your server, it is recommended that you set up separate directories for each of the different virtual hosts.

This is the most basic session configuration is complete, you just save php.ini, and restart Apache, you can use the session function.

  additional session configuration instructions

  Session.save_handler = "Files"

Default file access to session data, if you want to use a custom processor to access session data, such as databases, with "user".

  session.use_cookies = 1

Whether to use cookies to save session SessionID on the client, default to adopt cookies

  session.use_only_cookies = 0

Whether to save session SessionID on the client only using cookies, this option allows the administrator to prevent the user from passing the ID through the URL, the default is 0, and if disabled, the client will prevent sessions from working if the cookie is disabled.

  session.name = "Phpsessid"

  Session ID name to use as cookie name

  Session.auto_start = 0

Whether to start the session automatically, the default does not start, we know that when using the session function, we basically in each PHP script head will be through the session_start () function to start the session, if you start this option, The session is automatically started in each script header, without the need for each script header to start session with the Session_Start () function, which is recommended to turn off this option and use the default value.

  session.cookie_lifetime = 0

Passing SessionID cookie validity (in seconds), 0 means only valid during browser opening.

  session.gc_probability = 1

  session.gc_divisor =

Defines the probability of starting the garbage collector each time the session is initialized. The formula is as follows: Session.gc_probability/session.gc_divisor, such as 1/100, means that there is a 1% probability to start the garbage collector, the more frequent access to the session page, the probability should be smaller. The recommended value is 1/1000~5000.

  session.gc_maxlifetime = 1440

Sets the lifetime of the saved session file, and after this parameter is set seconds, the saved data is considered ' garbage ' and cleaned up by the garbage collector. The criterion is the last time the data is accessed (for the FAT file system is the last time the data was refreshed). If multiple scripts share the same Session.save_path directory but session.gc_maxlifetime differ, the minimum values in all session.gc_maxlifetime directives are the same.

If you set up a subdirectory to store the session data file in the Session.save_path option, the garbage collector does not start automatically, and you must use your own shell scripts, cron entries, or other methods to perform garbage collection.

For example, set "Session.gc_maxlifetime=1440″ (24 minutes):

Cd/path/to/sessions; Find-cmin +24 | Xargs RM

These are some common session configuration options, and more session configuration options indicate that you can refer to the instructions in the php.ini file.

So far, In the php.ini configuration file to configure the session of the PHP tutorial is finished, through the steps above the practice and learning, the basic session function can be used, as for the performance of the session and other aspects of the need to be based on the server environment and needs of fine-tuning, this to their own experience.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.