login.php、login_validate.php、userhome.php の 3 つのページがあります。私の問題は、ログイン情報が login_validate.php に正しく渡されているが、login_validate が userhome.php にリダイレクトされると消えてしまうことです。
どんな助けでも大歓迎です。
ログイン.php
<form name="login" action="login_validate.php" method="post">
Username: <input type="text" name="myusername" /><br />
Password: <input type="password" name="mypassword" /><br />
<input type="submit" value="Login" />
</form>
login_validate.php (長いですね)
<?php
include'connect.php';
// username and password sent from form
if(isset($_POST['myusername']) and isset($_POST['mypassword'])){
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
}else{
header('Location:login.php?failatvalidate');
}
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$mypassword = hash('sha256', $mypassword);
$sql="SELECT * FROM users WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "userhome.php"
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
print '<pre>';
var_dump($_SESSION);
print '</pre>';
echo "<a href='userhome.php'>Home</a>";
}
else {
echo "Wrong Username or Password";
}
?>
userhome.php
<?php
error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors',1);
session_start();
if(!isset($_SESSION['myusername'])){
echo "error :( ";
}else{
echo "Logged in as ".$_SESSION['myusername']."      <a href='logout.php'>Log out</a><br /><br />";
}
?>