index.php
<?php
//sessionを使う宣言
session_start();
$username = 'abcd';
$password = 'pass';
//$_SESSION = array(
// 'test' => $username,
// 'test2' => $password,
//);
//$_SESSION['test'] = "テストです";
//$_SESSION['test'] = $username;はサーバのファイルに一時保存する 保存したものはどこでもechoで表示できるので
//今回はmypageでパスとユーザーネームを表示させている
$_SESSION['test'] = $username;
$_SESSION['test2'] = $password;
//header('Location: /mypage.php');
//exit;
?>
<?php if (! $_POST): ?>
<!--action 空だと自分に返ってくる-->
<form action="" method="post">
<p>ユーザーネーム</p><br>
<input type="text" name="username" value="">
<br>
<p>パスワード</p><br>
<input type="password" name="password" value="">
<br>
<input type="submit" value="送信ボタンです">
</form>
<?php endif; ?>
<?php
$input_name = $_POST['username'];
$input_password = $_POST['password'];
if(isset($input_name) && !empty($input_name) && isset($input_password) && !empty($input_password)) {
if ($username === $input_name && $password === $input_password) {
//requireはファイルの呼び出し
//require './mypage.php';
//header指定はページに遷移する
header('Location: /mypage.php');
//headerの後は必ずexit;を書くこと
exit;
} else {
//ファイルの呼び出し
require './no_user.php';
}
}
?>
mypage.php
<h1>マイページにようこそ</h1>
<?php
session_start();
//表示方法printでもechoでもどちらでもよいがechoが好ましい
if (isset($_SESSION["test"]) && ! empty($_SESSION['test'])) {
print "<p>";
print "ログインID:".$_SESSION["test"];
print "</p>";
}
if (isset($_SESSION["test2"])) {
print "<p>";
print "ログインパスワード:".$_SESSION["test2"];
print "</p>";
}
?>
no_user.php
<h1>会員ではないので、会員になって下さい。</h1>