php注冊功能的實(shí)現(xiàn)方法:首先在已有的數(shù)據(jù)庫里創(chuàng)建user表;然后創(chuàng)建HTML注冊表單以及PHP注冊程序;最后創(chuàng)建登錄表單和登錄程序即可。
本文操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦
php注冊登錄系統(tǒng)簡化版
登錄注冊系統(tǒng)是日常上網(wǎng)最普通的操作,我設(shè)了一個(gè)分類一步步完善注冊登錄系統(tǒng),若哪里有誤,請見諒。
所用語言:php
數(shù)據(jù)庫 :mysql
本次實(shí)現(xiàn)功能:
1.用戶注冊
2.用戶登錄
主要文件:
完整代碼
1 sql 在已有的數(shù)據(jù)庫里創(chuàng)建user表,id,username,password三個(gè)字段
代碼如下:
create table user(id int(10) not null auto_increment,username varchar(30),password varchar(40),primary key(id));
2 connect.php 數(shù)據(jù)庫配置文件
<?php $server="localhost";//主機(jī) $db_username="";//你的數(shù)據(jù)庫用戶名 $db_password="";//你的數(shù)據(jù)庫密碼 $con = mysql_connect($server,$db_username,$db_password);//鏈接數(shù)據(jù)庫 if(!$con){ die("can't connect".mysql_error());//如果鏈接失敗輸出錯(cuò)誤 } mysql_select_db('test',$con);//選擇數(shù)據(jù)庫(我的是test) ?>
3 signup.html 注冊表單
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用戶注冊頁面</title> </head> <body> <form action="signup.php" method="post"> <p>用戶名:<input type="text" name="name"></p> <p>密 碼: <input type="text" name="password"></p> <p><input type="submit" name="submit" value="注冊"></p> </form> </body> </html>
4 signup.php 注冊程序
<?php header("Content-Type: text/html; charset=utf8"); if(!isset($_POST['submit'])){ exit("錯(cuò)誤執(zhí)行"); }//判斷是否有submit操作 $name=$_POST['name'];//post獲取表單里的name $password=$_POST['password'];//post獲取表單里的password include('connect.php');//鏈接數(shù)據(jù)庫 $q="insert into user(id,username,password) values (null,'$name','$password')";//向數(shù)據(jù)庫插入表單傳來的值的sql $reslut=mysql_query($q,$con);//執(zhí)行sql if (!$reslut){ die('Error: ' . mysql_error());//如果sql執(zhí)行失敗輸出錯(cuò)誤 }else{ echo "注冊成功";//成功輸出注冊成功 } mysql_close($con);//關(guān)閉數(shù)據(jù)庫 ?>
注冊流程完成,下面是用戶登錄【推薦學(xué)習(xí):PHP視頻教程】
5 login.html 登錄表單
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登陸</title> </head> <body> <form name="login" action="login.php" method="post"> <p>用戶名<input type=text name="name"></p> <p>密 碼<input type=password name="password"></p> <p><input type="submit" name="submit" value="登錄"></p> </form> </body> </html>
6 login.php 登錄程序
<?PHP header("Content-Type: text/html; charset=utf8"); if(!isset($_POST["submit"])){ exit("錯(cuò)誤執(zhí)行"); }//檢測是否有submit操作 include('connect.php');//鏈接數(shù)據(jù)庫 $name = $_POST['name'];//post獲得用戶名表單值 $passowrd = $_POST['password'];//post獲得用戶密碼單值 if ($name && $passowrd){//如果用戶名和密碼都不為空 $sql = "select * from user where username = '$name' and password='$passowrd'";//檢測數(shù)據(jù)庫是否有對應(yīng)的username和password的sql $result = mysql_query($sql);//執(zhí)行sql $rows=mysql_num_rows($result);//返回一個(gè)數(shù)值 if($rows){//0 false 1 true header("refresh:0;url=welcome.html");//如果成功跳轉(zhuǎn)至welcome.html頁面 exit; }else{ echo "用戶名或密碼錯(cuò)誤"; echo " <script> setTimeout(function(){window.location.href='login.html';},1000); </script> ";//如果錯(cuò)誤使用js 1秒后跳轉(zhuǎn)到登錄頁面重試; } }else{//如果用戶名或密碼有空 echo "表單填寫不完整"; echo " <script> setTimeout(function(){window.location.href='login.html';},1000); </script>"; //如果錯(cuò)誤使用js 1秒后跳轉(zhuǎn)到登錄頁面重試; } mysql_close();//關(guān)閉數(shù)據(jù)庫 ?>
7 welcome.html 登錄成功跳轉(zhuǎn)頁面
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登陸成功</title> </head> <body> 歡迎光臨 </body> </html>
至此一個(gè)簡單的完整的注冊登錄系統(tǒng)完成,代碼很簡單沒有考慮驗(yàn)證安全性健壯性,之后在進(jìn)行完善。