php登录页面cookie自动登录及验证
Posted iriliguo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php登录页面cookie自动登录及验证相关的知识,希望对你有一定的参考价值。
<?php
//cookie实现自动登录
error_reporting(0);// 关闭错误报告(浏览页面出现notice可用此法消除)
$user = $_POST[‘username‘];
$pwd = $_POST[‘password‘];
if ($user!=‘‘&&$pwd!=‘‘){
if($_POST[‘remmber‘]==1){
header("Location:http://localhost/homework/login.php");
//转到登录页面
}
setcookie("username",$user,time()+600);//time()+600指600秒后cookie失效
setcookie("password",$pwd,time()+600);
}
?>
<html>
<head>
<title>login</title>
</head>
<body>
<div id="wrapper">
<form name="login-form" class="login-form" action="login.php" method="post">
<div class="header">
<h1>Login Form</h1>
</div>
<div class="content">
<input name="username" type="text" class="input username" value="<?php echo $_COOKIE["username"]; ?>" />
<input name="password" type="password" class="input password" value="<?php echo $_COOKIE["password"]; ?>"/>
</div>
<div class="footer">
<input type="submit" name="submit" value="Login" class="button" />
</div>
</form>
</div>
</body>
</html>
<?php
if(isset($_POST["username"])){//isset()判断是否为空,不空才执行,0就是空
$username=$_POST["username"];
$password=$_POST["password"];
//$submit=$_POST["submit"];
$con = mysql_connect("localhost","root","root");
if(!$con){
die("db connection is wrong:".mysql_errno());
}
mysql_select_db("db_admin",$con);
//进行输入验证
$sql1 ="select * from register where name =‘".$username."‘";
$str = mysql_query($sql1);
mysql_close($con);
$buffer_account="";
$buffer_pwd="";
while($user = mysql_fetch_array($str)){
$buffer_account=$user[‘name‘];
$buffer_pwd=$user[‘pwd‘];
}
$value1=strcmp($username,$buffer_account);
$value2=strcmp($password,$buffer_pwd);
if($value1==0&&$value2==0){
echo "<script> window.location.href=‘http://localhost/homework/singers.php‘</script>";
}
else{
echo "<script>alert(‘you are wrong‘); window.location.href=‘http://localhost/homework/login.php‘</script>";
}
}
?>
<?php
$con = mysql_connect("localhost","root","root");
if(!$con){
die("db connection is wrong:".mysql_errno());
}
mysql_select_db("db_admin",$con);
//把数据库里面查询的数据保存到文件里面
$sql = "select * from register";
$result = mysql_query($sql);
$data="";
while($row = mysql_fetch_row($result)){
foreach($row as $v){
$data .=$v;
}
}
file_put_contents("abc.txt",$data);
?>
以上是关于php登录页面cookie自动登录及验证的主要内容,如果未能解决你的问题,请参考以下文章