正好最近的域名備案通過(guò)了,興起就突然想做一個(gè)網(wǎng)頁(yè),雖然之前去備案域名也是有這個(gè)目的。
問(wèn)過(guò)幾個(gè)人,說(shuō)用linux上用PHP搭建網(wǎng)站很簡(jiǎn)單,就試著做了一個(gè),這里主要說(shuō)一下登錄驗(yàn)證相關(guān)的部分;
首相準(zhǔn)備幾個(gè)文件,主要是index.php、conn.php、data.php以及l(fā)ogin.php;
login.php 主要是登錄過(guò)程中的數(shù)據(jù)對(duì)比部分;其中include ('conn.php')內(nèi)容在下面有說(shuō)。
<?php if(!isset($_POST['submit'])){ exit('login in error.'); } $username = htmlspecialchars($_POST['username']); $password = MD5($_POST['password']); include('conn.php'); echo"$password"; $check_query = mysqli_query($result,"select USERID from USERINFO where EMAIL='$username' and PASSWORD='$password' limit 1"); if($ret = mysqli_fetch_array($check_query)){ echo'connect true.'; } else { echo'connect false'; } ?>
另外要注意的是:關(guān)于$_POST針對(duì)的是form中的method =“post”中的內(nèi)容。
因?yàn)槔锩嬗玫組D5加密的方式,所以說(shuō)一下,在后臺(tái)數(shù)據(jù)庫(kù)加密的時(shí)候也需要用到MD5加密的方式update數(shù)據(jù),具體方式如下:
UPDATE USERINFO SET PASSWORD = md5('root') WHERE USERID = 1000;
其中的表格以及具體查詢(xún)位置根據(jù)個(gè)人數(shù)據(jù)庫(kù)而定。
conn.php 主要是跟mysql數(shù)據(jù)庫(kù)連接相關(guān)的操作,分為數(shù)據(jù)庫(kù)連接,以及數(shù)據(jù)庫(kù)選擇部分(注意數(shù)據(jù)庫(kù)連接的返回值取值,不要隨便起,后邊引用的時(shí)候是用得著的。)
<?php try{ $result = mysqli_connect('localhost','root','root'); mysqli_select_db($result,'WEBDATAS'); }catch(Exception $e) { echo $e->message; exit; } if(!$result) { return false; } echo "ok\n"; ?>
剩下的主要是index.php 該文件是主頁(yè)相關(guān)了,我只把登錄相關(guān)的部分拿出來(lái)說(shuō)明一下(這里用到的是boostrap中的模板,有興趣的可以百度一下boostrap)
<?php session_start(); include_once('data.php'); $handle = db_connect(); if(!$handle){ echo 'Did not access to the database'; }else{ echo'connect success'; } ?>
其中包含的data.php登錄部分如下:
<div class="modal fade" tabindex="-1" role="dialog" id="login"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h3 class="modal-title">ログイン</h3> </div> <div class="modal-body"> <form class="form-signin" action="login.php" method = "post"> <h4 class="form-signin-heading">利用者名 パスワード 入力</h4> <label for="inputEmail" class="sr-only">利用者名</label> <input type="email" name="username" id="inputEmail" class="form-control" placeholder="利用者名を入力" required autofocus> <label for="inputPassword" class="sr-only">パスワード</label> <input type="password" name="password" id="inputPassword" class="form-control" placeholder="パスワード" required> <div class="checkbox"> <label> <input type="checkbox" value="remember-me"> ログイン狀態(tài)を保持 </label> </div> <button class="btn btn-lg btn-primary btn-block" type="submit" name="submit">ログイン</button> </form> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">クローズ</button> </div> </div> <!-- /.modal-content --> </div> <!-- /.modal-dialog --> </div>
基本關(guān)于登錄驗(yàn)證部分就是這些,主要是與數(shù)據(jù)庫(kù)的數(shù)據(jù)進(jìn)行對(duì)比,一開(kāi)始用的對(duì)稱(chēng)加密的方式,對(duì)加密的方式還沒(méi)有仔細(xì)去研究,后面有機(jī)會(huì)再仔細(xì)看一下就好。
以上這篇ubutu 16.04環(huán)境下,PHP與mysql數(shù)據(jù)庫(kù),網(wǎng)頁(yè)登錄驗(yàn)證實(shí)例講解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持。