今天寫了一個小功能——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
- <!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>
- </head>
- <body>
- <?php
- session_start();
- if ($_SESSION[username]) {
- ?>
- <form id="form3" name="form3" method="post" action="">
- <table width="150" border="1">
- <tr>
- <td width="64">當前使用者</td>
- <td width="70"><?php echo $_SESSION[username];?></td>
- </tr>
- <tr>
- <td colspan="2"><div align="center"><a href="logout.php">登出</a></div></td>
- </tr>
- </table>
- </form>
- <?php
- }else {
- ?>
- <form id="form1" name="form1" method="post" action="userlogin.php">
- <table width="150" border="1">
- <tr>
- <td width="64">使用者</td>
- <td width="70"><label>
- <input name="username" type="text" id="textfield" size="10" />
- </label></td>
- </tr>
- <tr>
- <td>密碼</td>
- <td><label>
- <input name="password" type="password" id="textfield2" size="10" />
- </label></td>
- </tr>
- <tr>
- <td colspan="2">
- <div align="center">
- <input type="submit" name="submit" id="button" value="提交" />
- <input type="reset" name="reset" id="button2" value="重置" />
- </div></td>
- </tr>
- </table>
- </form>
- <?php
- }
- ?>
- </body>
- </html>
config.php
- <?php
- $dbhost="localhost";
- $dbuser="root";
- $dbpassword="123456";
- $dbname="auth";
- $conn=mysql_connect("$dbhost","$dbuser","$dbpassword") or die(`不能連線伺服器`.mysql_error());
- mysql_select_db("$dbname",$conn) or die("資料庫訪問錯誤".mysql_errno());
- mysql_query("set names gb2312");
- ?>
userlogin.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>
- </head>
- <body>
- <?php
- session_start();
- require("config.auth.php");
- if ($_POST[submit]) {
- $sql="select * from userinfo where username = `$_POST[username]` and password = `$_POST[password]`";
- $result=mysql_query($sql);
- $numrows=mysql_num_rows($result);
- if ($numrows == 1) {
- $row=mysql_fetch_assoc($result);
- $_SESSION[username]=$row[username];
- //echo $_SESSION[username];
- echo "<script>alert(`登陸成功!`);window.location.href=`left.php`;</script>";
- }else {
- echo "<script>alert(`登陸失敗!`);window.location.href=`index.html`;</script>";
- }
- }else {
- echo "<script>alert(`請通過正確途徑登陸!`);window.location.href=`index.html`;</script>";
- }
- mysql_free_result($result);
- mysql_close($conn);
- ?>
- </body>
- </html>
logout.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>
- </head>
- <body>
- <?php
- session_start();
- unset($_SESSION[username]);
- session_destroy();
- echo "<script>alert(`登出成功!`);window.location.href=`index.html`;</script>";
- ?>
- </body>
- </html>