《PHP實(shí)例:php+mysql實(shí)現(xiàn)簡(jiǎn)單登錄注冊(cè)修改密碼網(wǎng)頁(yè)》要點(diǎn):
本文介紹了PHP實(shí)例:php+mysql實(shí)現(xiàn)簡(jiǎn)單登錄注冊(cè)修改密碼網(wǎng)頁(yè),希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
對(duì)于php和mysql的連接在許多blog上都有說(shuō)明,為了將mysql中的查詢,修改,插入等操作掌握,本文介紹了一下如何采用mysql做一個(gè)登錄注冊(cè)修改密碼的網(wǎng)頁(yè).
PHP實(shí)例
其中,如下
PHP實(shí)例
1.登錄-即為對(duì)數(shù)據(jù)庫(kù)中的內(nèi)容給予查詢,并驗(yàn)證html中的信息與數(shù)據(jù)庫(kù)是否匹配;
2.注冊(cè)-即為對(duì)數(shù)據(jù)庫(kù)中的內(nèi)容進(jìn)行插入,注冊(cè)帳號(hào)與密碼;
3.修改密碼-即為對(duì)數(shù)據(jù)庫(kù)中的內(nèi)容進(jìn)行修改.
PHP實(shí)例
這三個(gè)操作,我用了8個(gè)php和html文本來(lái)建立 具體見(jiàn)代碼部分
1.登錄的主界面index.html:
PHP實(shí)例
<p> </p><pre name="code" class="html"> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登錄注冊(cè)修改密碼系統(tǒng)主頁(yè)</title> <style type="text/css"> form { text-align: center; } </style> </head> <body> <form action="enter.php" method="post" onsubmit="return enter()"> 用戶名<input type="text" name="username" id="username"><br> 密碼<input type="password" name="password" id="password"><br> <input type="submit" value="登錄"> <input type="button" value="注冊(cè)" onclick="register();"> </form> <script type="text/javascript"> function enter() { var username=document.getElementById("username").value;//獲取form中的用戶名 var password=document.getElementById("password").value; var regex=/^[/s]+$/;//聲明一個(gè)判斷用戶名前后是否有空格的正則表達(dá)式 if(regex.test(username)||username.length==0)//判定用戶名的是否前后有空格或者用戶名是否為空 { alert("用戶名格式不對(duì)"); return false; } if(regex.test(password)||password.length==0)//同上述內(nèi)容 { alert("密碼格式不對(duì)"); return false; } return true; } function register() { window.location.href="register.html";//跳轉(zhuǎn)到注冊(cè)頁(yè)面 } </script> </body> </html>
2.登錄的后臺(tái)操作enter.php:PHP實(shí)例
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>登錄系統(tǒng)的后臺(tái)執(zhí)行過(guò)程</title> </head> <body> <?php session_start();//登錄系統(tǒng)開啟一個(gè)session內(nèi)容 $username=$_REQUEST["username"];//獲取html中的用戶名(通過(guò)post請(qǐng)求) $password=$_REQUEST["password"];//獲取html中的密碼(通過(guò)post請(qǐng)求) $con=mysql_connect("localhost","root","root");//連接mysql 數(shù)據(jù)庫(kù),賬戶名root ,密碼root if (!$con) { die('數(shù)據(jù)庫(kù)連接失敗'.$mysql_error()); } mysql_select_db("user_info",$con);//use user_info數(shù)據(jù)庫(kù); $dbusername=null; $dbpassword=null; $result=mysql_query("select * from user_info where username ='{$username}' and isdelete =0;");//查出對(duì)應(yīng)用戶名的信息,isdelete表示在數(shù)據(jù)庫(kù)已被刪除的內(nèi)容 while ($row=mysql_fetch_array($result)) {//while循環(huán)將$result中的結(jié)果找出來(lái) $dbusername=$row["username"]; $dbpassword=$row["password"]; } if (is_null($dbusername)) {//用戶名在數(shù)據(jù)庫(kù)中不存在時(shí)跳回index.html界面 ?> <script type="text/javascript"> alert("用戶名不存在"); window.location.href="index.html"; </script> <?php } else { if ($dbpassword!=$password){//當(dāng)對(duì)應(yīng)密碼不對(duì)時(shí)跳回index.html界面 ?> <script type="text/javascript"> alert("密碼錯(cuò)誤"); window.location.href="index.html"; </script> <?php } else { $_SESSION["username"]=$username; $_SESSION["code"]=mt_rand(0, 100000);//給session附一個(gè)隨機(jī)值,防止用戶直接通過(guò)調(diào)用界面訪問(wèn)welcome.php ?> <script type="text/javascript"> window.location.href="welcome.php"; </script> <?php } } mysql_close($con);//關(guān)閉數(shù)據(jù)庫(kù)連接,如不關(guān)閉,下次連接時(shí)會(huì)出錯(cuò) ?> </body> </html>
3.登錄成功后的歡迎界面welcome.php:
PHP實(shí)例
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>歡迎登錄界面</title> </head> <body> <?php session_start (); if (isset ( $_SESSION ["code"] )) {//判斷code存不存在,如果不存在,說(shuō)明異常登錄 ?> 歡迎登錄<?php echo "${_SESSION["username"]}";//顯示登錄用戶名 ?><br> 您的ip:<?php echo "${_SERVER['REMOTE_ADDR']}";//顯示ip ?> <br> 您的語(yǔ)言: <?php echo "${_SERVER['HTTP_ACCEPT_LANGUAGE']}";//使用的語(yǔ)言 ?> <br> 瀏覽器版本: <?php echo "${_SERVER['HTTP_USER_AGENT']}";//瀏覽器版本信息 ?> <a href="exit.php">退出登錄</a> <?php } else {//code不存在,調(diào)用exit.php 退出登錄 ?> <script type="text/javascript"> alert("退出登錄"); window.location.href="exit.php"; </script> <?php } ?> <br> <a href="alter_password.html">修改密碼</a> </body> </html>
4.修改密碼的主界面alter_password.html:
PHP實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>修改密碼</title> <style type="text/css"> form{ text-align: center; } </style> </head> <body> <?php session_start(); ?> <form action="alter_password.php" method="post" onsubmit="return alter()"> 用戶名<input type="text" name="username" id ="username" /><br/> 舊密碼<input type="password" name="oldpassword" id ="oldpassword"/><br/> 新密碼<input type="password" name="newpassword" id="newpassword"/><br/> 確認(rèn)新密碼<input type="password" name="assertpassword" id="assertpassword"/><br/> <input type="submit" value="修改密碼" onclick="return alter()"> </form> <script type="text/javascript"> document.getElementById("username").value="<? php echo "${_SESSION["username"]}";?>" </script> <script type="text/javascript"> function alter() { var username=document.getElementById("username").value; var oldpassword=document.getElementById("oldpassword").value; var newpassword=document.getElementById("newpassword").value; var assertpassword=document.getElementById("assertpassword").value; var regex=/^[/s]+$/; if(regex.test(username)||username.length==0){ alert("用戶名格式不對(duì)"); return false; } if(regex.test(oldpassword)||oldpassword.length==0){ alert("密碼格式不對(duì)"); return false; } if(regex.test(newpassword)||newpassword.length==0) { alert("新密碼格式不對(duì)"); return false; } if (assertpassword != newpassword||assertpassword==0) { alert("兩次密碼輸入不一致"); return false; } return true; } </script> </body> </html>
5.修改密碼的后臺(tái)操作alter_password.php:
PHP實(shí)例
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>正在修改密碼</title> </head> <body> <?php session_start (); $username = $_REQUEST ["username"]; $oldpassword = $_REQUEST ["oldpassword"]; $newpassword = $_REQUEST ["newpassword"]; $con = mysql_connect ( "localhost", "root", "root" ); if (! $con) { die ( '數(shù)據(jù)庫(kù)連接失敗' . $mysql_error () ); } mysql_select_db ( "user_info", $con ); $dbusername = null; $dbpassword = null; $result = mysql_query ( "select * from user_info where username ='{$username}' and isdelete =0;" ); while ( $row = mysql_fetch_array ( $result ) ) { $dbusername = $row ["username"]; $dbpassword = $row ["password"]; } if (is_null ( $dbusername )) { ?> <script type="text/javascript"> alert("用戶名不存在"); window.location.href="alter_password.html"; </script> <?php } if ($oldpassword != $dbpassword) { ?> <script type="text/javascript"> alert("密碼錯(cuò)誤"); window.location.href="alter_password.html"; </script> <?php } mysql_query ( "update user_info set password='{$newpassword}' where username='{$username}'" ) or die ( "存入數(shù)據(jù)庫(kù)失敗" . mysql_error () );//如果上述用戶名密碼判定不錯(cuò),則update進(jìn)數(shù)據(jù)庫(kù)中 mysql_close ( $con ); ?> <script type="text/javascript"> alert("密碼修改成功"); window.location.href="index.html"; </script> </body> </html>
6.注冊(cè)帳號(hào)的主界面register.html:PHP實(shí)例
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>注冊(cè)系統(tǒng)</title> <style type="text/css"> form { text-align: center; } </style> </head> <body> <form action="register.php" method="post" name="form_register" onsubmit="return check()"> 用戶名<input type="text" name="username" id="username"><br> 密碼<input type="password" name="password" id="password"><br> 確認(rèn)密碼<input type="password" name="assertpassword" id="assertpassword"><br> <input type="submit" value="注冊(cè)"> </form> <script type="text/javascript"> function check() { var username=document.getElementById("username").value; var password=document.getElementById("password").value; var assertpassword=document.getElementById("assertpassword").value; var regex=/^[/s]+$/; if(regex.test(username)||username.length==0){ alert("用戶名格式不對(duì)"); return false; } if(regex.test(password)||password.length==0){ alert("密碼格式不對(duì)"); return false; } if(password!=assertpassword){ alert("兩次密碼不一致"); return false; } } </script> </body> </html>
7.注冊(cè)帳號(hào)的后臺(tái)操作register.php:PHP實(shí)例
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>注冊(cè)用戶</title> </head> <body> <?php session_start(); $username=$_REQUEST["username"]; $password=$_REQUEST["password"]; $con=mysql_connect("localhost","root","root"); if (!$con) { die('數(shù)據(jù)庫(kù)連接失敗'.$mysql_error()); } mysql_select_db("user_info",$con); $dbusername=null; $dbpassword=null; $result=mysql_query("select * from user_info where username ='{$username}' and isdelete =0;"); while ($row=mysql_fetch_array($result)) { $dbusername=$row["username"]; $dbpassword=$row["password"]; } if(!is_null($dbusername)){ ?> <script type="text/javascript"> alert("用戶已存在"); window.location.href="register.html"; </script> <?php } mysql_query("insert into user_info (username,password) values('{$username}','{$password}')") or die("存入數(shù)據(jù)庫(kù)失敗".mysql_error()) ; mysql_close($con); ?> <script type="text/javascript"> alert("注冊(cè)成功"); window.location.href="index.html"; </script> </body> </html>
8.非法登錄時(shí)退出登錄的操作exit.php:
PHP實(shí)例
<!doctype html> <html> <head> <meta charset="UTF-8"> </head> <body> <?php session_start ();//將session銷毀時(shí)調(diào)用destroy session_destroy (); ?> <script type="text/javascript"> window.location.href="index.html"; </script> </body> </html>
9.mysql數(shù)據(jù)庫(kù)搭建部分PHP實(shí)例
PHP實(shí)例
PHP實(shí)例
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持維易PHP.PHP實(shí)例
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/2541.html