ricerocket Posted February 6, 2008 Share Posted February 6, 2008 On my site I have two different completely different parts the first part is the main site which has the about, register, login, support, etc. etc. and the second part is my webmail system. What I wanted to do is create something that when the user logs in through the webmail system it creates a cookie which is used on the mail site so that the "Register" and "Login" buttons arent there but instead something that says: "Logged in as: Username Logout" how would I do that? I know how to create a script that checks the cookie and displays the username and a logout button but I have no idea how to "change" the nav bar to something else if there is a user cookie available... How would I do this? If any of you can create a little snippet to do this... or make it so that if there is a cookie avail show a different header file.. or something, I don't know. Any help is appreciated Quote Link to comment Share on other sites More sharing options...
haku Posted February 6, 2008 Share Posted February 6, 2008 if(!cookie exists) { echo register button; } replace 'cookie exists' with the name of the cookie, and replace 'register button' with the code that outputs the register button. Quote Link to comment Share on other sites More sharing options...
ricerocket Posted February 6, 2008 Author Share Posted February 6, 2008 cool thanks that almost exactly what I need accept that I didn't really want a new register button I want it to use a different header file or make it so that it shows a different set of buttons on the navigation... do you know how I would do this? Quote Link to comment Share on other sites More sharing options...
ricerocket Posted February 6, 2008 Author Share Posted February 6, 2008 wait what if I change the header file navigation to something like this: if(!cookie exists) { echo " <div id="main"> <div id="banner"><img src="images/banner.gif" width="700" height="102" alt="Mail" /></div> <div id="mainnav"> <table cellpadding="0" cellspacing="0" summary="Main navigation"><tr> <td><a href="http://www.mysite.com/">Home</a></td> <td><a href="http://www.mysite.com/about.php">About</a></td> <td><a href="http://www.mysite.com/screens.php">Screenshots</a></td> <td><a href="http://www.mysite.com/support.php"> Support </a></td> <td>Logged in as: $name <a href='logout.php'>Logout</a></td> </tr></table></div>; " } else { echo " <div id="main"> <div id="banner"><img src="images/banner.gif" width="700" height="102" alt="Mail" /></div> <div id="mainnav"> <table cellpadding="0" cellspacing="0" summary="Main navigation"><tr> <td><a href="http://www.mysite.com/">Home</a></td> <td><a href="http://www.mysite.com/about.php">About</a></td> <td><a href="http://www.mysite.com/screens.php">Screenshots</a></td> <td><a href="http://www.mysite.com/members/">Login</a></td> <td><a href="http://www.mysite.com/register.php">Register</a></td> <td><a href="http://www.mysite.com/support.php"> Support </a></td> </tr></table></div> " } Would that work? Quote Link to comment Share on other sites More sharing options...
haku Posted February 6, 2008 Share Posted February 6, 2008 That last code is not good - tables shouldn't be used for menus. Time to study some XHTML and CSS. What that last code I gave you says is: If the cookie doesn't exist, then output the register button. Or in other words, the register button will only be outputted if the cookie doesn't exist. If the cookie does exist, then it wont output it. So if you want to output something instead of the register button when the cookie exists, just add an 'else' statement to that code: if(!cookie exists) { echo register button; } else { echo logout button; } I usually do this in three stages. Before the 'if' conditional, I output any buttons that are used whether the person is logged in or not (menu link etc). Then I make my if statement, and I put all the buttons for someone not logged in into the if statement, and all the buttons for someone logged in into the else statement. If the order needs to be different, I use multiple if/else statements so that the buttons will be outputted in the order I want. Quote Link to comment Share on other sites More sharing options...
ricerocket Posted February 6, 2008 Author Share Posted February 6, 2008 I've been having a few problems with the above little snippet, I keep getting header errors but I think it's just because of something I did since I created a little code to get the cookie username but it was impossible to make it not send the header before it gets the cookie information. My menu uses tables and yes my CSS isn't right but how would I make it so that if a cookie exists it shows this for the header: <DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <title>Mail</title> <link rel="stylesheet" type="text/css" href="styles.css" /> <link rel="SHORTCUT ICON" href="/images/favicon.ico" type="image/x-icon"> </head> <body> <div id="main"> <div id="banner"><img src="images/banner.gif" width="700" height="102" alt="Mail" /></div> <div id="mainnav"> <table cellpadding="0" cellspacing="0" summary="Main navigation"><tr> <td><a href="http://www.mysite.com/">Home</a></td> <td><a href="http://www.mysite.com/about.php">About</a></td> <td><a href="http://www.mysite.com/screens.php">Screenshots</a></td> <td><a href="http://www.mysite.com/support.php"> Support </a></td> <td>$username - <a href="logout.php">Logout</a></td> </tr></table></div> But if a cookie isn't available it shows the standard header: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <title>Mail</title> <link rel="stylesheet" type="text/css" href="styles.css" /> <link rel="SHORTCUT ICON" href="/images/favicon.ico" type="image/x-icon"> </head> <body> <div id="main"> <div id="banner"><img src="images/banner.gif" width="700" height="102" alt="Mail" /></div> <div id="mainnav"> <table cellpadding="0" cellspacing="0" summary="Main navigation"><tr> <td><a href="http://www.mysite.com/">Home</a></td> <td><a href="http://www.mysite.com/about.php">About</a></td> <td><a href="http://www.mysite.com/screens.php">Screenshots</a></td> <td><a href="http://www.mysite.com/members/">Login</a></td> <td><a href="http://www.mysite.com/register.php">Register</a></td> <td><a href="http://www.mysite.com/support.php"> Support </a></td> </tr></table></div> I'm also using this snippet to get the username from the cookie so if you know how I can integrate this with the header if the cookie exists: //Cookie is set and display the data $cookie_info = explode("-", $_COOKIE['cookie_name']); //Extract the Data $usernamename = $cookie_info[0]; So any help is welcome... Quote Link to comment Share on other sites More sharing options...
haku Posted February 6, 2008 Share Posted February 6, 2008 <?php if(isset($_COOOKIE['cookie_name'])) { ?> <DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <title>Mail</title> <link rel="stylesheet" type="text/css" href="styles.css" /> <link rel="SHORTCUT ICON" href="/images/favicon.ico" type="image/x-icon"> </head> <body> <div id="main"> <div id="banner"><img src="images/banner.gif" width="700" height="102" alt="Mail" /></div> <div id="mainnav"> <table cellpadding="0" cellspacing="0" summary="Main navigation"><tr> <td><a href="http://www.mysite.com/">Home</a></td> <td><a href="http://www.mysite.com/about.php">About</a></td> <td><a href="http://www.mysite.com/screens.php">Screenshots</a></td> <td><a href="http://www.mysite.com/support.php"> Support </a></td> <td>$username - <a href="logout.php">Logout</a></td> </tr></table></div> <?php } else { ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <title>Mail</title> <link rel="stylesheet" type="text/css" href="styles.css" /> <link rel="SHORTCUT ICON" href="/images/favicon.ico" type="image/x-icon"> </head> <body> <div id="main"> <div id="banner"><img src="images/banner.gif" width="700" height="102" alt="Mail" /></div> <div id="mainnav"> <table cellpadding="0" cellspacing="0" summary="Main navigation"><tr> <td><a href="http://www.mysite.com/">Home</a></td> <td><a href="http://www.mysite.com/about.php">About</a></td> <td><a href="http://www.mysite.com/screens.php">Screenshots</a></td> <td><a href="http://www.mysite.com/members/">Login</a></td> <td><a href="http://www.mysite.com/register.php">Register</a></td> <td><a href="http://www.mysite.com/support.php"> Support </a></td> </tr></table></div> <?php } ?> Integrate the cookie with the header in what way? Quote Link to comment Share on other sites More sharing options...
ricerocket Posted February 6, 2008 Author Share Posted February 6, 2008 Sry I didn't mean int he header I just mean for it to me integraded into the file. I also tried the code that you made me and I have to say I didn't even think of doing it like that but anyways I tried it and its showing the standard header but it's coming up that I'm logged in because I used a separate snippet that just shows the username of the cookie but the header is still the same... Any suggestions on why this might be happening? Is it my server? I have php5... Quote Link to comment Share on other sites More sharing options...
Aureole Posted February 6, 2008 Share Posted February 6, 2008 You want to use sessions... <?php session_start(); function logged_in() { if( isset( $_SESSION['logged_in'] ) ) return true; else return false; } ?> <DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <title>Mail</title> <link rel="stylesheet" type="text/css" href="styles.css" /> <link rel="SHORTCUT ICON" href="/images/favicon.ico" type="image/x-icon"> </head> <body> <div id="main"> <div id="banner"><img src="images/banner.gif" width="700" height="102" alt="Mail" /></div> <div id="mainnav"> <table cellpadding="0" cellspacing="0" summary="Main navigation"><tr> <td><a href="http://www.mysite.com/">Home</a></td> <td><a href="http://www.mysite.com/about.php">About</a></td> <td><a href="http://www.mysite.com/screens.php">Screenshots</a></td> <td><a href="http://www.mysite.com/support.php"> Support </a></td> <td><?php if( logged_in() ) echo( $_SESSION['username'] . ' - <a href="logout.php">Logout</a>' ); else echo( 'Welcome Guest - <a href="login.php">Login</a>' ); ?></td> </tr></table></div> When someone logs in, if their Username and Password exists in the Database (so if their login is successful) then just set $_SESSION['logged_in'] = 1 and $_SESSION['username'] to whatever their Username is in the Database. This is an extremely basic example but you get the idea... Quote Link to comment Share on other sites More sharing options...
haku Posted February 6, 2008 Share Posted February 6, 2008 I'm going to be a little picky here, and say that the 'logged in' session variable is unnecessary. Just set the username, and then check for the existence of the username variable to see if they are logged in. If the username session variable is set, they are logged in, if its not, they arent. Quote Link to comment Share on other sites More sharing options...
Aureole Posted February 6, 2008 Share Posted February 6, 2008 That is extremely picky if you don't mind me saying. Your way requires a few less lines of code, but it really wouldn't make that much of a difference. I know, you know this... but on a "pickiness" scale of 1-10, well you're off the scale. Quote Link to comment Share on other sites More sharing options...
revraz Posted February 6, 2008 Share Posted February 6, 2008 I also use the Logged In session to adjust my nav bars. It makes it easier for me to track my code logic. Only time I use the username session is when I want to display the username. Quote Link to comment Share on other sites More sharing options...
haku Posted February 6, 2008 Share Posted February 6, 2008 That is extremely picky if you don't mind me saying. Your way requires a few less lines of code, but it really wouldn't make that much of a difference. I know, you know this... but on a "pickiness" scale of 1-10, well you're off the scale. I know, but I'm a perfectionist that way. I like to aim for the least amount of the most efficient code I can. Still got a long way to go to get there, but that's one of the ways i do it. Pickiness like that doesn't really matter if its just a small site, but when you have a big site with constant users (like this one!) little differences like that make a difference. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.