Home  >  Article  >  Backend Development  >  PHP SESSION Literacy Chapter_PHP Tutorial

PHP SESSION Literacy Chapter_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:50:25766browse

In this article, you can learn some basics of how to use sessions. After reading this article, maybe you will learn the basic usage of session!

PHP Session Variables

When you run an application, you open it, make changes, and then close it. It's a lot like a session. The computer knows who you are. It knows when you start the application and when it terminates it. But on the Internet, there's a problem: the server doesn't know who you are and what you do, because HTTP addresses cannot maintain state.
PHP sessions solve this problem by storing user information on the server for subsequent use (such as user name, purchased items, etc.). However, session information is temporary and will be deleted after the user leaves the site. If you need to store information permanently, you can store the data in a database.
The working mechanism of Session is to create a unique id (UID) for each visitor and store variables based on this UID. The UID is stored in a cookie or transmitted through the URL (PS. In most cases we use COOKIE to save it).

Start PHP Session
When a php page is run, the default session is not started, and we need to start it manually. It's easy because you just need to call the following function!

session_start()


But this function needs to be called when the HTML code has not started to be output. This is worth noting! Otherwise, an error will be reported! Some students are curious as to why they have saved a value in the session but it disappears when they go to another page! It is very likely that the session_start() function is not called.



Store Session variables
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 also use or modify this variable at any time!


End Session
If we store the user's basic information in the session when the user logs in, we may want to destroy the session when the user logs out.

unset($_SESSION['username']);

Even more, you can call the following function to clear all session variables.

session_destroy();




All Rights Reserved by Zero Space

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478260.htmlTechArticleIn this article, you can know some basics of how to use session. After reading this article, maybe you will learn the basic usage of session! PHP Session Variables When you run an application...
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