PDA

View Full Version : Dynamically set navigation bar


deafboyzaudio
11-30-2009, 06:31 PM
I want my nav bar to have a 'logout' button and a 'my garage' button if the user is logged in.

I know this can be done with a if (????? == '?????'){
echo '<li><a href="myGarage.php">My Garage</a></li>'
};I just don't know what syntax to use for the ???? marks

it would have to be something saying if "user stutus == logged in" but im not sure exactly

can someone fill me in?

edbr
12-01-2009, 12:42 AM
are you using sessions? it will likely be $_session['user'] ==$row['user']

$row substituted for you own variable name

deafboyzaudio
12-07-2009, 10:44 PM
i keep getting a Undefined variable: _SESSION error and a Undefined index: USER error depending what page im on. this is the code i have:
<?php $thissession = ($_SESSION['USER']);
<li><?php if ($_SESSION['USER'] == $thissession['user']) {echo 'logout';} else{echo 'login';}?></li>i wasnt sure how to make the comparision between the 2 variables so i figures if i made $thissession be $_session['user'] then if the user was logged in via a session then they would match... is this right? if not what should my $thissession variable be set to?

edbr
12-08-2009, 01:20 AM
you want to match the logged in name with a name in your database so your conditional if should reflect if this
you are declaring at present
$thissession = ($_SESSION['USER']);
you have selected your database? if so
$row['user']or how ever you have namd that variable of the user name in your database

deafboyzaudio
12-09-2009, 12:33 AM
ok I am able to make the link i want to appear apprear only once after i start a session and only on the page im redirected to after login.
When i switch pages it dissapears and i get an error "undefined variable: Session"
I know i am doing something wrong but im confused on how to fix it.

Here is what im using....
$test = $_SESSION['MM_Username'];

<li><?php if ($test == $_SESSION['MM_Username'])
{
echo '<a href="logout.php">Logout</a>';
}
else
{
echo '<a href="login.php">Login</a>';
} ?></li>You say that i have to select the DB... that makes sense but the only way i know how to do this is by <php require_once ('mydatabase') ?> but that doesnt seem right b/c i need to designate which table and row to pull the username from. Am i on the right track? Thanks ahead of time for your patience, im learning....

edbr
12-09-2009, 12:35 AM
do you have session start() at the begining of all the pages?

deafboyzaudio
12-09-2009, 10:35 PM
no. just the ones like my login page.

edbr
12-10-2009, 12:32 AM
you need that whereever you want to access the session data

deafboyzaudio
12-10-2009, 11:57 PM
ok i have session_start(); on a few of my pages to test it and the logout button shows up on those.

<?php session_start();
$test = $_SESSION['MM_Username'];?>
<li><?php if ($test == $_SESSION['MM_Username'])
{
echo '<a href="logout.php">Logout</a>';
}
else
{
echo '<a href="login.php">Login</a>';
} ?></li>edbr you said that i need to compare the session name to the username in my database... my question is...

WHY do i need to do that???
cant i just check for the existence of a mm_username session.... wouldnt that ONLY be started if the user logged in properly?, im confused
if i have to do it that way then how do i designate which table to get the username from?

i can include the database reference but i'm confused on the way to compare the data to the data in the session.

When i look at my code im basically saying if mm_username== mm_username then display logout.

that sounds stupid, i know.... i know i need to have mm_username == $row[user] like you said but am unsure the exact way to code it... and the reason it has to work that way... any help?