php注册和登陆与数据库的链接
注册和登陆其实是从一个表中进行提取和写入数据
1.(1)先建立一个注册页面
|
1
2
3
4
5
6
7
8
9
|
<body> <h1>注册页面</h1> <form action="./zhucechuli.php" method="post"> //链接到的文件,就是登陆的处理页面 <div>用户名:<input type="text" name="uid"/></div> <div>密码:<input type="text" name="pwd"/></div> <div>姓名:<input type="text" name="nm"/></div> <div><input type="submit" value="注册" /></div> </form> </body> |
(2)创建注册处理页面(也就向数据库的一个表中写入数据)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?php$uid = $_POST["uid"]; //用的什么方法就用什么,这里注册中是用的post,所以这里用post$pwd = $_POST["pwd"];$nm = $_POST["nm"];<br>//造数据库$db = new MySQLi("localhost","root","123","test2");//写sql语句$sql = "insert into huiyuan values('{$uid}','{$pwd}','{$nm}')";//执行语句$r = $db->query($sql);//判断是否登陆成功if($r){ echo "注册成功!"; }else{ echo "注册失败!"; }?> |
2.登陆和注册差不多,(1)建立登陆页面
|
1
2
3
4
5
6
7
8
|
<body> <h1>登陆页面</h1> <form action="./dengluchuli.php" method="post"> <div>用户名:<input type="text" name="uid" /></div> <div>密码:<input type="password" name="pwd" /></div> <div><input type="submit" value="登陆" /></div> </form> </body> |
(2)登陆的处理页面(从一个表中提取数据)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?php$uid = $_POST["uid"];$pwd = $_POST["pwd"];//造数据库$db = new MySQLi("localhost","root","123","test2");//sql语句$sql = "select mima from huiyuan where yonghu='{$uid}'"; //用这个语句可以简单的避免用户名不对也可以登陆//执行sql语句$result = $db->query($sql);<br>//取值$attr = $result->fetch_row();<br>//判断if($attr[0]==$pwd && !empty($pwd)){ echo "登陆成功!"; }else{ echo "登录失败!"; }?> |
SQL注入攻击
1.过滤用户的输入
2.使用预处理语句
3.写代码的时候尽量避免
浙公网安备 33010602011771号