我有3页,login.php,login_validate.php和userhome.php。我的问题是登录信息正确传递给login_validate.php,但是当login_validate重定向到userhome.php时似乎消失了
任何帮助都将不胜感激。
的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 />";
}
?>
答案 0 :(得分:2)
您需要在每个使用会话的脚本中(以及在发送标头之前)调用session_start()。