LoginSignup
2
1

More than 5 years have passed since last update.

チラ裏: phpでログイン認証

Last updated at Posted at 2017-03-06

今度読む:

index.php(作りかけ)
<?php
$IP=$_SERVER["REMOTE_ADDR"];
$UA=$_SERVER["HTTP_USER_AGENT"];
$TIME=$_SERVER["REQUEST_TIME"];

// ログインページを表示してから$allow_seconds秒後までにSUBMITされたらOK
$allow_seconds = 3600;
if(($_POST["time"] + $allow_seconds) < $TIME) {
    echo sprintf("NOW: %d<br>PST: %d", $TIME, $_POST["time"] + $allow_seconds);
    echo "login time over.";
}

// 送信元IP
if($_POST["ip"] != $IP) {
    echo sprintf("NOW: %s, POST: %s", $IP, $_POST["ip"]);
    echo "login failed. ip";
}

// UserAgent
if($_POST["ua"] != $UA) {
    echo sprintf("NOW: %s, POST: %s", $UA, $_POST["ua"]);
    echo "login failed. ua";
}

// 許可するUserAgent
$w3m = "/w3m/";
//$w3m = "/Gecko/i";
//$w3m = "/ msie /i";
if(!preg_match($w3m, $UA)) {
    echo sprintf($UA);
    die("login failed. ua not in list");
}

// user:pass
if($_POST["ip"] == $IP) {
    if($_POST["username"] == "user1"
    && $_POST["password"] == "pass1") {
        echo "login success.";
        header('Location: mypage.php');
        exit;
    } else {
        echo "login failed.user:pass";
    }
}

echo "現在時刻:";
echo date("Y-m-d H:i:s", $_SERVER["REQUEST_TIME"]);
?>
<form method="post">
<input type="hidden" name="ip" value="<?php echo $IP ?>">
<input type="hidden" name="ua" value="<?php echo $UA ?>">
<input type="hidden" name="time" value="<?php echo $TIME ?>">
<li>user: <input type="text" name="username">
<li>pass: <input type="password" name="password">
<input type="submit" value="login">
</form>
mypage.php
hi
2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1