Php session getting started

Source: Internet
Author: User
Tags session id php session php tutorial

1. What is session?
Session, which is called a session in Chinese, is a solution used to maintain the session connection status between the client and the server.
Due to its wide applicability, the php Tutorial, as a web development language, also fully supports the session mechanism.
For a web site, session refers to the time when a user browses a website, from entering the website to closing the browser.
We can see that session is actually a specific concept of time.
By using session, we can pass variables between different pages of the website context and use the session for user identity authentication,
Program Status record, the most common form is to use with cookies to save user login status and other functions. 2.php session usage
Each website visitor is assigned a unique identifier (session id), which can be stored in either
The url is either stored in the cookies of the client. Of course, you can also save the session to the database tutorial.
It is safer, but the efficiency will decrease.
The security of url-based transmission must be too poor. The php session mechanism is to save the session id in cookies by setting cookies.
Session id. The session file is generated on the server and associated with the user.
Session-related data is transmitted between pages. 3.php session function and usage
There are many session-related functions in php, but we usually use the following functions:
Session_start (), session_register (), session_unregister (),
Session_is_registered (), session_destroy function.
Session_start (): Enables the session mechanism and calls it at the very beginning of the program file that requires the session.
Session_register (): registers the session variable.
Session_unregister (): deletes session variables one by one)
Session_is_registered (): determines whether the session variable is registered.
Session_distroy (): destroys all session variables (destroys all session variables)

Pay attention to the following aspects:
1. The session_start () function must be executed at the beginning of the program, and no output content exists before it; otherwise
The "warning: cannot send session cookie-headers already
Sent "warning information similar to this.
2. The session_register () function is used to register related variables to be saved in the session. Its usage is as follows:
<? Php
$ Val = "session value ";
Session_register ("val ");
?>
Val is the name of the session variable to be registered. Do not add the "$" symbol during registration. Just enter the variable name.

3. The session_unregister () function is used exactly the same as the above function, but the function is opposite. The above function is registered
The session variable is used to delete the specified session variable.
4. The session_is_registered () function is used to determine whether the session variable is registered.
5. The session_destroy () function is used to destroy all session variables when the system logs out and exits,
It has no parameters. You can call it directly.
The following is a simple and complete example program:
<? Php
Session_start ();
$ Val = "session value ";
Session_register ("val ");

If (session_is_registered ("val ")){
Session_unregister ("val ");
 }
Else {
Echo "the val variable has not been registered ";
 }

Session_destroy ();
?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.