Jump to content

If session active, Else...


Lynx992

Recommended Posts

Hello guys, before I start I'm a relativity new user of PHP so this really could be something extremely simple, I just can't seem to find it, Basically on my website i have php login script, when you're logged on every page you visit checks a file called auth.php, This will tell create a session called, "$SESSION" Inventive i know... That will tell the browser if the user is logged in or not, so it can decide whether or not to create a Login button, or a Log out button, Here's the code i have so far.

 

if ( $SESSION == '' ) {

echo "<a href='logout.php" ."Log out";

} else {

echo "<a href='login-form.php" ."Log in";

}

 

Now, I believe i'm on the right lines? But i'm not fully sure how to basically in English say, If that session is working or exists, Then do this, if not, do this.

 

I mention again, I'm very new to PHP so any help would be very appreciated!

 

 

 

Link to comment
Share on other sites

in order to access any session variables on a page, you have to first activate that session on each page that you want to access it.

 

session_start();

 

now if you have logged in someone successfully already, lets assume they have a value in the session to confirm this, for instance $_SESSION['logged'] = true;

 

so when it comes to the button bit, make sure the page has the session start bit, then do the if statement like this

if($_SESSION['logged'])
{
echo 'logout';
}
else
{
echo 'login'
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.