Save user data to session after login

Asked

Viewed 518 times

2

On the login screen I make the query to free the access to the system, after that I would like to make a complete query of the data of that user and to write in the SESSION.

    <?php
    // inclui o arquivo de inicialização
    require 'init.php';

    // resgata variáveis do formulário
    $email = isset( $_POST[ 'email' ] ) ? $_POST[ 'email' ] : '';
    $telefone = isset( $_POST[ 'telefone' ] ) ? $_POST[ 'telefone' ] : '';
    if ( empty( $email ) || empty( $telefone ) ) {
      echo "Informe Email e Senha corretamente.";
      exit;
    }

    // cria o hash da senha
    $telefoneHash = make_hash( $telefone );

    $PDO = db_connect();
    $sql = "SELECT id, name AND email FROM users WHERE email = :email AND telefone = :telefone ";
    $stmt = $PDO->prepare( $sql );
    $stmt->bindParam( ':email', $email );
    $stmt->bindParam( ':telefone', $telefoneHash );
    $stmt->execute();
    $users = $stmt->fetchAll( PDO::FETCH_ASSOC );
    if ( count( $users ) <= 0 ) {
      echo "Email ou Senha incorretos";
      exit;
    }

    // pega o primeiro usuário
    $user = $users[ 0 ];
    session_start();
    $_SESSION[ 'logged_in' ] = true;
    $_SESSION[ 'user_id' ] = $user[ 'id' ];
    $_SESSION[ 'user_name' ] = $user[ 'name' ];

    //AQUI É QUE PRECISO PREENCHER
    //$_SESSION['user_endereco'] =  $variavelbanco;
    //$_SESSION['user_numero'] =  $variavel2banco;
    ?>
  • Did I understand if you want to record your table data in a Session? if that’s why?

  • That’s right, I actually think I’m going to switch from Session to localStorage, is that I already have some data there as the user name, what I don’t understand is about the query and putting the data into variables. I researched a lot but I do not understand anything of connection with the bank, nothing comes out.

  • So if you need to stop and start from scratch (it’s a tip don’t get me wrong) because localStorage is something else ... !!!!

  • You can use json_encode and then save both in Session and Localstorage. Ps.: Depending on the goal, it is not the best option.

  • I’m using it, few user information that are Session, the shopping cart and other information are on locationStorage.

  • Yes, my problem lies more on the part of bringing the database data and passing it to variables

  • This information (which you want to add to the address and number sessions) is in the users table?

  • Yes, I need to get this information: name email telefone endereco numero bairro cidade

  • @Hugonascement use "@" to signal someone. Thus, the comment appears in the box of the person you are talking to. =)

Show 4 more comments

1 answer

1


First you change your select:

$sql = "SELECT id, name, email, telefone, endereco, numero, bairro, cidade FROM users WHERE email = :email AND telefone = :telefone ";

Then you fill in:

    $user = $users[ 0 ];
    session_start();
    $_SESSION[ 'logged_in' ] = true;
    $_SESSION[ 'user_id' ] = $user[ 'id' ];
    $_SESSION[ 'user_name' ] = $user[ 'name' ];

    //AQUI É QUE PRECISO PREENCHER
    $_SESSION['user_endereco'] =  $user[ 'endereco' ]." ".$user[ 'numero' ]." - ".$user[ 'bairro' ]." - ".$user[ 'cidade' ];
    $_SESSION['user_numero'] =  $user[ 'telefone' ];

Browser other questions tagged

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