Php会话回声不再显示了?

问题描述:

I have this php that is a login screen then goes to the profile page, and there it is suppose to echo out the users; username and email on the screen. I had it working before haven't changed anything but now only the username prints out.

This is the login 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.";
    }       
}

This is the profile php :

<?php 
    session_start();
    $username = $_SESSION["username"];
    $email = $_SESSION["email"];
    if(!$_SESSION["username"]){
        header("Location: http://www.mywebsitelogin.com/dev/");
}

And this is how im echoing it out:

<?php echo '<h1>'.htmlentities($username).'</h1>'; ?>
<?php echo '<h3>'.htmlentities($email).'</h3>'; ?>

Also on a side question is there a more constant way of including a file inside a php other than include_once(). Because it's happened where that part of the php code fails for no reason when refreshing or entering a page.

You're not putting the email into the session. You're doing the opposite:

$email = $stmt->fetchColumn(2);
$email = $_SESSION['email'];

Change that to:

$_SESSION['email'] = $stmt->fetchColumn(2);

Perhaps it appeared to work before because during debugging, you had already stored the email variable in the session.