私が過去数日間立ち往生しているこのログインを少し手に入れることができることを望んでいました。
基本的には、ログインはユーザーにログインしますが、毎回同じページに移動します。私がしたいのは、ユーザーが管理者の場合、そのユーザーを管理者ページに移動することです。ユーザーの場合、ユーザーページへ。
データベースに設定しました。usertypeフィールドがあり、2人のユーザーがハードコーディングされており、1人はユーザータイプとしてadminを使用し、もう1人はuserとして使用しています。
<?php
session_start();
$host="xxxxxxxxxxxxx"; // Host name
$username="xxxxxxx"; // Mysql username
$password="xxxxxxx"; // Mysql password
$db_name="xxxxxxxx"; // Database name
$tbl_name="member"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$username=$_POST['username'];
$password=$_POST['password'];
// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($sql);
$num_results = mysql_num_rows($result);
$array = mysql_fetch_array($result);
$_SESSION['username']=$array['username'];
$_SESSION['password']=$array['password'];
$_SESSION['usertype']=$array['usertype'];
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if($count==1){
$_SESSION['username']=$_POST['username'];
$_SESSION['password']=$_POST['password'];
if ($array['usertype']=="user")
{ header ("location: userpage.php"); }
else if ($array['usertype']=="admin");
{ header ("location: adminpage.php"); }
} else {
echo "Wrong user or password";
}
?>
上記は私が使用した最新のコードです。ログインするたびに、最初のヘッダーをスキップして、2番目のヘッダーに直接移動するようです。ログインした後でセッションを印刷したこともありますが、テーブルからユーザータイプが取得されます。
これを修正する方法についてこれ以上の手がかりはありません。