Session control in php_PHP tutorial

WBOY
Release: 2016-07-13 10:02:31
Original
965 people have browsed it

php session session control

When the browser turns off cookie data, the website will not be able to use cookie transfer, but the URL parameter transfer can still be carried out (session). In fact, the overall session control of writing PHP is the same as the cookie session control

First create the php file to be used in writing

Session control in php_PHP tutorial

This step is the same as cookie. In fact, the session can also be passed through cookies. Based on the cookie, open the session at the beginning: session_start()

<!--?php

	session_start();
    //判断:如果没登录自动跳转到登录页面
    if(!$_SESSION[isLogin5]){
        header(Location:login.php);
    }</pre-->

下面要注意的是,login.php 的跳转页面不能使用header 而只能通过 javascript 进行跳转
Copy after login

//跳转界面
echo &#39;<script>&#39;;|r|
echo location=&#39;index.php&#39;;|r|
echo &#39;</script>&#39;;
Copy after login

Then change $_COOKIE[ ] to $_SESSION[ ]

This is how session is passed through cookies. The following mainly explains how to pass url parameters

The first : Pass parameters through sid, that is, add "?sid="

after the link or form

This method can also use the PHPSESSID in the configuration file to replace the sid and achieve the same effect

login.php

Copy after login
Copy after login
The php part of login.php has also been slightly modified.

<!--?php
	
	session_start();
	echo session_id().<br-->;  //跳转页面不能不是header

    if(isset($_POST[sub])){
    	include conn.inc.php;

    	$sql=select id from users where name=&#39;{$_POST[name]}&#39; and password=&#39;.md5($_POST[password]).&#39;;

    	$result=$mysqli->query($sql);

    	//保存数据
    	if($result->num_rows > 0){
    		$row=$result->fetch_assoc();
    		
    		$_SESSION[username]=$_POST[name];
    		$_SESSION[uid]=$_POST[uid];
    		$_SESSION[isLogin5]=1;

    		//跳转界面
    		echo &#39;<script>&#39;;|r|
    		echo location=&#39;index.php?sid=.session_id().&#39;;   //将session_id() 调过来|r|
    		echo &#39;</script>&#39;;
    	}
    	echo 用户名密码有误;
    }
?>
Copy after login
For other pages, just add “?sid=” after the link.

The logout process is not like a cookie, it has four steps: open, clear, delete and completely destroy

    //开启session
    session_start();

    //情况session值
    $_SESSION=array();

    //删除客户端的在cookie中的sessionid
    if(isset($_COOKIE[session_name()])){
        setCookie(session_name(),&#39;&#39;,time()-3600,&#39;/&#39;); //一定要写上第四个参数(路径)
    }

    //彻底销毁session
    session_destroy();

Copy after login

The second type does not need to be set to automatically choose whether to use cookies or sessions for delivery based on whether the browser has turned on the cookie data function.

a, links or forms are followed by "?". This is similar to passing through sid, but SID is a constant

index.php:

    >第二页

    >第三页

    >退出
Copy after login

login.php:
   		//跳转界面
    		echo &#39;<script>&#39;;|r|
    		echo location=&#39;index.php?.SID.&#39;;   //SID 常量如果开启cookie则使用cookie,如果没开启就用session|r|
    		echo &#39;</script>&#39;;
Copy after login

Copy after login
Copy after login
method=post>

User login

 
用户名
密码

b, modify the php.ini configuration file

The code is basically the same as that passed by the cookie, except that the session needs to be opened at the beginning: session_start();

Method: Change the value of session.use_trans_sid in the configuration file to 1
Function: Add the form of PHPSESSID to all links by default

Session control in php_PHP tutorial

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/970254.htmlTechArticlephp session control When the browser turns off cookie data, the website will not be able to use cookie delivery, and the url Parameter transfer can still be carried out (session). In fact, the se...
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