我试图在会话变量中设置值并尝试访问另一个php文件 但我得到未定义的索引错误,但使用的变量是相同的。第一个php文件中的代码是
<html>
<head>
login
</head>
<body>
<?php
if(isset($_POST['name']))
{
//connecting to data base
$con=mysqli_connect("localhost:3306","root","","recommender");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// username and password sent from form
$myusername=$_POST['name'];
$mypassword=$_POST['password'];
// 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);
echo $myusername."<br>";
echo $mypassword."<br>";
$sql="SELECT * FROM userlog WHERE name='$myusername' and password='$mypassword'";
$result = mysqli_query($con,$sql) or die(mysqli_error());
// Mysql_num_row is counting table row
$count=mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
session_start();
$_SESSION['user'] = $myusername;
var_dump($_SESSION);
header("location: http://127.0.0.1/proj/dele.html");
}
else {
header("location: http://127.0.0.1/proj/home.html");
}
}
else
{
echo "not set";
}
?>
</body>
</html>
第二个php文件是
<html>
<head>
<title>simply</title>
</head>
<body>
<?php
session_start();
print $_SESSION['user'];
var_dump($_SESSION);
?>
</body>
</html>