PHP Session Learning Note _php Tutorial

Source: Internet
Author: User
Session refers to the time that a user has spent browsing a website, from entering the website to the browser closing, which is the time it takes for the user to browse the site. From the definition above, we can see that the session is actually a specific time concept.

When the session is used in PHP, the data in the session is stored on the server, and the client's information is identified and extracted through the SessionID from the client.

One instance of yourself

code as follows copy code

"!--? php
session_start ();// To start the session, you must start the
$_session[' name ']= "Fangshan" in sessions;//Set the Session,name value to Fangshan
?
Next

(Save as session.php)

!--? php
session_start ()//Start session
if (isset ($_session[' name '))//determine existence
{
Echo $_session[' name '];
Echo "del session";
}
Else
{
echo "Session had not been setted";
}
?

(save above as session2.php)

!--? php
session_start ();//Start session
Unset ($_session[' name ");//unregister Session variable
Session_destroy (); /clear Session ID
Header ("Location:session2.php");//Jump back to previous page
?

(Save as deletesession.php above)


Common Operations for session:

Session writes, reads, registers, and deletes.

The beginning of the session

The function that marks the beginning of session use is the Session_start,session_start function used to initialize the session variable. The syntax is as follows:

Session_Start ();

The return value is true.

Session Write and read

In PHP, the use of the session is done through the invocation and reading of the predefined array $_session.

In the page of the website, the $_session array is assigned on the registration page, and the $_session array is read on the other pages.

Register the session in the page, for example:

The code is as follows Copy Code
Session_Start ();
$_session[' keyword ']= "PHP";
?>

A session on another page, for example:

The code is as follows Copy Code
Session_Start ();
echo $_session[' keyword '];
?>

Run in sequence and the result is:

Php

Session Detection and logoff

Do you remember the isset and unset functions? These two functions implement session detection and logoff respectively.

The ISST function is used to detect if the session is already present and the syntax is as follows:

BOOL Isset ($_session[' session_name ')

For example:

The code is as follows Copy Code

Session_Start ();
if (isset ($_session[' keyword '))
echo $_session[' keyword '];
Else
echo "www.bKjia.c0m";
?>

Results:

Php

The Usset function is used to unregister a session variable that has already been established. The syntax is as follows:

unset ($_session[' session_name '))

For example:

The code is as follows Copy Code

Session_Start ();
unset ($_session[' keyword ');
if (isset ($_session[' keyword '))
echo $_session[' keyword '];
Else
echo "www.bKjia.c0m";
?>

Results:

www.bKjia.c0m


There are 10 functions in the PHP processing session, and we'll go through a few more functions.

1, Session_Start

function function: Start a session or return a session that already exists.
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 better to put this function first, and before it can have no output, otherwise it will alarm, such as: Warning:cannot send session cache Limiter-headers already sent (output started At/us R/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: A Boolean value.
Function Description: This function is to add a variable to the current session in the global variable, the parameter name is the name of the variable you want to add, and success returns the logical value TRUE. You can use $_session[name] or $http_session_vars[name] to take the form of a value or assignment.
3, session_is_registered


function function: Check if the variable is registered as a session variable.
Function prototype: Boobean session_is_registered (string name);
Return Value: Boolean value
Function Description: This function checks to see if a specified variable has been registered in the current session, and the parameter name is the variable name to check. Success returns the logical value TRUE.
4, Session_unregister


function function: Delete the registered variable.
Function prototype: Boolean Session_session_unregister (string name);
Return Value: Boolean value
Function Description: This function removes variables from global variables in the current session. The parameter name is the name of the variable you want to delete, and success returns TRUE.
5, Session_destroy


function function: Ends the current session and empties all resources in the session.
Function Prototype: Boolean session destroy (void);
Return value: A Boolean value.
Function Description: This function ends the current session, this function has no parameters and the return value is True
The functions described above will be used, but there are also some functions about the session:
6, Session_encode


function function: sesssion information encoding
Function prototype: string session_encode (void);
return value: String
Function Description: The returned string contains the names and values of the variables in the global variable, in the form of: 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 length is a semicolon between 12 variables"; Separated.
7, Session_decode


function function: sesssion information decoding
Function prototype: Boolean Session_decode (String data)
Return Value: Boolean value
Function Description: This function can decode session information, and success returns 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 get or reset the name of the current session. If no parameter name is given, the current session name is obtained, plus the parameter indicates that the session 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 get or reset the identification number of the session currently stored. If no parameter ID means only the identification number of the current session, plus the parameter indicates that the session's identification number is set to the newly specified ID
10, Session_unset


function function: Delete all registered variables.
Function prototype: void Session_unset (void)
Return Value: Boolean value
Function Description: Unlike Session_destroy, this function does not end the session. It's like using the function Session_unregister to unregister all session variables individually.

http://www.bkjia.com/PHPjc/632662.html www.bkjia.com true http://www.bkjia.com/PHPjc/632662.html techarticle Session refers to the time that a user has spent browsing a website, from entering the website to the browser closing, which is the time it takes for the user to browse the site. From the above definition ...

  • 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.