session会话

开启会话


session_start();

储存session 变量


 $_SESSION['username'] = 'hello kitty';
 echo $_SESSION['username'];  //输出 hello kitty

SESSION的应用

----------------index.php页面--------------

	
	//index.php页面,检查是否登录,如果没有登录跳转至登录页面,如果登录成功,显示欢迎您,某某
	
	session_start();
 	if(!isset($_SESSION['is_login'])){
		header('Location:login.php');
	}else{
		echo '欢迎您,'.$_SESSION['username'].'<a href="./logout.php">退出</a>';
	}
 
	
 
 

--------------login.php页面-------------

	
	//php部分
	
	session_start();
	if($_POST['username']==='dongsongshan' && $_POST['password']==='123456'){
		$_SESSION['is_login'] = true;
		$_SESSION['username'] = dongsongshan;
		header('Location:index.php')
	}



	//html部分
	
	<form action='./' mothed='post'>
	<input type='text' name='username'>
	<input type='password' name='password'>
	</form>

 

-----logout.php页面-----

	
	session_start();
	session_destroy()
	$SESSION = array();
	header('Location:index.php')

 

12 Jun 2016