PHP vol.19

  • ログインシステムを作ってみる。

ログインシステムの制作

仕様

ログイン画面

入力されたID,Passをファイル内に存在していればログイン成功。

  • エラーメッセージ一覧
    • 指定したアカウントがファイル内に存在しない→ログインIDかパスワードのどちらかが間違っている可能性があります。再度ご確認下さい
    • ログインIDが入力されていない→ログインIDを入力してください。
    • パスワードが入力されていない→パスワードを入力してください。
管理者一覧画面
  • 現在ログインしているアカウント名を表示
  • ファイル内に存在している管理者数を表示
  • ファイル内に存在する管理者アカウント情報をすべて一覧に表示

任意で拡張

  • ログアウト機能
  • ファイルの存在有無をチェックする
  • 管理者一覧に一人も居なかった場合に表示するメッセージを考える
index.php
<?php
session_set_cookie_params(60*60);
session_start();
$filepath = '設定データファイル';
$file = file($filepath);
foreach ($file as $value) {
    list($uid,$login_id,$login_pass,$u_name,$e_mail,$create_date,$reload_date) = explode(",",$value);
    if (empty($_POST['login'])&&empty($_POST['password'])) {
        $msg = "ログインIDとパスワードを入力してください。";
    } elseif (empty($_POST['login'])) {
        $msg = "ログインIDを入力してください。";
    } elseif (empty($_POST['password'])) {
        $msg = "パスワードを入力してください。";
    } elseif ($_POST['login']!=$login_id||$_POST['password']!=$login_pass) {
        $msg = "ログインIDかパスワードのどちらかが間違っている可能性があります。再度ご確認下さい。";
    } elseif ($_POST['login']==$login_id&&$_POST['password']==$login_pass) {       
        $_SESSION['login_id'] = $login_id;
        $_SESSION['login_pass'] = $login_pass;
        header("Location:account.php");
        
    }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>ログイン</title>
<link href="./css/backend.css" rel="stylesheet" type="text/css" />
<link href="./css/literal.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="layout">
    <div id="header">
        <div id="headerbody">
            <div class="logo">
                <h1>xxxxシステム<br />管理者画面</h1>
            </div>
        </div>
    </div>

    <div id="contents">
        <div id="contentsbody">
            <div id="main">
                <div id="mainbody">
                    <h2>ログイン</h2>
                    <p><span class="red weight-bold">[!]ご注意ください</span><br>・必ず利用者自身のアカウントで作業をして下さい。<br>・PCから離れる場合はブラウザを終了するか、PCをロックして下さい。</p>
    <p class="red weight-bold"><?php echo $msg; ?></p>
                    <div class="main_area">
                    <!-- ログインフォーム開始 -->
                        <form  action="" method="post" name="LI01" id="LI01">
                            <p class="error">
                            
                            </p>
                            <table border="0" cellspacing="1" cellpadding="0" summary="フォーム" class="tlayout">
                                <tr>
                                    <th class="content">ログインID</th>
                                    <td class="content"><input name="login" type="text" /></td>
                                </tr>
                                <tr>
                                    <th class="content">パスワード</th>
                                    <td class="content"><input maxlength="12" name="password" type="password" /></td>
                                </tr>
                                <tr>
                                    <td colspan="2" class="center"><input name="submit" value="ログイン" type="submit" /></td>
                                </tr>
                            </table>
                        </form>
                    <!-- ログインフォーム終了 -->
                    </div>
                    <div class="border"><p><span class="orange weight-bold">[※]ご利用に際して</span><br>・xxxxサイトで使用される全てのデータは目的の用途以外で複製・保存、閲覧することは堅く禁止されています。<br>・xxxxは、個人情報保護法で定められる個人情報取扱事業者です。xxxxバックエンドサイト利用者は、個人情報の不正な利用や漏洩を防ぎ、個人情報データの正確さを保つ義務があります。<br><span class="weight-bold">お問い合わせ先</span><br>会社名: 株式会社xxxx  住所:東京都渋谷区 渋谷<br>この件に対する問い合わせ窓口(担当者): ●●<br>電話: 03-xxxx-xxxx FAX: 03-xxxx-xxxx 電子メール:
 hoge@example.co.jp</p>
                    </div>
                </div>
            </div>
            <div class="clear">
                <p></p>
            </div>
        </div>
    </div>
    <div id="footer">
        <div id="footerbody">
            <p>&nbsp;</p>
        </div>
    </div>
    <div id="copyright">
        <p><a href="http://www.example.co.jp" target="_blank">copyright 2009 (C) All rights reserved,xxxx Inc.</a> </p>
    </div>
</div>

</body>
</html>
account.php
<?php
session_start();
$filepath = '設定データファイル';
$file = file($filepath);
foreach ($file as $value) {
    list($uid,$login_id,$login_pass,$u_name,$e_mail,$create_date,$reload_date) = explode(",",$value);
    if (isset($_SESSION['login_id'])==false) {
        session_destroy();
        header("Location: index.php");
    }
}
if (!empty($_GET['logout'])) {
    session_destroy();
    header("Location: index.php");
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>管理者用ページ &gt; 管理者リスト
</title>
<link href="./css/backend.css" rel="stylesheet" type="text/css" />
<link href="./css/literal.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="layout">
	<!-- ヘッダーメニュー開始 -->
	<div id="header">

		<div id="headerbody">
			<div class="logo">
				<h2>&nbsp;&nbsp;xxxx<br>管理者画面</h2>
			</div>
			<div id="alertmess">
				<p><span class="red weight-bold">[!]ご注意ください</span><br>・アカウント名が、利用者本人のものでないときはログアウトをしてから、再ログインして下さい。<br>・必ず利用者自身のアカウントで作業をして下さい。<br>・PCから離れる場合はブラウザを終了するか、PCをロックして下さい。</p>

			</div>
			<div id="utility">
                                    <p><span class="weight-bold">ユーザー名:</span><?php echo $_SESSION['login_id']; ?> | <a href="?logout=1">ログアウト</a> |</p>
			</div>
		</div>
	</div>
	<!-- ヘッダーメニュー終了 -->

	<!-- グローバルナビ開始 -->
	<div id="globalnavi">
		<div id="globalnavibody">
			<ul>
				<li>
					<div class="width100px"><a href="">掲示板</a></div>
				</li>
				<li class="tabselect">
					<div class="width100px"><a href="">管理者</a></div>
				</li>
			</ul>
		</div>
	</div>
		<div class="localnavi">
		<div class="localnavibody"><img src="./img/spacer.gif" height="5" width="550"></div>
	</div>
	<!-- グローバルナビ終了 -->

	<!-- タブメニュー開始 -->
	<div class="localnavi">
		<div class="localnavibody center">
			<table cellpadding="0" cellspacing="0" class="menutableadmin">
				<tr>
					<td nowrap class="tabselect"><a href="">管理者</a></td>
				</tr>
			</table>
		</div>
	</div>
	<!-- タブメニュー終了 -->

	<!-- コマンドメニュー開始 -->
	<div class="comand">
		<div class="comandbody">
		<p>
		<span><a href="">新規登録</a></span>&nbsp;
		<span class="weight-bold blue"><a href="">一覧</a></span>
		</p>
		</div>
	</div>
	<!-- コマンドメニュー終了 -->

	<!-- パンくず開始 -->
  	<div id="youarehere">
		<div id="youareherebody">
			<p>管理者用ページ &gt; 管理者 &gt; 管理者リスト</p>
		</div>
	</div>
	<!-- パンくず終了 -->

	<div id="contents">
		<div id="contentsbody">
			<div id="main">
				<div id="mainbody">
				    <div class="form">
						<h2>管理者リスト</h2>
					</div>
					<hr>

                                                   総件数;<?php echo count($file); ?><br>

&lt;&lt;
最初
&lt;

|
1&nbsp;|&nbsp;<a href="">2</a>
|
<a href="">次の50件</a>
&gt;
<a href="">最後</a>
&gt;&gt;

<!-- BEGIN table -->
<table class="tlayout" border="0" cellspacing="1" cellpadding="1">
<thead>
 <tr>
  <th class="th">管理者ID</th>
  <th class="th">管理者名</th>
  <th class="th">メールアドレス</th>
  <th class="th">作成日時</th>
  <th class="th">更新日時</th>
  <th class="th">操作</th>
 </tr>
</thead>
<tbody>
<!-- BEGIN rows -->
<?php
        $ckpt = file_get_contents($filepath);
        if (empty($ckpt)) {
            echo '<td class="td" colspan="6">データが登録されていません。</th>';
        } elseif ($file==false) {
            echo '<td class="td" colspan="6">データベースが存在しません。</th>';
        } else {
        foreach ($file as $value) {
        list($uid,$login_id,$login_pass,$u_name,$e_mail,$create_date,$reload_date) = explode(",",$value);
        if($uid%2==0){
            echo '<tr class="odd">';
        } else {
            echo '<tr class="even">';
        }
            echo '<td class="td">'.$uid.'</td>';
            echo '<td class="td">'.$u_name.'</td>';
            echo '<td class="td">'.$e_mail.'</td>';
            echo '<td class="td">'.$create_date.'</td>';
            echo '<td class="td">'.$reload_date.'</td>';
            echo '<td class="td">';
            echo '<form action="">';
            echo '<input type="submit" value="修正" />';
            echo '</form>';
            echo '<form action="">';
            echo '<input type="submit" value="削除" />';
            echo '</form>';
            echo '</td>';
            echo '</tr>';
        }
        }
?>
<!-- END rows -->
</tbody>
</table>
<!-- END table -->

				</div>
			</div>
			<div class="clear">
				<p></p>
			</div>
		</div>

	</div>
		<div id="footer">
		<div id="footerbody">&nbsp;</div>
	</div>
	<div id="copyright">
		<p><a href="http://www.example.co.jp" target="_blank">copyright 2009 (C) All rights reserved,xxxx Inc.</a> </p>
	</div>
	<div class="border"><p><span class="orange weight-bold">>[※]ご利用に際して</span><br>・xxxxサイトで使用される全てのデータは目的の用途以外で複製・保存、閲覧することは堅く禁止されています。<br>・xxxxは、個人情報保護法で定められる個人情報取扱事業者です。xxxxバックエンドサイト利用者は、個人情報の不正な利用や漏洩を防ぎ、個人情報データの正確さを保つ義務があります。<br><span class="weight-bold">お問い合わせ先</span><br>会社名: 株式会社xxxx  住所:東京都渋谷区 渋谷TKビル7F<br>この件に対する問い合わせ窓口(担当者): ●●<br>電話: 03-xxxx-xxxx FAX: 03-xxxx-xxxx 電子メール:
 hoge@example.co.jp</p>

	</div>
</div>
</body>
</html>
設定データファイル
1,crazyup,password1,Crazyup,crazyup@example.com,2009-5-12 19:01,2009-5-12 19:10
2,nippon,password2,Nippon Taro,nippon-taro@example.co.jp,2009-5-12 19:01,2009-5-12 19:10
3,nippon2,password3,Nippon Hanako,nippon-hanako@example.jp,2009-5-12 19:01,2009-5-12 19:10
4,nippon3,password4,Nippon Ichiro,nippon-ichiro@example.net,2009-5-12 19:01,2009-5-12 19:10
現在の問題点
  • ファイルの存在がないとエラーが表示される。