PHPSESSION literacy _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags php session
PHPSESSION literacy. In this article, you can know how to use the basics of session. After reading this article, you may have learned the basic usage of the session! PHPSession variables when you run a program in this article, you can know how to use the basics of session. After reading this article, you may have learned the basic usage of the session!

PHP Session variable

When you run an application, you open it, make some changes, and then close it. This is like a session. The computer knows who you are. It knows when to start the application and when to terminate it. However, there is a problem on the Internet: the server does not know who you are and what you are doing, because the HTTP address cannot be maintained.
By storing user information on the server for subsequent use, PHP session solves this problem (such as user name and product purchase ). However, the session information is temporary and will be deleted after the user leaves the website. If you need to store information permanently, you can store the data in the database.
The Session mechanism is to create a unique id (UID) for each visitor and store the variables based on the UID. The UID is stored in the cookie or transmitted through the URL (PS. In most cases, we use cookies to save it ).

Start PHP Session
When a php page is running, the default session is not started. we need to start it manually. This is easy because you only need to call the following functions!

Session_start ()


However, this function should be called when html code is not output. this is worth noting! Otherwise, an error is reported! Some people are wondering why they obviously saved a value in the session and disappeared on another page! It is very likely that the session_start () function is not called.



Store Session variables
The session in PHP is easy to use. it is actually an array variable $ _ SESSION. You can use the following statement to store session variables:

$ _ SESSION ['username'] = 'www .zeroplace.cn ';

On any other page, you can use or modify this variable at any time!


End Session
If we store basic user information in the session during user logon, we may want to destroy the session when the user exits.

Unset ($ _ SESSION ['username']);

You can call the following function to clear all session variables.

Session_destroy ();




Copyright of the 0th space

Bytes. After reading this article, you may have learned the basic usage of the session! PHP Session variable when you run a response...

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.