PHP實現一個簡陋的註冊登入頁面
PHP菜鳥
今天來水一篇沒有**用的 /滑稽臉,程式碼簡陋臃腫考慮不全,各位大佬輕噴,還望不吝賜教。
首先考慮了一下需要至少四個頁面: register.html
、 register.php
、 login.html
、 login.php
。
register.html
是這麼寫的:
<!DOCTYPE html> <html> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <head> <title>註冊介面</title> </head> <body> <form action="register.php" method="post"> 使用者名稱:<input type="text" name="name"></input> <br /> 密碼:<input type="password" name="password"></input> <br /> <input type="submit" value="註冊"></input> </form> </body> </html>
register.php
是這麼寫的:
<?php header("Content-type:text/html;charset=utf-8"); $conn=new mysqli('localhost','wy','000000','test'); if ($conn->connect_error){ die("伺服器連線失敗!"); } $name=$_POST["name"]; $password=$_POST["password"]; $sql="insert into new_info values('$name',$password)"; $res=$conn->query($sql); if(!$res){ echo "註冊失敗!"; }else{ if($conn->affected_rows>0){ sleep(2); header("Location:login.html"); exit; }else{ echo "註冊失敗"; } } $conn->close(); ?>
login.html
是這麼寫的:
<!DOCTYPE html> <html> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <head> <title>登入介面</title> </head> <body> <p>註冊成功,請登入!</p> <form action="login.php" method="post"> 使用者名稱:<input type="text" name="name"></input> <br /> 密碼:<input type="password" name="password"></input> <br /> <input type="submit" value="登入"></input> </form> </body> </html>
login.php
是這麼寫的:
<?php header("Content-type:text/html;charset=utf-8"); $conn=new mysqli('localhost','wy','000000','test'); if ($conn->connect_error){ die("伺服器連線失敗!"); } $name=$_POST["name"]; $password=$_POST["password"]; $sql_name="select name from new_info where name='$name'"; $res_sql=$conn->query($sql_name); if($conn->affected_rows==0){ die("賬號或密碼有誤"); }else{ $sql_pass="select password from new_info where name='$name'"; $res_pass=$conn->query($sql_pass); $row_pass=$res_pass->fetch_row(); if($row_pass[0]==$password){ echo "登入成功!"; }else{ echo "賬號或密碼有誤"; } } $conn->close(); ?>
然後來看一下效果:


看一下資料庫:

可以看到已經將資料寫入資料庫。
接著來登入試一下:


換個錯誤密碼試一下:


#### 2018-09-04 問題修正:
1.使用者密碼加密
2.資料庫編碼問題
1.使用者密碼加密
register.php
頁面:
<?php header("Content-type:text/html;charset=utf-8"); $conn=new mysqli('192.168.134.128','root','123456','test'); if ($conn->connect_error){ die("伺服器連線失敗!"); } $name=$_POST["name"]; $password=$_POST["password"]; $password=md5($password);//將使用者輸入的密碼進行md5加密 $sql="insert into test values('$name','$password')"; $res=$conn->query($sql); if(!$res){ echo "註冊失敗!"; }else{ if($conn->affected_rows>0){ sleep(2); header("Location:login.html"); }else{ echo "註冊失敗"; } } $conn->close(); ?>
login.php
頁面:
<?php header("Content-type:text/html;charset=utf-8"); $conn=new mysqli('192.168.134.128','root','123456','test'); if ($conn->connect_error){ die("伺服器連線失敗!"); } $name=$_POST["name"]; $password=$_POST["password"]; $password=md5($password);//對使用者輸入的密碼進行md5加密 $sql_name="select name from test where name='$name'"; $res_sql=$conn->query($sql_name); if($conn->affected_rows==0){ die("賬號或密碼有誤!"); }else{ $sql_pass="select password from test where name='$name'"; $res_pass=$conn->query($sql_pass); $row_pass=$res_pass->fetch_row(); if($row_pass[0]==$password){//將使用者輸入的加密密碼與資料庫密碼進行對比 echo "登入成功!"; }else{ echo "賬號或密碼有誤"; } } $conn->close(); ?>
2.資料庫編碼問題
在資料庫執行 set names utf8
命令,將資料庫編碼改為 utf8
。
這樣就可以使用中文名註冊登入。
#### 2018-09-06 問題修正:
使用者註冊或者登入時輸入為空的問題
register.php
頁面:
<?php header("Content-type:text/html;charset=utf-8"); $conn=new mysqli('192.168.134.128','root','123456','test'); if ($conn->connect_error){ die("伺服器連線失敗!"); } $name=$_POST["name"]; $password=$_POST["password"]; if(empty($name) || empty($password)){//判斷註冊時賬號或密碼是否為空 die('賬號或密碼不能為空!'); }else{ $password=md5($password); $sql="insert into test values('$name','$password')"; $res=$conn->query($sql); if(!$res){ echo "註冊失敗!"; }else{ if($conn->affected_rows>0){ sleep(2); header("Location:login.html"); }else{ echo "註冊失敗"; } } $conn->close(); } ?>
login.php
頁面:
<?php header("Content-type:text/html;charset=utf-8"); $conn=new mysqli('192.168.134.128','root','123456','test'); if ($conn->connect_error){ die("伺服器連線失敗!"); } $name=$_POST["name"]; $password=$_POST["password"]; if(empty($name) || empty($password)){//判斷登陸時賬號或密碼是否為空 die('賬號或密碼不能為空!'); }else{ $password=md5($password); //var_dump($password); $sql_name="select name from test where name='$name'"; $res_sql=$conn->query($sql_name); if($conn->affected_rows==0){ die("賬號或密碼有誤!"); }else{ $sql_pass="select password from test where name='$name'"; $res_pass=$conn->query($sql_pass); $row_pass=$res_pass->fetch_row(); if($row_pass[0]==$password){ echo "登入成功!"; }else{ echo "賬號或密碼有誤"; } } $conn->close(); } ?>
程式碼臃腫,還望見諒。