简体   繁体   中英

How to check whether to show the login form or [logged in] user details?

I want to show two different elements depending on whether a user has logged in or not. If they are not logged in I will show one element with include "./navn.PHP"; , and if they are include "./navl.PHP";

I have the following code:

require_once("connection.php");
session_start();
$id = $_SESSION['user_login'];

$select_stmt = $db->prepare("SELECT * FROM tbl_user WHERE id = :uid");
$select_stmt->execute(array(':uid' => $id));
$row = $select_stmt->fetch(PDO::FETCH_ASSOC);

if (empty($row["username"]))  {
    include "./navn.php";
} else {
    include "./navl.php";
}

But my solution does not seem to work -- I log in and it shows the register and login buttons.

Assuming that you need to be logged to have the $_SESSION['user_login']

just use

if(!$_SESSION['user_login']){
   include "./navn.php";
}else{
   include "./navl.php";
}

or create a function

function isLogged($id){
   //using mysqli procedure
   global $db;
   $query = $db->query("SELECT * FROM tbl_user WHERE id = $id");
   $data = $query->fetch_assoc();
   if(!$data["username"]){
      return 0;
   }else{
      return 1;
   }

}

if(isLogged($_SESSION['user_login']) == 0){
   include "./navn.php";
}else{
   include "./navl.php";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM