Home >Backend Development >PHP Tutorial >php session management
1. Cookie
1. How to get the value in cookie?
Example jumps to a.php through index.php to get the corresponding value jason with the key name;
index.php code:
<?php //设置cookie的键值对 setcookie('name','jason'); setcookie('mm','mark'); //跳转页面 header('Location:a.php');a.php code;
<?php //获取cookie的相应键对应的值 echo $_COOKIE['name'];in Firefox Browser execution result:
2. How to access cookies through javascript?
Example displays results through pop-up box
<?php //设置cookie的键值对 setcookie('name','jason'); setcookie('mm','mark'); ?> <meta charset="UTF-8"> <title>cookie知识点</title> <script> //用js获取cookie alert(document.cookie); </script>in index.php:
3. If the browser or user has disabled cookies, how to pass parameters between pages?
Example passes the value of b.php to c.php through URL parameters;
b.php code:
<?php header('Location:c.php?name=rose');c.php code:
<?php echo $_GET['name'];can obtain the corresponding value through $_GET The value of the key rose.
二.session:
1. Each time the browser is reopened, the server will assign a new session_id value to the client.
<?php //启用session session_start(); //访问session_id echo session_id();2. How to get the value corresponding to the session key?
Example jumps to a.php through index.php to display the value corresponding to the corresponding key of the session:
index.php code;
<?php //启用session session_start(); //设置session的键值对 $_SESSION['name']='aili'; //跳转页面 header('Location:a.php');a.php code:
<?php //启用session session_start(); //获取session相应键对应的值 if(isset($_SESSION['name'])){ echo $_SESSION['name']; }else{ echo 'no name found'; }The last page jump The successful transfer shows: aili
3. What should I do if I want to destroy the session? (ps application scenario: clear login status after timeout)
PHP provides session_destroy() to destroy the session.
Insert session_destroy() in index.php:
<?php //启用session session_start(); //设置session的键值对 $_SESSION['name']='aili'; //销毁session session_destroy(); //跳转页面 header('Location:a.php');and then perform the operation. After jumping to the page, the result is: no name found.
The above introduces the session management of PHP, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.