Home >Backend Development >PHP Tutorial >Getting Started with PHP Session

Getting Started with PHP Session

零到壹度
零到壹度Original
2018-03-28 17:41:271926browse

This article mainly shares with you a session method for getting started with PHP. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor and take a look.

Function: Store a user's information, change user session settings, and can be used on all pages.

PHP Session variable

Open the program, make some changes, then save and exit. It's just a session. The server can know who is operating through the session.
Working mechanism: Create a uid for the visitor, store variables according to this uid, the uid is stored in a cookie, or URL transmission.

Use PHP Session

Save user information before the session and start the session
Note: The session_start() function must be located before the 100db36a723c770d327fc0aef2ce13b1 tag:

<?php
 session_start(); 
 ?><html><body></body></html>

Register the user session with the server to facilitate saving user information, and assign a uid to the user session

Storage Session variable

Store and obtain session using $ _SESSION

<?php
session_start();
// 存储 session 数据
if(isset($_SESSION[&#39;views&#39;]))
{  
  $_SESSION[&#39;views&#39;]=$_SESSION[&#39;views&#39;]+1;
}
else{  
  $_SESSION[&#39;views&#39;]=9999;
}
?>

<html>
<head>
<meta charset="utf-8">
</head>
<body>
<?php
// 检索 session 数据
echo "浏览量:". $_SESSION[&#39;views&#39;];
?>
</body
></html>

Output "Views: 9999"

Destroy Session

Which one to useunset() or session_destroy() Function destruction

<?php
session_start();
if(isset($_SESSION[&#39;views&#39;]))
{  
  //释放指定的 session 变量
    unset($_SESSION[&#39;views&#39;]);
}
//彻底销毁 sessionsession_destroy();
?>

Related recommendations:

##session principle and implementation sharing

session principle

Session working principle in PHP

The above is the detailed content of Getting Started with PHP Session. For more information, please follow other related articles on the PHP Chinese website!

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