戻る

ログインフォーム(暗号化)

┣sess_cache

┣index.php

<?php
require_once './config.php';

if(isset($_SESSION["login"]) && $_SESSION["login"] == 1){
	header("Location: ./admin.php");
	exit;
}
?>
<!DOCTYPE HTML>
<html lang="ja-JP">
<head>
<meta charset="UTF-8">
<title>ログインフォーム</title>
</head>
<body>
<form method="POST" action="./admin.php">
<table>
	<tr>
		<td>
		ユーザ名
		</td>
		<td>
		<input name="user" type="text" value="<?=isset($_COOKIE["login_user"])?$_COOKIE["login_user"]:""?>">
		</td>
	</tr>
	<tr>
		<td>
		パスワード
		</td>
		<td>
		<input name="pass" type="password">
		</td>
	</tr>
	<tr>
		<td>
		ユーザ名を保存<input name="check" type="checkbox" value="1"<?=isset($_COOKIE["login_check"]) && $_COOKIE["login_check"]==1?" checked":""?>>
		</td>
		<td align="right">
		<input type="submit" value="ログイン">
		</td>
	</tr>
<?if(isset($_GET["error"]) && $_GET["error"] == "1"):?>
	<tr>
		<td colspan="2">
		ログインに失敗しました
		</td>
	</tr>
<?endif;?>
</table>
</form>
</body>
</html>

┣admin.php

<?php
header("content-type:text/html;charset=utf-8");

require_once './config.php';

if((isset($_POST["user"]) && (hash("md5",$_POST["user"]) == USER) and isset($_POST["pass"]) && hash("md5",$_POST["pass"]) == PASS) or $_SESSION["login"] == 1){
	
	if($_SERVER["REQUEST_METHOD"] == "POST"){
		if(isset($_POST["check"]) && $_POST["check"] == 1){
			#1か月保存
			setcookie("login_check", $_POST["check"], time() + 60 * 60 * 24 * 30);
			setcookie("login_user", $_POST["user"], time() + 60 * 60 * 24 * 30);
		}
		else{
			setcookie("login_check", "", time() -1);
			setcookie("login_user", "", time() -1);
		}
	}
	
	$msg = "ログイン成功";
	
	$_SESSION["login"] = 1;	
}
else{;
	header("Location: index.php?error=1");
	exit;
}
?>
<!DOCTYPE HTML>
<html lang="ja-JP">
<head>
<meta charset="UTF-8">
<title>ログインフォーム</title>
</head>
<body>
<?=$msg?>
<br><br>
<input type="button" value="ログアウト"	onclick="window.location.href='./logout.php';">
</body>
</html>

┣logout.php

<?php
require_once './config.php';

unset($_SESSION["login"]);

header("Location: ./index.php");
exit;
?>

┣config.php

<?php
#ガーベジコレクションのルーチンを起動
ini_set('session.gc_probability', 1);

#データがゴミとみなされる秒数
ini_set('session.gc_maxlifetime', 60*60);

#ガーベジコレクションの働く確率
ini_set('session.gc_divisor', 100);

#セッションキャッシュの保存パス
//session_save_path("C:\\xampp\\htdocs\\loginform\\sess_cache");
session_save_path(dirname(__FILE__)."\\sess_cache");

#セッションスタート
session_start();

#セッションIDの変更
session_regenerate_id(TRUE);

#セッション名の変更
session_name("LOGINSESSION");

#アイパス
define("USER", "55c64b2e108beea023871df110e4f08f");
define("PASS", "55c64b2e108beea023871df110e4f08f");

?>

inserted by FC2 system