Grab user id by SESSION

Asked

Viewed 3,935 times

2

I have a question , how do I get the id of the guy who logged in to 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 sessão e vrf se há sessao

 <?php
  session_start();
  function session_checker(){
    if (! isset($_SESSION['login'])){
      header ("Location:../");
      exit(); 
    }
  }
  session_checker();
  ?>
  • <?php if (isset ($_POST ["loginUsuario_external"]){ include_once("./controllers/Usuario_externo_controller.php"); $usuario_external = new Usuario_externo_controller; $usuario_external->login(); }

  • I tried $codigo = mysql_query("SELECT u.id, u.nome FROM usuario_externos u WHERE u.nome = '" . $_SESSION['nome'] . "';"); &#xA;echo mysql_result($codigo, 0);

  • let me get this straight, you want to store in the session this user ID coming from the database, and then recover this session with the ID to change the data ?

  • that :) but it’s not working out at all

  • You need to edit the question and put the code that starts the session and login. we don’t know what login() does, not even if you did the assignment in $_SESSION or if you signed in to the other file.

  • I don’t understand what that line does, $this->usuario_externos->email($this->input->get('email'))&#xA; ->senha($this->input->get('senha')); you are passing a password by get? in the login of your model see if email and password have any value.

  • 1

    yes they are coming from view , get

  • print_r($stmt->fetch()) displays something? in the email bind, change : PDO::PARAM_INT for PDO::PARAM_STR or leave with nothing.

  • like if I put in the controller $id=$_SESSION['id']; it was to catch the id’s carrion ? or not?

  • print_r($stmt->fetch()) brought stdClass Object ( [id] => 1 [nome] => marcus [cpf] => [email] => [email protected] [perfil] => 1 [senha] => 6fab33feda475044 [status] => 1 [instituicao_ext] => unie )

  • Yes it is to get the id, must be $_SESSION['login']['id'] in the doubt of a print_r($_SESSION).

  • Fatal error: Cannot use object of type stdClass as array in C:\xampp\htdocs\portoaberto\app\controllers\Usuario_Externo_Controller.php on line 45 PQ???? ehwaaeauw only error ahweauw

  • 1

    Change, to $_SESSION['login']->id, if you want to access as an array, do return $stmt->fetch(PDO::FETCH_ASSOC);

  • rray you and the bixao vei ---------------- put in the answers that worked --

Show 9 more comments

2 answers

2


The problem seems to be access to session, by the comments it was seen that the return of login() model return an object and played it in 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 in the fetch(). return $stmt->fetch(PDO::FETCH_ASSOC);

DO NOT pass sensitive information such as a password by the get method, as it will be visible at the url, you practically offer the gold to the bandit.

  • 2

    Thank you very much, solved a major impediment to my project .

0

Try this way using session_id()

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

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

?>
  • i tried that , however it appears session id and not user id that logged in session.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.