Home > Software design >  How can i stop $_SESSION from going to the login page?
How can i stop $_SESSION from going to the login page?

Time:02-12

I am building a php login form, but the problem is the auth session that forces visitors to go to a login page which i don't want that, is it possible to just leave a message if the user is not logged in like "Hi user you are not currently logged in" please see the attached code:

<?php
session_start();
if(!isset($_SESSION["username"])){
header("Location: login.php");
exit(); }
?>

CodePudding user response:

Rather than calling the header method to redirect the user what you should do is this:

<?php
    session_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <?php
            if( !isset( $_SESSION['username'] ) ){
                echo 'You are not logged in';
            }else{
                echo 'You are logged in as ' . $_SESSION['username']
            }
        ?>
    </body>
</html>

You need to keep the session_start() throughout your site if you wish to maintain sessions but rather than redirect the user if the session does not exist you can instead, from within the body section of the HTML to be valid, echo a message based upon whether the session does or does not exist.

CodePudding user response:

this is what i get when i logout enter image description here

CodePudding user response:

Wherever you echo $username, before that check condition - if( isset( $_SESSION['username'] ) )

  •  Tags:  
  • php
  • Related