賈維斯的智慧工坊

  • Home
  • About
  • Note
  • Project
  • Experience
  • Service
  • Sitemap


MY | NOTES

  1. 首頁
  2. >
  3. 筆記
  4. >
  5. 網頁設計

[HTML/CSS/PHP] Login example

用PHP設計登入功能
Mar, 2016
學會使用表單form後,除了可以用來存資料,另一個功能就是登入功能
我們有兩個頁面,分別為index.php(登入畫面)與home.php(登入後畫面)
以下要示範的範例具有以下功能

1、密碼可以自行設定,並且為顯示為「*」
2、當網址連結到home.php時,自動跳轉到index.php

index.php

<?php
	//密碼設定為123,php的語法在網頁運行時,檢視原始碼中不會顯示,無需擔心
	if(isset($_POST[password])){
		$passward = $_POST[password];
		if($passward == '123'){
		    setcookie("login",'USER', time()+3600);
		    //將網址改為登入成功後要導向的頁面
		    header("Location: home.php"); 
		} 
	}

?>
Please enter the password:<br>
<form name="form" method="post" action="index.php">
	<input type="password" id="password" name="password"></input><br>
	<button>ENTER</button>
</form>
									

home.php

可以放在任意位置,如果他人直接進到home.php此網址,也會需要重新移轉到登入畫面index.php

<?php if(!isset($_COOKIE["login"])){
	//將網址改為要導入的登入頁面
	header("Location: index.php");
}
?>
									

Reference

1、Cookie 應用:利用 PHP 製作簡單的網站登入系統,判斷使用者是否已登入

← Back to note