Code to show the data of the client that logged in

1

Well my question is this, when I log in how do I get the session data?

I think it's a select, but in doing so I have to create a session? Because I want to fetch the data only from this client that logged in.

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

    
asked by anonymous 10.06.2015 / 22:59

3 answers

1

Answering your questions separately:

How do I get session data?

Using "pure php", do the following:

$_SESSION['valor']

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

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

How do I use session data to search for something related to them?

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

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

Now just use the $cliente_id variable where you want it (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 vehicle table as an example, but in your case it could be another.

Any questions leave a comment.

Useful links:

11.06.2015 / 04:10
0

Just do the SELECT based on the user that is saved in the session created at the time of login.

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

    
11.06.2015 / 01:01
0

One way to do this 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 bind_param to avoid attacks of SQL Injection .

I suggest you take a look at the subject, look at tutorials and video lessons that exist on the mountain on YouTube.

My entry for beginners is this one:

System of Registration and User Login # 1

    
11.06.2015 / 01:19