dragon0118 2013-12-02 02: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-02 02: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.

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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题