幾乎大部分的網站都有註冊帳號,登陸系統,這是很是須要的一個模塊。
本人此次的案例就是作一個註冊帳號,登陸帳號,登錄後才能夠查看某些資源的功能,其實就是簡單的php操做數據庫。javascript
首先講註冊帳號。
註冊帳號的邏輯是,先有個表單,輸入帳號和密碼,而後經過post方式提交給php頁面處理(驗證表單,例如是否爲空,數據庫是否存在同樣的數據,是否爲中文、英文、數字等),若沒有問題,就插入數據庫,如有問題就從新返回填寫,除了輸入帳號密碼,我還特別加了一個邀請碼,須要輸入正確的邀請碼才能夠註冊帳號,若是不想要邀請碼這個,能夠自行去掉,只是我本身的項目須要用這個。
下面是註冊表單:
register.phpphp
<!DOCTYPE html> <html> <head> <script type="text/javascript" src="http://tajs.qq.com/stats?sId=56441686" charset="UTF-8"></script> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" /> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <meta name="format-detection" content="telephone=no"> <title>裏客雲 - 註冊</title> <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link rel="stylesheet" type="text/css" href="likeyun.css"> </head> <body> <form role="form" action="regcheck.php" method="post" name="form1"> <div class="form-group"> <div class="alert alert-danger">你須要註冊後,才能夠登陸!</div> <input type="text" class="form-control" id="name" name="username" placeholder="請輸入帳號"> <input type="password" class="form-control" id="name" name="password" placeholder="請輸入密碼"> <input type="text" class="form-control" id="name" name="yaoqing" placeholder="請輸入邀請碼"> <input type="Submit" name="Submit" class="btn btn-default" onclick="check()" value="註冊"/> <p>沒有邀請碼?<a href="yaoqingma.html">當即獲取</a></p> </div> </form> <script> function check(){ var username=document.form1.name; var checkinput=document.form1.checknameinput; for(i=0;i<document.getElementsByName("username")[0].value.length;i++){ var c = document.getElementsByName("username")[0].value.substr(i,1); var ts = escape(c); if(ts.substring(0,2) == "%u"){ alert("不支持中文帳號"); document.getElementsByName("username")[0].value = ""; document.getElementsByName("username")[0].focus(); } } } </script> </body> </html>
上面表單能夠看出來表單提交後直接給regcheck.php進行處理,驗證。css
regcheck.phphtml
<?php header("Content-type:text/html;charset=utf-8"); if(isset($_POST["Submit"]) && $_POST["Submit"] == "註冊") { $user = $_POST["username"]; $psw = $_POST["password"]; $yqm = $_POST["yaoqing"]; if($user == "" || $psw == "" || $yqm == "") { echo "<script>alert('請不要留空!'); history.go(-1);</script>"; } else { if($yqm == "LiKeYuNbA2017") //邀請碼 { mysql_connect("數據庫地址","數據庫帳號","數據庫密碼"); //鏈接數據庫 mysql_select_db("數據庫名"); //選擇數據庫 mysql_query("SET NAMES 'utf8'");//設定字符集 $sql = "select username from 表名 where username = '$_POST[username]'"; //SQL語句 $result = mysql_query($sql); //執行SQL語句 $num = mysql_num_rows($result); //統計執行結果影響的行數 if($num) //若是已經存在該用戶 { echo "<script>alert('用戶名已存在'); history.go(-1);</script>"; } else //不存在當前註冊用戶名稱 { $sql_insert = "insert into user (username,password,phone,address) values('$_POST[username]','$_POST[password]','','')"; $res_insert = mysql_query($sql_insert); //$num_insert = mysql_num_rows($res_insert); if($res_insert) { echo "<script>alert('註冊成功'); history.go(-1);</script>"; } else { echo "<script>alert('系統繁忙,請稍候!'); history.go(-1);</script>"; } } } else { echo "<script>alert('邀請碼不正確'); history.go(-1);</script>"; } } } else { echo "<script>alert('註冊失敗!'); history.go(-1);</script>"; } ?>
上面就是整個註冊的過程了。java
接着就是登陸了。
登陸就是經過php進行數據庫字段對比驗證,若存在該字段則登陸成功,不存在則沒法登錄。mysql
登陸表單
login.phpjquery
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" /> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <script type="text/javascript" src="http://tajs.qq.com/stats?sId=56441686" charset="UTF-8"></script> <meta name="format-detection" content="telephone=no"> <title>裏客雲 - 登陸</title> <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link rel="stylesheet" type="text/css" href="likeyun.css"> </head> <body> <form role="form" action="logincheck.php" method="post"> <div class="form-group"> <div class="alert alert-danger">你須要登錄後,才能夠獲取本站資源!</div> <input type="text" class="form-control" id="name" name="username" placeholder="請輸入帳號"> <input type="password" class="form-control" id="name" name="password" placeholder="請輸入密碼"> <input type="submit" name="submit" class="btn btn-default" value="登錄" onclick="fn()"/> <p>沒有帳號?<a href="register.php">當即註冊</a></p><br/> </div> </form> </body> </html>
登陸處理頁面。在登陸過程,會經過session_start()在瀏覽器儲存登陸行爲。
logincheck.phpweb
<?php //頁面編碼 header("Content-type:text/html;charset=utf-8"); //隱藏報錯信息 error_reporting(E_ALL^E_NOTICE^E_WARNING); //儲存登陸行爲 session_start(); //直接把用戶名賦給echo $_SESSION['echo']="$_POST[username]"; if(isset($_POST["submit"]) && $_POST["submit"] == "登錄") { $user = $_POST["username"]; $psw = $_POST["password"]; if($user == "" || $psw == "") { echo "<script>alert('用戶名或密碼不能爲空'); history.go(-1);</script>"; } else { mysql_connect("數據庫地址","帳號","密碼"); //鏈接數據庫 mysql_select_db("數據庫名"); //選擇數據庫 mysql_query("SET NAMES 'utf8'");//設定字符集 $sql = "select username,password from 表名 where username = '$_POST[username]' and password = '$_POST[password]'"; $result = mysql_query($sql); $num = mysql_num_rows($result); if($num) { $row = mysql_fetch_array($result); //驗證經過後跳轉 echo "<script>window.location.href='index.php';</script>"; } else { echo "<script>alert('用戶名或密碼不正確!');history.go(-1);</script>"; } } } else { echo "<script>alert('登陸失敗'); history.go(-1);</script>"; } ?>
這樣就完成了登陸。sql
下面就看看如何實現登陸後才能夠查看某些內容,不少網站都這樣作,要登陸後才能夠查看或者下載一些資源的。數據庫
例如index.php是該網站首頁,首頁有一些內容,未登陸以前是顯示的,有一些內容須要登陸後才能夠顯示。
<?php header("Content-type:text/html;charset=utf-8"); session_start(); if(isset($_SESSION['echo'])){ //若是已經登錄了,那麼就輸出 echo "你已經登陸,該網站的網址是:https://segmentfault.com/"; }else{ //若是還沒登陸,那麼就輸出 echo "你還沒登陸,不能夠查看網址。"; //強制中斷程序的執行 exit(); } ?>
就這麼簡單實現了。
既然有登陸,那就要有退出登陸。
exitlogin.php
<?php header("Content-type:text/html;charset=utf-8"); session_start(); unset($_SESSION['echo']);//直接unset,或者用session裏面的函數,我沒試過 echo "<script>window.location.href='index.php';</script>"; ?>