我有一个这是一个登录屏幕的PHP,然后进入个人资料页面,在那里假设回应用户;用户名和电子邮件在屏幕上。我没有改变任何东西,但现在只打印出用户名。
这是登录php:
public function Login($username, $password){
if(!empty($username) && !empty($password)){
$stmt = $this->db->prepare("SELECT username, password, email FROM users WHERE BINARY username = ? AND BINARY password = ?");
$stmt->bindParam(1,$username);
$stmt->bindParam(2,$password);
$stmt->execute();
if($stmt->rowCount() == 1){
$_SESSION['username'] = $username;
$email = $stmt->fetchColumn(2);
$email = $_SESSION['email'];
header('Location: http://www.mywebsite.com/dev/profile.php');
}else{
echo "Incorrect username or password please try again.";
}
}else{
echo "Must type username or password.";
}
}
这是个人资料php:
<?php
session_start();
$username = $_SESSION["username"];
$email = $_SESSION["email"];
if(!$_SESSION["username"]){
header("Location: http://www.mywebsitelogin.com/dev/");
}
这就是我如何回应它:
<?php echo '<h1>'.htmlentities($username).'</h1>'; ?>
<?php echo '<h3>'.htmlentities($email).'</h3>'; ?>
另外一个侧面问题是,除了include_once()之外,还有一种更常量的方法可以将文件包含在php中。因为在刷新或进入页面时,部分php代码无故失败。
答案 0 :(得分:1)
您没有将电子邮件放入会话中。你正在做相反的事情:
$email = $stmt->fetchColumn(2);
$email = $_SESSION['email'];
将其更改为:
$_SESSION['email'] = $stmt->fetchColumn(2);
也许它之前似乎有效,因为在调试过程中,您已经将电子邮件变量存储在会话中。