第一种:if($_POST['username']!=NULL && $_POST['password']!=NULL){...}
第二种:if(isset($_POST['username']) && isset($_POST['password'])){...}
<?php
if(isset($_POST['username']) && $_POST['username']!=null && $_POST['password']!=null){
$username=$_POST['username'];
$password=$_POST['password'];
$conn=mysqli_connect('localhost','root','root','test');
if(mysqli_connect_errno()){
die('数据库连接失败'.mysqli_connect_error());
}else{
mysqli_set_charset($conn,'utf8');
}
$sql="select * from user where username='$username'";
$res=mysqli_query($conn,$sql);
$row=mysqli_fetch_assoc($res);
if($row['password']==$password){
setcookie('username',$username,time()+60*60*24*30);
setcookie('password',$password,time()+60*60*24*30);
header('Location:welcome.php'."?username=$username");
}else{
echo '用户与密码不匹配';
exit;
}
}
if(isset($_COOKIE['username']) && $_COOKIE['username']!=null && $_COOKIE['password']!=null){
$username=$_COOKIE['username'];
$password=$_COOKIE['password'];
$conn=mysqli_connect('localhost','root','root','test');
if(mysqli_connect_errno()){
die('数据库连接失败'.mysqli_connect_error());
}else{
mysqli_set_charset($conn,'utf8');
}
$sql="select * from user where username=$username";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_assoc($result);
if($row['password']==$password){
header('Location:welcome.php'."?username=$username");
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>登陆界面</title>
</head>
<body>
<form action="" method='post'>
用户名:<input type="text" name='username' /><br/>
密码:<input type="password" name='password' /><br/>
<input type="submit" value='登录' />
</form>
</body>
</html>教程上经常是第一种,由于没有使用isset判断经常会出现Notice: Undefined index: 的问题
如果自己改用第二种写法就不会有问题了,但是会不会有漏洞;
子期不遇
大写的王
慕的地6079101
江户川秋风