您的位置:首页 > 编程语言 > PHP开发

java 到 php入门:cookie保存登录名和密码例子

2013-12-01 01:26 513 查看
登录界面:logon.php

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<?php
//用户是否保持用户名和密码
if(isset($_COOKIE['username'])){
echo "<script language='javascript'>";
echo "location.href='login_success.html'"; //重定向url
echo "</script>";
}
?>
<body>
<form action="deal_cookie.php" method="post">
用户名:<input type="text" name="username"/><br>
密    码:<input type="password" name="password"/><br>
<input type="checkbox" name="save" id="save"/><label for="save">7天内自动登录</label><br>
<input type="submit" name="login" value="提交"/>
</form>
</body>
</html>


cookie处理:deal_cookie.php

<?php
//设置编码
header('Content-Type:text/html;charset=utf-8');
// 判断用户是否提交
if(isset($_POST['login'])){
//获取用户名和密码
$username = $_POST['username'];
$password = $_POST['password'];

if($username !="" && $password != ""){
// 用户是否保存登录名
if(isset($_POST['save'])){
// 创建cookie,保存7天
setcookie('username',$username,time()+(7*24*60*60));
setcookie('password',$password,time()+(7*24*60*60));
}
// 重定向用户
header('Location:login_success.html');
}else{
echo "<script type='text/javascript'>alert('用户名和密码不能为空');history.back;</script>";
}
}
?>
成功登录:login_success.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录成功</title>
</head>
<body>
<h1>登录成功</h1>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java php 登录cookie