使用cookie保存页面登录信息
1、数据库连接配置页面:connectvars.php
[url=style.css]</head>
<body>
<h3>Msimatch - Log In</h3>
<!--通过$_COOKIE['user_id']进行判断,如果用户未登录,则显示登录表单,让用户输入用户名和密码-->
<?php
if(empty($_COOKIE['user_id'])){
echo '<p class="error">'.$error_msg.'</p>';
?>
<!-- $_SERVER['PHP_SELF']代表用户提交表单时,调用自身php文件 -->
<form method = "post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<fieldset style="width:250px;">
<legend>Log In</legend>
<label for="username">Username:</label>
<!-- 如果用户已输过用户名,则回显用户名 -->
<input type="text" id="username" name="username"
value="<?php if(!empty($user_username)) echo $user_username; ?>" />
<br/>
<label for="password">Password:</label>
<input type="password" id="password" name="password"/>
</fieldset>
<br/>
<input type="submit" value="Log In" name="submit"/>
</form>
<?php
}
?>
</body>
</html>
效果图:
[img]http://files.jb51.net/upload/201203/20120308223023304.gif[/img]
3、登入页面:loged.php
<?php
//已登录页面,显示登录用户名
if(isset($_COOKIE['username'])){
echo 'You are Logged as '.$_COOKIE['username'].'<br/>';
//点击“Log Out”,则转到logOut.php页面进行cookie的注销
echo '<a href="logOut.php"> Log Out('.$_COOKIE['username'].')</a>';
}
/**在已登录页面中,可以利用用户的cookie如$_COOKIE['username']、
* $_COOKIE['user_id']对数据库进行查询,可以做好多好多事情*/
?>
效果图:
[img]http://files.jb51.net/upload/201203/20120308223023595.gif[/img]
4、注销cookie页面:logOut.php(注销后重定向到lonIn.php)
<?php
/**cookies注销页面*/
if(isset($_COOKIE['user_id'])){
//将各个cookie的到期时间设为过去的某个时间,使它们由系统删除,时间以秒为单位
setcookie('user_id','',time()-3600);
setcookie('username','',time()-3600);
}
//location首部使浏览器重定向到另一个页面
$home_url = 'logIn.php';
header('Location:'.$home_url);
?>