0

Here is how I am setting the session. Here I am pasting my header.php and here is my action_login.php

if(session_id() == ''){
    session_start();
    $_SESSION["state_login"]=true;
    $_SESSION["realname"]=$row['realname'];
    $_SESSION["username"]=$row['username'];
    
    if($row["type"]==0){
        $_SESSION["user_type"]="public";
    }
        elseif($row["type"]==1){
        $_SESSION["user_type"]="admin";
    }
}

and checking in my header that if session is true then login and replace link logout as:

<?php
session_start();
if(isset($_SESSION["state_login"])&& $_SESSION["state_login"]===true && $_SESSION["user_type"]=="admin"){
?>

Why my code is not working.

Pippo
  • 2,173
  • 2
  • 3
  • 16
  • What do you mean by "not working"? – GrumpyCrouton Jun 12 '23 at 14:10
  • In my header file `if(isset($_SESSION["state_login"])&& $_SESSION["state_login"]===true && $_SESSION["user_type"]=="admin"){` this code is not working – Java_Beginner Jun 12 '23 at 14:17
  • Have you done any debugging? `var_dump($_SESSION["state_login"], $_SESSION["user_type"])` – user3783243 Jun 12 '23 at 14:29
  • `user_type` also would only be set if you don't have a session.. maybe you meant to close the `if` or add an `else`? – user3783243 Jun 12 '23 at 14:30
  • @user3783243 `var_dump` is not printing anything – Java_Beginner Jun 12 '23 at 14:56
  • `print_r($SESSION);` is not printing anything. its blank – Java_Beginner Jun 12 '23 at 15:07
  • @user3783243 I completely copy pasted my 2 php files in my question that have problem – Java_Beginner Jun 12 '23 at 15:28
  • You can't have `session_start();` in the middle of a page. `To use cookie-based sessions, session_start() must be called before outputting anything to the browser.` – user3783243 Jun 12 '23 at 15:35
  • `var_dump` and `print_r` not outputting anything would indicate you are not calling the code blocks where that was added. – user3783243 Jun 12 '23 at 15:36
  • Do you have your [errors set to be visible](https://stackoverflow.com/q/1053424/1941241)? Since you include `header.php` before calling `session_start()` and that file outputs a whole lot of HTML before doing anything PHP related, your `session_start()` should do nothing except issue a "headers already sent" warning and whatever you write into `$_SESSION` will not be persisted to the next page. – rickdenhaan Jun 12 '23 at 16:53

0 Answers0