Help - Search - Members - Calendar
Full Version: If Statement With Sessions
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
MrTouz
Hello !

I thought i asked already... but can't find it.

I have a login script... members profile and i need 2 things to complete it. One hard... One easy (i believe).

The first is the IF statement. The thing is, i want to show certain links if my member is not logged in... and certain if he is ! for example :

(not logged in)
Home
About
Sign In
...

(logged in)
Home
About
My Account
Messages
Signout
...

I thought of the if statement like

if (session.... and if the session is started than show these links... if session is closed... than show the other links ! I was told using sessions is best so that is why i am using them right now...

______

The second thing is... ok i am done with my script i have secured some pages that ask if the session is started than yes it shows the pages if not than it redirects the user to the login page...
Now i have that set... i want him to have an account where he can change his data... i know how to retrieve data from a mysql database which is not a problem, the problem is... how to tell the script to retrieve data from this specific user ?

I was thinking of messing around with sessions... if(!isset($_SESSION['SESS_MEMBER_ID']) so it selects only the user session's and show his data... not sure where to start or what to do...

I need enlightenment... really ... i do...

Thanks.
swordz
When your user logs in, do session_start(), then set $_SESSION['user'] to that user's unique id, which you should have anyway. This then solves both questions.

For Q1, use if(isset($_SESSION['user'])) to determine if the user should see the extra pages. For Q2, do SELECT * WHERE `id`='$_SESSION['user']', with the obvious alterations to make it fit your script.

Hope this makes sense, post back if it doesn't.

swordz
MrTouz
I think you solved my question for Q2 which really makes sense.. i did not know i could do that.

For the number 1, can you help me out writting it up ? give me an example ? should i use the if (isset.. than an echo""; ?
swordz
As luck would have it, I have this ready prepared...

CODE
<?php if(isset($er)) { ?>
    <tr>
        <td>Please fix the following errors:</td><td colspan="2">
            <ul>
            </ul>
        </td>
</tr>
<?php } ?>


Note the opening and closing {/}. Echo would also work if you don't mind staying in php code, but that's better for large html blocks. Here $er is the equivalent of $_SESSION['user'].

swordz
peck68
Yeah use isset() - it works for every variable.

CODE
<?php
echo "Home<br />";
echo "About<br />";

if(!isset($_SESSION['login'])){ # Not logged in
echo "Sign in<br />";
}
else{ # Logged in
echo "My account<br />";
echo "Messages<br />";
echo "Sign out<br />";
}
?>
MrTouz
Thank you very much both.

It has been fixed. I used

CODE
if($session->logged_in){
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.