-1
我有3頁,login.php,login_validate.php和userhome.php。我的問題是,登錄信息正在正確傳遞給login_validate.php,但似乎消失login_validate重定向到userhome.phpPHP會話變量消失了嗎?
任何幫助,將不勝感激。
的login.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 />";
}
?>
你是否在login_validate.php中的任何地方調用'session_start()'? – Darkzaelus
我不認爲你必須這樣做。 – user1436735
@ user1436735你爲什麼認爲你不必? – xdazz