Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

ログイン処理からページ遷移まで、ログイン処理その3「PHP」

 
   こんにちは、前回の「送信の続きからデータベース接続」に引き続き
今回は、ログイン処理からページ遷移をやっていこうと思います。
これは、前回までのです。
送信続きからデータベース接続まで、ログイン処理その2「PHP」
それでは前回のを基盤に進んでいきます。

<!DOCTYPE html>
<html lang=“ja”>
<head>
    <meta charset=“UTF-8”>
    <title>Document</title>
</head>
<body>
   <?php

if(isset($_POST[‘hid’])){
    if($_POST[‘id’] && $_POST[‘pw’]){

   $id = $_POST[‘id’];
   $pw = $_POST[‘pw’];

    $username=“ユーザー名”;
    $password=“パスワード”;
    $dbname=“データベース名”;
    $mysqli = new mysqli(‘ホストかIP’,$username,$password);

    $mysqli->set_charset(‘utf8’);

if($mysqli-> connect_errno){
    print(‘<p>データベースへの接続に失敗しました。</p>’.$mysqli ->
    connect_error);
    exit();
}$db_selected = $mysqli->select_db($dbname);
if (!$db_selected){
    die(‘データベース選択失敗です。’.mysql_error());
}
    $sql = SELECT * FROM テーブル名;
    $stmt = $mysqli ->query($sql);
}else{
        echo “ID、パスワードを入力してください”;
}
?>
<h1>ログイン画面</h1>
<hr>
<br>
    <form action=“” method=“POST”>
        <input type=“hidden” name=“hid” value=“hid”/>
        ID:<input type=“text” name=“id” value=“”><br>
        PW:<input type=“password” name=“pw” value=“”>
        <input type=“submit” value=“ログイン”>
        </form>
</body>
</html>

ではここからですね

Foreach文入れる!

foreach($stmt as $row){
if($id==$row[‘カラム名’] && $pw==$row[‘カラム名’]){
}}

こちらはパスワードとIDが一致したときの処理の中に入れましょう。
こちらの処理では
入力されたID,PWとカラム内の値と一致したら
の処理をしています。
要は、IDかつパスワードが一致したときのみの実行するための
前準備です。
データベースのテーブル内のカラム名です。
シングルクォートで囲みましょう。
次に
SESSIONを使います!

 
こちらの処理では、
セッションとは、コンピュータのサーバー側に一時的にデータを保存

を行っています。
つまり、別のページにとんでもその変数のデータを覚えていたりするわけです。
cookieによく似てますが、cookieはブラウザに保存します
なのでセキュリティ的にはセッションのほうが安全です。

<?php
session_start();
    ?>

まず、こちらを入力します。
入力するところに注意が必要で
HTMLの外に書きます。。
一行目に書きます。つまり

<!DOCTYPE html>

の上に書くということです。
セッションしたいものを先ほどの中に入れてもいいのですが、
僕はパスワードとIDが一致したときの処理の中に書いていきます。

$_SESSION[‘id’]=$row[‘カラム名’];
$_SESSION[‘pw’]=$row[‘カラム名’];
$_SESSION[‘name’]=$row[‘カラム名’];

$_SESSION[‘OK’]=“OK”;

一応今回持っていくデータは、
Idとパスワード、名前、と「OK」です。
「OK」は正直いらないですが、確認のために持っています。
次にページ遷移の処理

を書いていきます。

header(“Location:遷移先.php”);
exit();

このように、書きます。
exit();は正直書いても書かなくてもこの処理の場合変わらないので
どちらでもいいです。
では、こちらのPHPファイルは完成したので
完全版を貼ります!

まとめ


<?php
session_start();

    ?>

<!DOCTYPE html>
<html lang=“ja”>
<head>
    <meta charset=“UTF-8”>
    <title>Document</title>
</head>
<body>
   <?php

if(isset($_POST[‘hid’])){
    if($_POST[‘id’] && $_POST[‘pw’]){

    $id = $_POST[‘id’];
    $pw = $_POST[‘pw’];
    $username=“ユーザー名”;
    $password=“パスワード”;
    $dbname=“データベース名”;
    $mysqli = new mysqli(‘ホストかIP’,$username,$password);

    $mysqli->set_charset(‘utf8’);

if($mysqli-> connect_errno){
    print(‘<p>データベースへの接続に失敗しました。</p>’.$mysqli ->
    connect_error);
    exit();
}$db_selected = $mysqli->select_db($dbname);
if (!$db_selected){
    die(‘データベース選択失敗です。’.mysql_error());
}
    $sql = SELECT * FROM テーブル名;
 
    $stmt = $mysqli ->query($sql);
    foreach($stmt as $row){
if($id==$row[‘カラム名’] && $pw==$row[‘カラム名’]){

$_SESSION[‘id’]=$row[‘カラム名’];
$_SESSION[‘pw’]=$row[‘カラム名’];
$_SESSION[‘name’]=$row[‘カラム名’];

$_SESSION[‘OK’]=“OK”;

header(“Location:loginok.php”);
exit();

}
    }

}else{
        echo “ID、パスワードを入力してください”;
}
?>
<h1>ログイン画面</h1>
<hr>
<br>
    <form action=“” method=“POST”>
        <input type=“hidden” name=“hid” value=“hid”/>
        ID:<input type=“text” name=“id” value=“”><br>
        PW:<input type=“password” name=“pw” value=“”>
        <input type=“submit” value=“ログイン”>
        </form>
</body>
</html>

ではこれでページ遷移はできるので、次回はページ遷移先の
設定をしていきます!

ではありがとうございます!

コメント

スポンサーリンク
スポンサーリンク
タイトルとURLをコピーしました