php session management

WBOY
Release: 2016-07-29 09:15:01
Original
1348 people have browsed it

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(&#39;name&#39;,&#39;jason&#39;);
setcookie(&#39;mm&#39;,&#39;mark&#39;);
//跳转页面
header(&#39;Location:a.php&#39;);
Copy after login
a.php code;

<?php //获取cookie的相应键对应的值
echo $_COOKIE[&#39;name&#39;];
Copy after login
in Firefox Browser execution result:

php session management

2. How to access cookies through javascript?

Example displays results through pop-up box

<?php //设置cookie的键值对
setcookie(&#39;name&#39;,&#39;jason&#39;);
setcookie(&#39;mm&#39;,&#39;mark&#39;);
?>



    <meta charset="UTF-8">
    <title>cookie知识点</title>
    <script>
        //用js获取cookie
        alert(document.cookie);
    </script>



Copy after login
in index.php:

php session management

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(&#39;Location:c.php?name=rose&#39;);
Copy after login
c.php code:

<?php echo $_GET[&#39;name&#39;];
Copy after login
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();
Copy after login
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[&#39;name&#39;]=&#39;aili&#39;;
//跳转页面
header(&#39;Location:a.php&#39;);
Copy after login
a.php code:

<?php //启用session
session_start();
//获取session相应键对应的值
if(isset($_SESSION[&#39;name&#39;])){
    echo $_SESSION[&#39;name&#39;];
}else{
    echo &#39;no name found&#39;;
}
Copy after login
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[&#39;name&#39;]=&#39;aili&#39;;
//销毁session
session_destroy();
//跳转页面
header(&#39;Location:a.php&#39;);
Copy after login
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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template