dougou3871 2014-05-23 01:52
浏览 17
已采纳

会话ID未设置但页面登录

I am trying to write a login/session page for the first time in oop style. When I run the script below, the page redirects, but when i try to echo session id, it does nothing shows. Am I doing this the right way?

<?php
      session_start();
require_once('classes/function.php');
require_once('classes/user.php');

class Session{

    private $is_logged_in = FALSE;
    private $id ;
Public $ user_data;
    private $email ;

public function logout(){
    session_start();
    unset($_SESSION['id']);
    unset($this->id);
    session_destroy();
    $is_logged_in = FALSE;
    Misc::redirect('index.php');
}

public function is_logged_in(){
    return $this->is_logged_in ;
}

public function login($email, $password){
    global $user;
    global $misc;

    if($misc->check_form()){
        echo $this->error;
    }
    if (!MIsc::check_email_format($email)) {
            echo "Email format is invalid";
          }
          $login_user = $user->authenticate($email, $password);
          if($login_user){
            $_SESSION['id'] = $this->id = $this->user_data['id'];
            $_SESSION['email'] = $this->email = $this->user_data['email'];
            $is_logged_in = TRUE;
            Misc::redirect('127.0.0.1/users/index.php');
          }
}
}

$session = new Session;
?>

my login page looks like this

<?php
require_once('classes/user.php');
require_once('classes/session.php');
require_once('classes/function.php');

if(isset($_POST['submit'])){
    try{
    $email =$_POST['email'];
    $password = $_POST['password']; 
    $user = new Users;
     $user->authenticate($email , $password);
    $login = new session;
    $login->login($email , $password);
}
 catch (Exception $e){
    $error = new Errors();
    echo "<b>".$error->displayError($e)."</b>";
}
}

?>

$user_data is gotten from these methods

  public function user_exist($email){
  global $db;
  $stmt = $db->dbh->prepare("SELECT id, password, email FROM $this->table WHERE `email`= :email LIMIT 1");
  $stmt->bindValue(':email', $email);
  $stmt->execute();
  if($stmt->rowCount() == 1) {
     $this->user_data = $stmt->fetch();
     return TRUE;
  } else {
     throw new Exception('User do not exist');
  }
}

  /*
  what does this method do
*/
 public function authenticate($email, $password){
   global $db;
  $auth = $this->user_exist($email);
  if($auth) {
     if(password_verify($password, $this->user_data['password'])) {
       return  $this->user_data;
     } else {
        throw new Exception('Email/Password combination incorrect');
     }
  }
 }
  • 写回答

1条回答 默认 最新

  • dougang8233 2014-05-23 02:07
    关注

    Probably you are wrong in this section..

         $login_user = $user->authenticate($email, $password);
          if($login_user){
            $_SESSION['id'] = $this->id = $this->user_data['id'];
            $_SESSION['email'] = $this->email = $this->user_data['email'];
            $is_logged_in = TRUE;
            Misc::redirect('127.0.0.1/users/index.php');
          }
    

    I can see that $user_data is not declared in class you should declare it before using it.

    Another issue i see in your oop style

     public function authenticate($email, $password){
       global $db;//you should have $db declared in class, SInce it is not used here you should remove
      $auth = $this->user_exist($email); //Why you are assigning it to $auth you should directly place in condition if you need it once
      if($auth) {
         if(password_verify($password, $this->user_data['password'])) {  //You already have $password variable
           return  $this->user_data; //you can return true instead of returning object which you are not reusing.
         } else {
            throw new Exception('Email/Password combination incorrect'); //i like it :)
         }
      }
    

    You should also enable error reporting to check the issue

    ini_set('display_startup_errors',1);
    ini_set('display_errors',1);
    error_reporting(-1);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据