LoginSignup
3
1

More than 1 year has passed since last update.

PHPで簡単な会員ログインシステムを作る

Posted at

こんなんで良いのかな?

index.php

<?php
include("logincheck.php");
include('config.php');
session_start();
//ここに会員ページコンテンツを書く

config.php

<?php
$auth_id ="ログインID";
$auth_pass ="ログインパスワード";

function h($str){
    if(is_array($str)){
        return array_map("h",$str);
    }else{
        return htmlspecialchars(stripslashes($str),ENT_QUOTES,"UTF-8");
    }
}

function getSha1Password($s) {
    return (sha1("3k1L7Alyb".$s));
}

logincheck.php

<?php
session_start();
if(empty($_SESSION["login_name"])){
 header("Location:login_auth.php");
 exit;
}

login_auth.php

<?php
include("config.php");
include("functions.php");
session_start();

$error_message=<<<eof
IDとパスワードを入力してログインしてください
eof;

if($_SERVER["REQUEST_METHOD"] == "POST"){
 if($_POST["auth_id"] == $auth_id and $_POST["auth_pass"] == $auth_pass){
  session_regenerate_id(true);
  $random_id = mt_rand(0, 10);
  $_SESSION["login_name"] = getSha1Password($random_id);
  header("Location:index.php");
  exit;
 }else{
  $error_message = "IDもしくはパスワードが間違っています。";
 }
}
?>

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>ログインページ</title>
</head>

<?php echo $error_message; ?>

<form action="" method="post">
ID:<input type="text" name="auth_id" />
パスワード:<input type="text" name="auth_pass" />
<input type="submit" name="submit" value="Login">
</form>

</body>
</html>
3
1
0

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
3
1