Get userid by SESSION

2

I have a question, how do I get the id of the guy who logged in on my system? So that later he can change his data.

<?php
    if (isset ($_POST ["loginUsuario_externo"])){
        include_once("../controllers/Usuario_Externo_Controller.php");
        $usuario_externo = new Usuario_externo_Controller;
        $usuario_externo->login();
    }

Login function

public function login() {

    $this->usuario_externos->email($this->input->get('email'))
                           ->senha($this->input->get('senha'));

    $resultado = $this->usuario_externos->login();


    if($resultado) {
      session_start();
      $_SESSION['login'] = $resultado;
      echo '<script>window.location = "../views/externo/";</script>';
      echo ('Aguarde, redirecionando.');
    } else {
        echo ('E-mail ou Senha incorreto(os).');
    }
}

MODEL

  public function login() {

    $sql  = "SELECT * FROM usuario WHERE email = :email AND senha = :senha AND status = 1 LIMIT 1";
    $stmt = $this->conn->prepare($sql);
    $stmt->bindParam(':email', $this->email, PDO::PARAM_INT);
    $stmt->bindParam(':senha', $this->senha, PDO::PARAM_STR);
     $stmt->execute();
    return $stmt->fetch();

}

starta session and vrf if there is session

 <?php
  session_start();
  function session_checker(){
    if (! isset($_SESSION['login'])){
      header ("Location:../");
      exit(); 
    }
  }
  session_checker();
  ?>
    
asked by anonymous 29.10.2015 / 14:21

2 answers

2

The problem seems to be session access, by commenting it was seen that the return of the login() model return an object and threw it in the session it should be accessed so $_SESSION['login']->id .

If you want to access user information in the session as an array, set the return type to fetch() . return $stmt->fetch(PDO::FETCH_ASSOC);

Do not pass sensitive information as a password by the get method, as it will be visible in the url, practically you offer the gold to the bandit.

    
29.10.2015 / 15:49
0

Try this way, using session_id ()

<?php
session_start();
$r=session_id();

echo "o id da sessão é: ".$r;

?>
    
29.10.2015 / 14:24