Home  >  Article  >  Backend Development  >  Detailed introduction to the usage of session in php

Detailed introduction to the usage of session in php

王林
王林Original
2019-09-02 16:08:266168browse

Detailed introduction to the usage of session in php

Session in PHP uses client cookies by default. When the client's cookies are disabled, it will automatically be passed through Query_String.

Php has a total of 11 functions for processing sessions. Let’s introduce in detail a few functions that will be used.

1. session_start

Function: Start a session or return an existing session.

Function prototype: boolean session_start(void);

Return value: Boolean value

Function description: This function has no parameters, and the return value is true. It is best to put this function first, and there must be no output before it, otherwise an alarm will be issued, such as: Warning: Cannot send session cache limiter - headers already sent (output started at /usr/local/apache/htdocs/cga /member/1.php:2) in /usr/local/apache/htdocs/cga/member/1.php on line 3

2、 session_register

Function function: Register a new variable as a session variable

Function prototype: boolean session_register(string name);

Return value: Boolean value.

Function description: This function adds a variable to the current SESSION in the global variable. The parameter name is the name of the variable you want to add. If successful, the logical value true is returned. You can use the form of $_SESSION[name] or $HTTP_SESSION_VARS[name] to get or assign values ​​

3. session_is_registered

Function: Check whether the variable is registered as Session variables.

Function prototype: boobean session_is_registered(string name);

Return value: Boolean value

Function description: This function can check whether the specified session already exists in the current session Variable registration, the parameter name is the name of the variable to be checked. If successful, the logical value true is returned.

4. session_unregister

Function: Delete registered variables.

Function prototype: boolean session_session_unregister(string name);

Return value: Boolean value

Function description: This function deletes variables in global variables in the current session . The parameter name is the name of the variable to be deleted, and returns true if successful.

5. Session_destroy

Function: End the current session and clear all resources in the session .

Function prototype: boolean session destroy(void);

Return value: Boolean value.

Function description: This function ends the current session. This function has no parameters, and the return value is true.

The functions introduced above will be used later, but there are also some functions related to the session. Also introduce:

6. session_encode

Function: sesssion information encoding

Function prototype: string session_encode(void);

Return value: string

Function description: The returned string contains the name and value of each variable in the global variable, in the form: a|s:12:"it is a test";c| s:4:"lala"; a is the variable name s:12 represents the value of variable a "it is a test. The length is 12. The variables are separated by semicolons ";".

7, session_decode
Function function: decoding session information
Function prototype: boolean session_decode (string data)
Return value: Boolean value
Function description: This function can decode session information and return if successful Logical value true

8, session_name

Function function: access the current session name

Function prototype: boolean session_name(string [name]);

Return value: string

Function description: This function can obtain or reset the name of the current session. If there is no parameter name, it means getting the current session name. Adding parameters means changing the session name. The name is set to the parameter name

9, session_id

Function function: access the current session identification number

Function prototype: boolean session_id(string [id ]);

Return value: string

Function description: This function can obtain or reset the identification number of the currently stored session. If there is no parameter id, it means only obtaining the identification number of the current session. , adding parameters means setting the session identification number to the newly specified id

10. session_unset

Function: Delete all registered variables.

Function prototype: void session_unset (void)

Return value: Boolean value

Function description: This function is different from Session_destroy in that it does not end the session. Just like using the function session_unregister to log out all session variables one by one.

The above only introduces some commonly used usages of session, and there are many more usages. For more related content, please visit the PHP Chinese website: PHP Video Tutorial

Related topic recommendations: php session (including pictures, texts, videos, cases)

The above is the detailed content of Detailed introduction to the usage of session in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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