php中施用session

WBOY
Release: 2016-06-13 13:15:46
Original
823 people have browsed it

php中使用session

在登录页面新建session

session_start(); 
$_SESSION['username'] = $username;
Copy after login

?在需要验证的页面使用

//验证session
session_start();
//session中是否存在username并且session是否过期
if(!isset($_SESSION["username"])|| time()";
	echo '点击此处 <a href="login.html">登录</a> !<br>';
	exit;
}
Copy after login

?注销session

session_start();
/*** 删除所有的session变量..也可用unset($_SESSION[xxx])逐个删除。****/
$_SESSION = array();
/***删除sessin id.由于session默认是基于cookie的,所以使用setcookie删除包含session id的

cookie.***/
if (isset($_COOKIE[session_name()])) {
	setcookie(session_name(), '', time()-42000, '/');
}
// 最后彻底销毁session.
session_destroy();

//跳转到登陆页面
Header("HTTP/1.1 303 See Other"); 
Header("Location: login.html"); 
Copy after login

?ps:session_start();前不能有任何输出,必须加在页面的最顶部

不然会报 Warning:?session_start() [function.session-start]: Cannot send session cache limiter - headers?already sent?

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