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');
<?php //获取cookie的相应键对应的值 echo $_COOKIE['name'];
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>
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');
<?php echo $_GET['name'];
二.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();
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');
<?php //启用session session_start(); //获取session相应键对应的值 if(isset($_SESSION['name'])){ echo $_SESSION['name']; }else{ echo 'no name found'; }
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');
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.