Phpsession usage _ PHP Tutorial

Source: Internet
Author: User
Phpsession usage. This article will introduce you to php beginners the basic usage of global variable session in php. For more information, see reference. Session is what Session is. This article will introduce you to php beginners the basic usage of global variable session in php. For more information, see.

What is a Session?

Session refers to a series of actions or information starting from beginning to end. for example, the whole process from going to a movie theater to going out of the cinema can be called a Session. Cookie stores data on the client computer, while Session stores non-quantitative variables on the server.

Each user connects to the website server and generates a Session. Therefore, the server must first be notified to use the Session function to store data. There are two ways to enable the Session function:

1. use the session_start () function to start a session. its structure is as follows:

Session_start ()
A function cannot contain parameters, and its return value is Boolean.

2. set the session. auto_start parameter in php. ini and set this parameter to 1. that is, the Session function is automatically enabled when a connection is established to the server.

After the Session is started, you must register the variable before using the Session variable. Otherwise, you must cancel the registration if you do not use a variable. Session_register (variable name) can register a Session variable; Session_unregister (variable name) can cancel registration of a Session variable, for example:

The code is as follows:

Session_start ();/* start the session variable */
Session_register ('user')/* register the user variable */
Session_unregister ('user')/* cancel the user variable */

Call session variables

Php session usage: You can use function $ _ SESSION [] to create a function variable in the following format:

The code is as follows:

$ _ SESSION ['$ string'] = $ str;

The parameter $ string is the name of the variable to be defined, and $ str is the value of the variable. Use the $ _ SESSION [$ string] format when calling.

Instance:

The code is as follows:

Session_start ();
$ _ SESSION ['name'] = "Jane ";
$ _ SESSION ['password'] = "888888 ";
Echo "username:". $ _ SESSION ['name']."
";
Echo "password:". $ _ SESSION ['password']."
";
?>

Session usage

This site has previously introduced some concepts and basic operations of the session. here we will introduce the methods used by the php session through examples.

Instance code:

The code is as follows:

Session_start ();/* start session */
$ _ SESSION ['name'] = "LIMing ";
$ _ SESSION ['password'] = "888888 ";
$ _ SESSION ['Time'] = time ();
Echo "transfer session
"
?>

Session. php code:

The code is as follows:
Session_start ();
Echo "username:". $ _ COOKIE ['name']."
";
Echo "password:". $ _ SESSION ['password']."
";
Echo "access time:". date ('Y m d H: I: S', $ _ SESSION ['Time'])."
";
?>

Obtains the session_id value.

Each session is identified by a specific id identified by session_id. You can use the session_id () function to view the current session_id value. its structure is as follows:

Session_id ()

Instance:

The code is as follows:

Session_start ();/* start session */
$ Id = session_id ();/* get the current session_id */
Echo "the current sessionID is:". $ id;
?>

Differences between cookie and session

Session and cookie are important session management applications that are used in many php development projects. They are used to create different values for different users, so that the results returned by different users are different.

So what is the difference between session cookies?

A cookie is created using a client. when a user opens a website in a browser, a cookie is created for the user. this process is executed by the user's computer. The website server only sends instructions. Therefore, if the cookie function is disabled, you cannot use cookies to implement related functions.

The session is created by the server. the whole process is executed by the server, and the user has no right to intervene.

If you want to save server resources, you can use cookies for session management. if you want all viewers to use session management, you should use session.

Beginners of idea will introduce the basic usage of global variable session in php. if you need to know about it, please refer. What is a Session? what is a Session...

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.