Code to display the login client data

Asked

Viewed 1,239 times

1

Well my question is this, when logging in how I get session data?

I think it’s a select, but when I do it I have to create a session? Because I want to search the data only of this client who logged in.

I already have the login.php done, with sessions, etc, but now I want to create the listar_dados.php (that will only list the data of the client that logged in).

  • What is the structure of the "people table"?

  • Name, date of birth, address, city, country, mail, user pass

3 answers

1

Answering your questions separately:

How I get session data?

Using "pure php", do the following:

$_SESSION['valor']

For example: after the customer logs in, you play the client id in the session

$_SESSION['cliente']['id'] = $cliente->id;
$_SESSION['cliente']['nome'] = $cliente->nome;
// outros valores aqui
$_SESSION['logado'] = true;

How I use session data to find something related to them?

After setting the data in the session, on another page you want to search for something by customer id, then you would do something like this:

$cliente_id = $_SESSION['cliente']['id'];

Now just use the variable $cliente_id where you wish (in your query for example):

$sql = 'SELECT id, placa, modelo, marca, valor 
    FROM veiculos 
    WHERE cliente_id < :cliente_id';

$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->bindParam(':cliente_id', $cliente_id, PDO::PARAM_INT);
$veiculos = $sth->fetchAll();

I used the table vehicles as an example, but in your case may be another.

Any questions leave a comment.


Useful links:

0

Simply SELECT based on the user who is saved in the session created at login.

To read the session, use $_SESSION['valor'] normally.

0

One of the ways to do that would be:

/* lembrando que o correto é você ter `user` únicos, ou seja, sem repetir no banco */
$userLogado = $_SESSION[' nome da variavel que você guardou o usuário '];

$db = new mysqli('localhost', 'usuario', 'senha', 'database_name');

if($db->connect_errno > 0){
    die('Não foi possível se conectar ao servidor [' . $db->connect_error . ']');
}

/* cria sua query */
$stmt = $db->prepare('SELECT * FROM pessoas WHERE user = ?');
$stmt->bind_param('s', $userLogado);

$stmt->execute();

$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    /* dentro das aspas vai o nome da sua coluna no banco */
    echo $row['nome'] . '<br />';
    echo $row['data_de_nascimento'] . '<br />';
    echo $row['cidade'] . '<br />';
}

Always prefer to pass parameters to the query through the bind_param to prevent attacks of SQL Injection.

I suggest you give a studied in the subject, look at tutorials and video lessons that exist in bulk on youtube.

My referral for beginners is this one:

User Registration and Login System #1

Browser other questions tagged

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