Show user logged in php

1

Greetings, I have a basic log system, but I want you to show who is logged in, here is my code:

<?php
/* start the session */

session_start();
?>

<!DOCTYPE html>
<html lang="en">

<head>
 <title>Check Login</title>
 <meta charset = "utf8" />
</head>


<body>

<?php

 $host_db = "localhost";
 $user_db = "root";
 $pass_db = "****";
 $db_name = "****";
 $tbl_name = "usuarios";

// Connect to server and select databse.
mysql_connect("$host_db", "$user_db", "$pass_db")or die("Cannot Connect to Data Base.");

mysql_select_db("$db_name")or die("Cannot Select Data Base");

// data enviada desde el formulario
$username = $_POST['username'];
$password = $_POST['password'];

$sql= "SELECT*FROM $tbl_name WHERE usuario='$username' and password='$password'";

$result=mysql_query($sql);

// counting table row
$count = mysql_num_rows($result);
// If result matched $username and $password

if($count == 1){

 $_SESSION['loggedin'] = true;
 $_SESSION['username'] = $username;
 $_SESSION['start'] = time();
 $_SESSION['expire'] = $_SESSION['start'] + (30 * 60) ;

 header('location: inicio.php');

}
 else {
 echo "Usuario o contraseña estan incorrectos.";

 echo "<a href='index.php'>Volver a Intentarlo</a>"; 
}

?>

</body>
</html>

And this is the page after the data is validated, this is where I want the name of the loqueado user to appear:

<?php 
session_start();

if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
	
	

} else {
   echo "Esta pagina es solo para usuarios registrados.<br>";
   echo "<br><a href='index.php'>Login</a>";
   

exit;
}?>
    
asked by Leonardo Rodríguez 26.01.2018 в 23:19
source

1 answer

0

You should only print the value of username that you saved in the previous form as a session variable. In your second form you place:

echo $_SESSION['username']; // Imprimes el usuario que esta logueado.

Here your second form with the extra code line where you should go so you can see the username that you logged in the system:

<?php 
session_start();

if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {

    echo $_SESSION['username']; // Imprimes el usuario que esta logueado.

} else {
   echo "Esta pagina es solo para usuarios registrados.<br>";
   echo "<br><a href='index.php'>Login</a>";


exit;
}?>
    
answered by 27.01.2018 / 01:17
source