开始先写出登陆页面的html框架 将登陆的用户 密码 提交 等各种功能 写出来
然后用php实现功能正常
首先 判断是否提交
是则 判断用户 密码是否为空 不为空就接收输入的内容 传到魔术变量 用$_POST[]提取
然后将用户信息与数据库中信息进行比对
没有就输出 "error 如果没注册请注册" 有则将session传到数据库 并转到留言板页面
<?php
include 'sqldenglu.php';//写的数据库连接文件session_start();//开启session
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>留言板登陆</title>
</head>
<body class="vertical">//vertical是文本对齐
<center>
<?php
if(isset($_POST["submit"]))//isset()确认是否为空 是则输出ture 否则falese $_POST['submit'];是提取submit的内容;
{
if(empty($_POST["name"]))//empty()判断是否为空 ,空则输出为ture 反之输出为falese
{
$nameerror = "用户名不能为空";
}
else
{
$username = $_POST["name"];//将$_POST["name"]内容赋予$username;
}
if(empty($_POST['password']))
{
$passwderror = "密码不能为空";
}
else
{
$password = $_POST["password"];
}
}
if(isset($username,$password))//判断$username,$password是否为空
{
$sql = "select * from user where username = \"$username\" and passwd = \"$password\"";
// select * from user where username = \"$username\" and passwd = \"$password\"这是sql语句
//搜索 username passwd 一致的用户信息
$res = mysqli_query($con,$sql);//mysqli_query() mysqli_num_rows($res)是sql函数 ;
//mysqli_query()是获取执行结果 mysqli_num_rows($res)是将mysqli_query()的结果转化为数字;
if(mysqli_num_rows($res) > 0){
$user=mysqli_fetch_array($res);
//mysqli_fetch_array()是获取mysqli_query()的结果中的关联数组;
$_SESSION['uid']=$user['uid'];
//将session传递到服务器中
echo "<script>alert('dengluseccess')</script>";
//将登陆成功的信息做成弹窗
echo "<script> window.location.href=\"show.php\"</script>";
//随后跳到show.php页面
}else{
echo "<script>alert('error 如果没注册请注册')</script>";
}
}
?>
<h2>用户登陆</h2>
//二等标题大小的标题
<table border="0">//table 表格 border="0" 就是0像素的表格线 就是没有表格线<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
//form表单 method表示信息传递的方法 action表示信息传递的地方 <?php echo $_SERVER["PHP_SELF"]; ?>这是php中的魔术变量
<tr><th>用户名:</th>
<td><input type="text" name="name"></td>
<?php if (isset($nameerror)){ echo "$nameerror"; } ?>//判断$nameerror是否为空 是则输出
</tr>
<tr>
<th>密码:</th>
<td><input type="password" name="password" ></td>
<?php if (isset($passwderror)){ echo "$passwderror"; } ?>
</tr>
<tr>
<th colspan="2" align="center"><input type="checkbox" name="a[]" value="下次自动登陆">自动登陆
<input type="checkbox" name="a[]" value=" 记住密码">记住密码
</th>
</tr>
<tr>
<th align="center" colspan="2">
<input type="submit" value="提交" name="submit">
</th>
</tr>
<tr>
<th align="center" colspan="2">
<a href="忘记密码.html" target="blank">忘记密码</a>
<a href="zhuce.php" target="blank">注册</a>
</th>
</tr>
</form>
</table>
</center>
</body>
</html>