本帖最後由 guo.cane 於 2013-11-26 21:30 編輯
SESSION- <?php
- header('Content-Type:text/html; charset=utf-8');
- session_start(); //啟動SESSION 機制, 使用此函式之前不允許任何輸出
- ?>
- <html>
- <head>
- <title>SESSION</title>
- <meta http-equiv="Content-Type" Content="text/html; charset=utf-8" />
- </head>
- <body>
- <?php
- //session_register('name'); //註冊 SESSION, 新版本的PHP不在支援
- $_SESSION['name'] = 'Steve2';
- $_SESSION['number'] = 10;
- $_SESSION['bool'] = true;
- unset($_SESSION['name']); //刪除SESSION
- echo $_SESSION['name'];
- ?>
- </body>
- </html>
複製代碼 會員登入頁 login.php- <?php
- session_start(); //啟動SESSION功能
- header('Content-Type:text/html; charset=utf-8');
- ?>
- <html>
- <head>
- <title>會員登入</title>
- <meta http-equiv="Content-Type" Content="text/html; charset=utf-8" />
- <script type="text/javascript">
- //自定方法 格式: function 方法名稱()
- function check(){
- //執行的程式碼 var=區域變數
- var $account = document.form_login.account.value; //文件(document)->表單(form)->帳號欄位(account)的值(value)
- var $password = document.form_login.password.value; //文件(document)->表單(form)->密碼欄位(password)的值(value)
- if($account==''){ //若帳號為空值
- alert('請輸入帳號');
- }else if($password==''){ //若密碼為空值
- alert('請輸入密碼');
- }else{
- document.form_login.submit(); //將表單送出
- }
- }
- </script>
- </head>
- <body>
- <?php if($_SESSION['account']==''){ //若SESSION是空值的話, 就顯示登入頁面?>
- <form method="post" action="process.php" name="form_login">
- 帳號:<input type="text" name="account" /><br />
- 密碼:<input type="password" name="password" /><br />
- <input type="button" name="send" value="登入" onclick="check();" /><!-- 點擊後會執行 javascript的check方法 -->
- <input type="reset" name="reset" value="重設" />
- </form>
- <?php
- }else{ //若SESSION不是空值的話, 就顯示歡迎字樣
- echo $_SESSION['account'].'您好, 歡迎登入!! <a href="logout.php">登出</a>';
- }
- ?>
- </body>
- </html>
複製代碼 資料處理頁 process.php- <?php
- session_start(); //啟動SESSION功能
- header('Content-Type:text/html; charset=utf-8');
- $account = $_POST['account']; //取得帳號
- $password = $_POST['password']; //取得密碼
-
- $default_account = 'abc'; //預設帳號
- $default_password = '123'; //預設密碼
-
- //echo $account.'<br />';
- //echo $password.'<br />';
- if($account==$default_account && $password==$default_password){
- //帳號與密碼符合, 註冊SESSION, 並導回login.php頁面
- $_SESSION['account'] = $account; //指派 $account 到 $_SESSION
- //header('Refresh:3; URL=login.php');
- //echo '登入成功,三秒鐘後導回';
-
- //Javascript
- echo '<script type="text/javascript">';
- echo 'alert("登入成功");'; //跳出警告視窗
- echo 'window.location.href = "login.php";'; //導頁至 login.php
- echo '</script>';
- }else{
- //帳號或密碼不符合
- //header('Refresh:3; URL=login.php');
- //echo '帳號或密碼錯誤,三秒鐘後導回';
-
- //Javascript
- echo '<script type="text/javascript">';
- echo 'alert("帳號或密碼錯誤");'; //跳出警告視窗
- echo 'window.location.href = "login.php";'; //導頁至 login.php
- echo '</script>';
- }
- ?>
複製代碼 Javascript- <html>
- <head>
- <title>Javascript</title>
- <meta http-equiv="Content-Type" Content="text/html; charset=utf-8" />
- <script type="text/javascript">
- alert('HI, 我是Steve');
- </script>
- </head>
- <body>
-
- </body>
- </html>
複製代碼 |