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 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型