注册和登录的代码怎么写php
4 回答
InfiniteLoop
TA贡献3条经验 获得超5个赞
注册页面
<!doctype html> <html> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>注册页面</title> </head> <body> <form action="reg.php" method="post"> <p> <label>用户名:</label> <input type="text" name="username" id="txt1" /> </p> <p> <label>密码:</label> <input type="password" name="password" id="pwd1" /> </p> <p> <label>确认密码:</label> <input type="password" id="pwd2" /> </p> <p> <input type="submit" value="注册" /> </p> </form> </body> </html>
php对提交过来的信息的处理
<?php //Author:avirtuous $con = mysql_connect("localhost","root","root") or die('Cloud not connect:'.mysql_error()); mysql_select_db("demo",$con); $un = $_POST['username']; $pwd = $_POST['password']; $result=mysql_query("INSERT INTO user (username,password) VALUES ($un,$pwd)"); if($result){ echo "注册成功"; } else { echo "注册失败"; } ?>
登陆页面
<!doctype html> <html> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>登陆页面</title> </head> <body> <form action="login.php" method="post"> <p> <label>用户名:</label> <input type="text" name="username" id="txt1" /> </p> <p> <label>密码:</label> <input type="password" name="password" id="pwd1" /> </p> <p> <input type="submit" value="登陆" /> </p> </form> </body> </html>
登陆页面的处理
<?php //Author:avirtuous $con = mysql_connect("localhost","root","root") or die('Cloud not connect:'.mysql_error()); mysql_select_db("demo",$con); $un = $_POST['username']; $pwd = $_POST['password']; $result=mysql_query("SELECT * from user where username=$un and password=$pwd"); if($result){ echo "登陆成功"; } else { echo "用户名或密码错误"; } ?>
这里只做简单的功能实现,放到实际中还欠缺许多处理
InfiniteLoop
TA贡献3条经验 获得超5个赞
不好意思 之前脑袋短路了 mysql_query()返回的是一个资源标示符,也就是说只要sql语句正确,无论查询到的结果如何 都会返回值,所以对登陆的php页面做了点修改
<?php $con = mysql_connect("localhost","root","root") or die('Cloud not connect:'.mysql_error()); mysql_select_db("demo",$con); $un = $_POST['username']; $pwd = $_POST['password']; $result=mysql_query("SELECT * from user where username=$un"); $row = mysql_fetch_array($result); if($row) { if($row['password'] == $pwd) { echo "登陆成功"; } else { echo "密码错误"; } } else { echo "用户名不存在"; } ?>
添加回答
举报
0/150
提交
取消