您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)怎么在php項目中利用面向?qū)ο髮?shí)現(xiàn)一個用戶登錄驗證功能,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
一、代碼
conn.php
<?php $conn = new com("adodb.connection"); $connstr="driver={microsoft access driver (*.mdb)}; dbq=". realpath("data/db_database07_188.mdb"); $conn->open($connstr); ?>
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>用戶身份驗證</title> <link rel="stylesheet" type="text/css" href="css/style.css" rel="external nofollow" > <style type="text/css"> <!-- .STYLE1 {color: #FFFFFF} --> </style> </head> <body> <table width="250" border="0" align="center" cellpadding="1" cellspacing="0"> <tr> <td height="75" bgcolor="#0099CC"><table width="250" height="75" border="0" cellpadding="0" cellspacing="1"> <form name="form1" method="post" action="index.php"> <tr> <td height="25" colspan="2" bgcolor="#0099CC"><div align="center" class="STYLE1">用戶身份驗證</div></td> </tr> <tr> <td width="60" height="25" bgcolor="#FFFFFF"><div align="center">用戶名:</div></td> <td width="187" bgcolor="#FFFFFF"><div align="left"> <input type="text" name="username" size="22" class="inputcss"></div></td> </tr> <tr> <td height="25" bgcolor="#FFFFFF"><div align="center">密碼:</div></td> <td height="25" bgcolor="#FFFFFF"><div align="left"> <input type="password" name="userpwd" size="22" class="inputcss"></div></td> </tr> <tr> <td height="25" colspan="2" bgcolor="#FFFFFF"><div align="center"><input name="submit" type="submit" value="登錄" class="buttoncss"></div></td> </tr> </form> </table></td> </tr> </table> <?php if($_POST[submit]!="") { $username=$_POST[username]; //接收提交的用戶名 $userpwd=$_POST[userpwd]; //接收提交的密碼 if(trim($username)==""||trim($userpwd)=="") { echo "<script>alert('請輸入用戶名或用戶密碼!');history.back();</script>"; exit; } class chk //定義密碼驗證類 { private $name; //定義用戶名屬性 private $pwd; //定義密碼屬性 public function __construct($x,$y) //構(gòu)造函數(shù),對類的屬性初始化 { $this->name=$x; $this->pwd=$y; } public function chkuser() //驗證用戶身份 { include_once("conn.php"); $rs=new com("adodb.recordset"); //創(chuàng)建記錄集對象 $rs->open("select * from tb_user where username='".$this->name."' and userpwd='".$this->pwd."'",$conn,3,1); if($rs->eof || $rs->bof) { echo "<script>alert('對不起,密碼或用戶名錯誤!');history.back();</script>"; exit; } else { echo "<script>alert('恭喜您登錄成功!');history.back();</script>"; exit; } } } $chk1=new chk($username,$userpwd); //對密碼驗證類進(jìn)行實(shí)例化 $chk1->chkuser(); //調(diào)用chkuser()方法驗證用戶身份 } ?> </body> </html>
二、運(yùn)行結(jié)果
以上就是怎么在php項目中利用面向?qū)ο髮?shí)現(xiàn)一個用戶登錄驗證功能,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。