注冊和登錄的代碼怎么寫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
提交
取消