php+mysql實現登入註冊修改密碼網頁

來源:互聯網
上載者:User
這篇文章主要為大家詳細介紹了php+mysql實現簡單登入註冊修改密碼系統,具有一定的參考價值,感興趣的小夥伴們可以參考一下

對於php和mysql的串連在許多blog上都有說明,為了將mysql中的查詢,修改,插入等操作掌握,本文介紹了一下如何採用mysql做一個登入註冊修改密碼的網頁。

其中,如下

1.登入-即為對資料庫中的內容給予查詢,並驗證html中的資訊與資料庫是否匹配;
2.註冊-即為對資料庫中的內容進行插入,註冊帳號與密碼;
3.修改密碼-即為對資料庫中的內容進行修改。

這三個操作,我用了8個php和html文本來建立 具體見代碼部分
1.登入的主介面index.html:

<p>   </p><pre name="code" class="html"> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登入註冊修改密碼系統首頁</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="註冊" 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]+$/;//聲明一個判斷使用者名稱前後是否有空格的Regex       if(regex.test(username)||username.length==0)//判定使用者名稱的是否前後有空格或者使用者名稱是否為空白         {           alert("使用者名稱格式不對");           return false;         }       if(regex.test(password)||password.length==0)//同上述內容       {         alert("密碼格式不對");         return false;       }       return true;     }     function register()     {       window.location.href="register.html";//跳轉到註冊頁面     }   </script> </body> </html>

2.登入的後台操作enter.php:

<!doctype html> <html> <head>   <meta charset="UTF-8">   <title>登入系統的後台執行過程</title> </head> <body>   <?php      session_start();//登入系統開啟一個session內容     $username=$_REQUEST["username"];//擷取html中的使用者名稱(通過post請求)     $password=$_REQUEST["password"];//擷取html中的密碼(通過post請求)      $con=mysql_connect("localhost","root","root");//串連mysql 資料庫,賬戶名root ,密碼root     if (!$con) {       die('資料庫連接失敗'.$mysql_error());     }     mysql_select_db("user_info",$con);//use user_info資料庫;     $dbusername=null;     $dbpassword=null;     $result=mysql_query("select * from user_info where username ='{$username}' and isdelete =0;");//查出對應使用者名稱的資訊,isdelete表示在資料庫已被刪除的內容     while ($row=mysql_fetch_array($result)) {//while迴圈將$result中的結果找出來       $dbusername=$row["username"];       $dbpassword=$row["password"];     }     if (is_null($dbusername)) {//使用者名稱在資料庫中不存在時跳回index.html介面   ?>   <script type="text/javascript">     alert("使用者名稱不存在");     window.location.href="index.html";   </script>   <?php      }     else {       if ($dbpassword!=$password){//當對應密碼不對時跳回index.html介面   ?>   <script type="text/javascript">     alert("密碼錯誤");     window.location.href="index.html";   </script>   <?php        }       else {         $_SESSION["username"]=$username;         $_SESSION["code"]=mt_rand(0, 100000);//給session附一個隨機值,防止使用者直接通過調用介面訪問welcome.php   ?>   <script type="text/javascript">     window.location.href="welcome.php";   </script>   <?php        }     }   mysql_close($con);//關閉資料庫連接,如不關閉,下次串連時會出錯   ?> </body> </html>

3.登入成功後的歡迎介面welcome.php:

<!doctype html> <html> <head> <meta charset="UTF-8"> <title>歡迎登入介面</title> </head> <body>  <?php session_start (); if (isset ( $_SESSION ["code"] )) {//判斷code存不存在,如果不存在,說明異常登入   ?> 歡迎登入<?php   echo "${_SESSION["username"]}";//顯示登入使用者名稱   ?><br> 您的ip:<?php   echo "${_SERVER['REMOTE_ADDR']}";//顯示ip   ?> <br> 您的語言: <?php   echo "${_SERVER['HTTP_ACCEPT_LANGUAGE']}";//使用的語言   ?> <br> 瀏覽器版本: <?php   echo "${_SERVER['HTTP_USER_AGENT']}";//瀏覽器版本資訊   ?> <a href="exit.php">退出登入</a> <?php } else {//code不存在,調用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:

<!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/> 確認新密碼<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("使用者名稱格式不對");         return false;       }       if(regex.test(oldpassword)||oldpassword.length==0){         alert("密碼格式不對");         return false;       }       if(regex.test(newpassword)||newpassword.length==0) {         alert("新密碼格式不對");         return false;       }       if (assertpassword != newpassword||assertpassword==0) {         alert("兩次密碼輸入不一致");         return false;       }       return true;      }   </script>  </body> </html>

5.修改密碼的後台操作alter_password.php:

<!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 ( '資料庫連接失敗' . $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("密碼錯誤");     window.location.href="alter_password.html";   </script>   <?php   }   mysql_query ( "update user_info set password='{$newpassword}' where username='{$username}'" ) or die ( "存入資料庫失敗" . mysql_error () );//如果上述使用者名稱密碼判定不錯,則update進資料庫中   mysql_close ( $con );   ?>     <script type="text/javascript">     alert("密碼修改成功");     window.location.href="index.html";   </script> </body> </html>

6.註冊帳號的主介面register.html:

<!doctype html> <html> <head> <meta charset="UTF-8"> <title>註冊系統</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>     確認密碼<input type="password" name="assertpassword" id="assertpassword"><br>     <input type="submit" value="註冊">    </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("使用者名稱格式不對");         return false;       }       if(regex.test(password)||password.length==0){         alert("密碼格式不對");         return false;           }       if(password!=assertpassword){         alert("兩次密碼不一致");         return false;       }     }   </script> </body> </html>

7.註冊帳號的後台操作register.php:

<!doctype html> <html> <head> <meta charset="UTF-8">   <title>註冊使用者</title> </head> <body>   <?php      session_start();     $username=$_REQUEST["username"];     $password=$_REQUEST["password"];      $con=mysql_connect("localhost","root","root");     if (!$con) {       die('資料庫連接失敗'.$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("存入資料庫失敗".mysql_error()) ;     mysql_close($con);   ?>   <script type="text/javascript">     alert("註冊成功");     window.location.href="index.html";   </script>                        </body> </html>

8.非法登入時退出登入的操作exit.php:

<!doctype html> <html> <head> <meta charset="UTF-8"> </head> <body> <?php session_start ();//將session銷毀時調用destroy session_destroy (); ?> <script type="text/javascript">  window.location.href="index.html"; </script> </body> </html>

9.mysql資料庫搭建部分

以上就是本文的全部內容,希望對大家的學習有所協助。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.