今天寫了一個小功能——PHP登陸 出來使用,才剛學PHP不久,有錯誤的地方請大蝦們指正指正:

left.php           登陸頁面

userlogin.php  驗證功能

login.php         登出功能

config.auth.php 資料庫連線功能

就四個頁面,left.php通過post提交資訊給userlog.php處理,userlog.php呼叫config.auth.php連線資料庫進行資料處理,然後把結果返回left.php,logout.php就只是登出session功能。沒有加入cookie,還暫時不需要儲存cookie!程式碼如下:

left.php

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  5. <title>登陸系統</title> 
  6. </head> 
  7. <body> 
  8. <?php 
  9. session_start();  
  10. if ($_SESSION[username]) {  
  11. ?> 
  12. <form id="form3" name="form3" method="post" action=""> 
  13. <table width="150" border="1"> 
  14.     <tr> 
  15.       <td width="64">當前使用者</td> 
  16.       <td width="70"><?php echo $_SESSION[username];?></td> 
  17.     </tr> 
  18.     <tr> 
  19.       <td colspan="2"><div align="center"><a href="logout.php">登出</a></div></td> 
  20.     </tr> 
  21.   </table> 
  22. </form> 
  23. <?php 
  24. }else {  
  25. ?> 
  26. <form id="form1" name="form1" method="post" action="userlogin.php"> 
  27.   <table width="150" border="1"> 
  28.     <tr> 
  29.       <td width="64">使用者</td> 
  30.       <td width="70"><label> 
  31.         <input name="username" type="text" id="textfield" size="10" /> 
  32.       </label></td> 
  33.     </tr> 
  34.     <tr> 
  35.       <td>密碼</td> 
  36.       <td><label> 
  37.         <input name="password" type="password" id="textfield2" size="10" /> 
  38.       </label></td> 
  39.     </tr> 
  40.     <tr> 
  41.       <td colspan="2"> 
  42.         <div align="center"> 
  43.           <input type="submit" name="submit" id="button" value="提交" /> 
  44.           <input type="reset" name="reset" id="button2" value="重置" /> 
  45.           </div></td> 
  46.     </tr> 
  47.   </table> 
  48. </form> 
  49. <?php 
  50. }  
  51. ?> 
  52. </body> 
  53. </html> 

config.php

  1. <?php  
  2. $dbhost="localhost";  
  3. $dbuser="root";  
  4. $dbpassword="123456";  
  5. $dbname="auth";  
  6. $conn=mysql_connect("$dbhost","$dbuser","$dbpassword"or die(`不能連線伺服器`.mysql_error());  
  7. mysql_select_db("$dbname",$connor die("資料庫訪問錯誤".mysql_errno());  
  8. mysql_query("set names gb2312");  
  9. ?> 

userlogin.php

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  5. <title>登陸系統</title>  
  6. </head>  
  7. <body>  
  8. <?php  
  9. session_start();  
  10. require("config.auth.php");  
  11. if ($_POST[submit]) {  
  12.     $sql="select * from userinfo where username = `$_POST[username]` and password = `$_POST[password]`";  
  13.     $result=mysql_query($sql);  
  14.     $numrows=mysql_num_rows($result);  
  15.     if ($numrows == 1) {  
  16.         $row=mysql_fetch_assoc($result);  
  17.         $_SESSION[username]=$row[username];  
  18.         //echo $_SESSION[username];  
  19.         echo "<script>alert(`登陸成功!`);window.location.href=`left.php`;</script>";  
  20.     }else {  
  21.         echo "<script>alert(`登陸失敗!`);window.location.href=`index.html`;</script>";  
  22.     }  
  23. }else {  
  24.     echo "<script>alert(`請通過正確途徑登陸!`);window.location.href=`index.html`;</script>";  
  25. }  
  26. mysql_free_result($result);  
  27. mysql_close($conn);  
  28. ?>  
  29. </body>  
  30. </html> 

logout.php

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  5. <title>登陸系統</title>  
  6. </head>  
  7. <body>  
  8. <?php  
  9.     session_start();  
  10.     unset($_SESSION[username]);  
  11.     session_destroy();  
  12.     echo "<script>alert(`登出成功!`);window.location.href=`index.html`;</script>";  
  13. ?>  
  14. </body>  
  15. </html>