像很多网站首页一样,如discuz论坛或百度首页,没登录前显示登录输入框或登录按钮,登录后返回首页,但不显示登录框了 ,而显示用户名?未登录前:
登录后:
我写的两个文件,求大神解答
index.html:
<!DOCTYPE html>
<html>
<head>
<title>登录页面</title>
<meta charset="utf-8">
</head>
<body>
<form method="post" action="doaction.php">
<table>
<tr>
<td align="right">用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td align="right">密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" name="submit" value=" 登 录 "></td>
<td>没有账号?<a href="reg.html">立即注册</a></td>
</tr>
</table>
</form>
</body>
</html>doaction.php:
<?php
header("Content-type:text/html;charset=utf-8");$username = $_POST['username'];
$password = $_POST['password'];
if(empty($username) || empty($password)){
echo "<script>alert('用户名或密码不能为空!'); history.go(-1);</script>";
}else{
include "conn.php";
$sql = "SELECT username, password FROM user WHERE username='$username' && password='$password'";
$res = $conn->query($sql);
$no = $res->num_rows;
if(!$no){
echo "<script>alert('用户名或密码错误'); history.go(-1);</script>";
}else{
echo "<script>alert('登录成功!'); location.href='index.html';</script>";
}
}