今天写了一个小功能——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. > 
  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. $dbhost="localhost";  
  2. $dbuser="root";  
  3. $dbpassword="123456";  
  4. $dbname="auth";  
  5. $conn=mysql_connect("$dbhost","$dbuser","$dbpassword"or die('不能连接服务器'.mysql_error());  
  6. mysql_select_db("$dbname",$connor die("数据库访问错误".mysql_errno());  
  7. mysql_query("set names gb2312");  
  8. ?> 

userlogin.php

   
   
   
   
  1. "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. "http://www.w3.org/1999/xhtml">  
  3.  
  4. "Content-Type" content="text/html; charset=gb2312" />  
  5. 登陆系统  
  6.  
  7.  
  8. session_start();  
  9. require("config.auth.php");  
  10. if ($_POST[submit]) {  
  11.     $sql="select * from userinfo where username = '$_POST[username]' and password = '$_POST[password]'";  
  12.     $result=mysql_query($sql);  
  13.     $numrows=mysql_num_rows($result);  
  14.     if ($numrows == 1) {  
  15.         $row=mysql_fetch_assoc($result);  
  16.         $_SESSION[username]=$row[username];  
  17.         //echo $_SESSION[username];  
  18.         echo "";  
  19.     }else {  
  20.         echo "";  
  21.     }  
  22. }else {  
  23.     echo "";  
  24. }  
  25. mysql_free_result($result);  
  26. mysql_close($conn);  
  27. ?>  
  28.  
  29.  

logout.php

   
   
   
   
  1. "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. "http://www.w3.org/1999/xhtml">  
  3.  
  4. "Content-Type" content="text/html; charset=gb2312" />  
  5. 登陆系统  
  6.  
  7.  
  8.     session_start();  
  9.     unset($_SESSION[username]);  
  10.     session_destroy();  
  11.     echo "";  
  12. ?>  
  13.  
  14.