標籤:style http ar io color os sp for java
login.html
- <!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=gbk" />
- <title>使用者登入</title>
- <style type="text/css">
- html{font-size:12px;}
- fieldset{width:520px; margin: 0 auto;}
- legend{font-weight:bold; font-size:14px;}
- label{float:left; width:70px; margin-left:10px;}
- .left{margin-left:80px;}
- .input{width:150px;}
- span{color: #666666;}
- </style>
- <script language=JavaScript>
- <!--
-
- function InputCheck(LoginForm)
- {
- if (LoginForm.username.value == "")
- {
- alert("請輸入使用者名稱!");
- LoginForm.username.focus();
- return (false);
- }
- if (LoginForm.password.value == "")
- {
- alert("請輸入密碼!");
- LoginForm.password.focus();
- return (false);
- }
- }
-
- //-->
- </script>
- </head>
- <body>
- <div>
- <fieldset>
- <legend>使用者登入</legend>
- <form name="LoginForm" method="post" action="login.php" onSubmit="return InputCheck(this)">
- <p>
- <label for="username" class="label">使用者名稱:</label>
- <input id="username" name="username" type="text" class="input" />
- <p/>
- <p>
- <label for="password" class="label">密 碼:</label>
- <input id="password" name="password" type="password" class="input" />
- <p/>
- <p>
- <input type="submit" name="submit" value=" 確 定 " class="left" />
- </p>
- </form>
- </fieldset>
- </div>
- </body>
- </html>
conn.php
- <?php
- /*****************************
- *資料庫連接
- *****************************/
- $conn = @mysql_connect("localhost","root","root123");
- if (!$conn){
- die("串連資料庫失敗:" . mysql_error());
- }
- mysql_select_db("test", $conn);
- //字元轉換,讀庫
- mysql_query("set character set ‘gbk‘");
- //寫庫
- mysql_query("set names ‘gbk‘");
- ?>
reg.php
- <?php
- if(!isset($_POST[‘submit‘])){
- exit(‘非法訪問!‘);
- }
- $username = $_POST[‘username‘];
- $password = $_POST[‘password‘];
- $email = $_POST[‘email‘];
- //註冊資訊判斷
- if(!preg_match(‘/^[\w\x80-\xff]{3,15}$/‘, $username)){
- exit(‘錯誤:使用者名稱不符合規定。<a href="javascript:history.back(-1);">返回</a>‘);
- }
- if(strlen($password) < 6){
- exit(‘錯誤:密碼長度不符合規定。<a href="javascript:history.back(-1);">返回</a>‘);
- }
- if(!preg_match(‘/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/‘, $email)){
- exit(‘錯誤:電子郵箱格式錯誤。<a href="javascript:history.back(-1);">返回</a>‘);
- }
- //包含資料庫連接檔案
- include(‘conn.php‘);
- //檢測使用者名稱是否已經存在
- $check_query = mysql_query("select uid from user where username=‘$username‘ limit 1");
- if(mysql_fetch_array($check_query)){
- echo ‘錯誤:使用者名稱 ‘,$username,‘ 已存在。<a href="javascript:history.back(-1);">返回</a>‘;
- exit;
- }
- //寫入資料
- $password = MD5($password);
- $regdate = time();
- $sql = "INSERT INTO user(username,password,email,regdate)VALUES(‘$username‘,‘$password‘,‘$email‘,
- $regdate)";
- if(mysql_query($sql,$conn)){
- exit(‘使用者註冊成功!點擊此處 <a href="login.html">登入</a>‘);
- } else {
- echo ‘抱歉!添加資料失敗:‘,mysql_error(),‘<br />‘;
- echo ‘點擊此處 <a href="javascript:history.back(-1);">返回</a> 重試‘;
- }
- ?>
login.html
- <!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=gbk" />
- <title>使用者登入</title>
- <style type="text/css">
- html{font-size:12px;}
- fieldset{width:300px; margin: 0 auto;}
- legend{font-weight:bold; font-size:14px;}
- .label{float:left; width:70px; margin-left:10px;}
- .left{margin-left:80px;}
- .input{width:150px;}
- span{color: #666666;}
- </style>
- <script language=JavaScript>
- <!--
-
- function InputCheck(LoginForm)
- {
- if (LoginForm.username.value == "")
- {
- alert("請輸入使用者名稱!");
- LoginForm.username.focus();
- return (false);
- }
- if (LoginForm.password.value == "")
- {
- alert("請輸入密碼!");
- LoginForm.password.focus();
- return (false);
- }
- }
-
- //-->
- </script>
- </head>
- <body>
- <div>
- <fieldset>
- <legend>使用者登入</legend>
- <form name="LoginForm" method="post" action="login.php" onSubmit="return InputCheck(this)">
- <p>
- <label for="username" class="label">使用者名稱:</label>
- <input id="username" name="username" type="text" class="input" />
- <p/>
- <p>
- <label for="password" class="label">密 碼:</label>
- <input id="password" name="password" type="password" class="input" />
- <p/>
- <p>
- <input type="submit" name="submit" value=" 確 定 " class="left" />
- </p>
- </form>
- </fieldset>
- </div>
- </body>
- </html>
login.php
- <?php
- session_start();
-
- //登出登入
- if($_GET[‘action‘] == "logout"){
- unset($_SESSION[‘userid‘]);
- unset($_SESSION[‘username‘]);
- echo ‘登出登入成功!點擊此處 <a href="login.html">登入</a>‘;
- exit;
- }
-
- //登入
- if(!isset($_POST[‘submit‘])){
- exit(‘非法訪問!‘);
- }
- $username = htmlspecialchars($_POST[‘username‘]);
- $password = MD5($_POST[‘password‘]);
-
- //包含資料庫連接檔案
- include(‘conn.php‘);
- //檢測使用者名稱及密碼是否正確
- $check_query = mysql_query("select uid from user where username=‘$username‘ and password=‘$password‘
- limit 1");
- if($result = mysql_fetch_array($check_query)){
- //登入成功
- $_SESSION[‘username‘] = $username;
- $_SESSION[‘userid‘] = $result[‘uid‘];
- echo $username,‘ 歡迎你!進入 <a href="my.php">使用者中心</a><br />‘;
- echo ‘點擊此處 <a href="login.php?action=logout">登出</a> 登入!<br />‘;
- exit;
- } else {
- exit(‘登入失敗!點擊此處 <a href="javascript:history.back(-1);">返回</a> 重試‘);
- }
- ?>
my.php
- <?php
- session_start();
-
- //檢測是否登入,若沒登入則轉向登入介面
- if(!isset($_SESSION[‘userid‘])){
- header("Location:login.html");
- exit();
- }
-
- //包含資料庫連接檔案
- include(‘conn.php‘);
- $userid = $_SESSION[‘userid‘];
- $username = $_SESSION[‘username‘];
- $user_query = mysql_query("select * from user where uid=$userid limit 1");
- $row = mysql_fetch_array($user_query);
- echo ‘使用者資訊:<br />‘;
- echo ‘使用者ID:‘,$userid,‘<br />‘;
- echo ‘使用者名稱:‘,$username,‘<br />‘;
- echo ‘郵箱:‘,$row[‘email‘],‘<br />‘;
- echo ‘註冊日期:‘,date("Y-m-d", $row[‘regdate‘]),‘<br />‘;
- echo ‘<a href="login.php?action=logout">登出</a> 登入<br />‘;
- ?>
社交
PHP 附錄 : 使用者註冊與登入完整代碼