dragon0118 2013-12-01 18:37
浏览 48
已采纳

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.

展开全部

  • 写回答

1条回答 默认 最新

  • dongqi9125 2013-12-01 18:49
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部