第一次更新(2018.8.3 20:53)
这篇主要写登录的
我写的登录主要有两种办法
一是同时比对用户名和密码
二是先比对用户名在比对密码
以下是第一种方法的代码:
$sql = "select * from ceshi1 where name='$name' and password='$password'";
$res = mysql_query($sql);
//var_dump($res);
$row = mysql_fetch_row($res);
//var_dump($row);
if($row){
echo "<script>alert('登录成功')</script>";
echo "<script>location.href='一楼.html';</script>";
}else{
echo "<script>alert('登录失败')</script>";
//echo "<script>history.go(-1);</script>"; //登录失败返回上一个页面
echo "<script>location.href='登录.html';</script>"; //登录失败,跳转到另外一个页面
}
先写出数据库查询语句
$sql = "select * from ceshi1 where name='$name' and password='$password'"
这段sql语句的作用是
查询所有项目从ceshi1的表里 当name等于用户输入的名字,当password等于用户输入的密码,或者说,把用户输入的名字赋值给name,把用户输入的密码赋值给password
$res = mysql_query($sql);
这语句的意思是发送sql语句并执行,如果sql语句为真返回1。否则返回0;
所以上面的sql语句的意思是从数据库中查询符合条件的数据,如果有,返回1,否则返回0;
这样就可以根据返回值来判断是否登录成功;
第二种方法
先查询姓名再查询密码
具体代码如下:
$sql = "select name from ceshi.ceshi1 where name = '$name'";
$res = mysql_query($sql);
//var_dump($res);
//$row = mysql_fetch_row($res);
var_dump($res);
if($res){
$sql_password = "select password from ceshi.ceshi1 where password = '$password'";
$res_password = mysql_query($sql_password);
$row_password = mysql_fetch_row($res_password);
if($row_password){
echo "<script>alert('登录成功')</script>";
echo "<script>location.href='一楼.html';</script>";
}
else{
echo "<script>alert('登录失败111')</script>";
echo "<script>location.href='登录.html';</script>"; //登录失败,跳转到另外一个页面
}
}
else
{
echo "<script>alert('登录失败2222')</script>";
echo "<script>location.href='登录.html';</script>"; //登录失败,跳转到另外一个页面
}
这两个办法都实现了登录,但是缺点很明显,那就是非常容易被攻击,明文密码没有安全性。