ElectricShaka Posted February 13, 2008 Share Posted February 13, 2008 I have a template.php file that is included in almost every page on my website. Within this template.php file I have a dynamic link which shows up differently at the top of every page depending on whether the user is logged in or not. If logged in, it says "LOGOUT" and leads to the logout.php page. If not logged in, it shows up "LOGIN | REGISTER" and leads to the login.php page. My user login/logout pages all work properly. I check if the user is logged in with the following in the template.php file: session_start(); if (isset($_SESSION['logname'])==TRUE) { $user = $_SESSION['logname']; echo "<div class=dynamic>Welcome $user | <a href='mywebsite/logout.php'>LOGOUT</a></div>"; } if (isset($_SESSION['logname'])==FALSE) { echo '<div class="dynamic"><a href="http://epiklayouts.com/login.php">LOGIN</a> | <a href="mywebsite/login.php"> REGISTER</a></div>'; } My problem is that after logging in the link will correctly say "LOGOUT" except when I click on a link within the template that links to another page on my site which also includes the template.php -- then the user stays logged in but the dynamic link says "LOGIN | REGISTER" as if they are not logged in. I was thinking perhaps the page is loading the session variables after the template has already been included or something like that. I think something is happening out of order somewhere but I could be entirely wrong. Thank you very much for your time and if you have any insight I would greatly appreciate your help. Quote Link to comment Share on other sites More sharing options...
Isityou Posted February 13, 2008 Share Posted February 13, 2008 Try this <?php session_start(); if (isset($_SESSION['logname'])) { $user = $_SESSION['logname']; echo "<div class=dynamic>Welcome $user | <a href='mywebsite/logout.php'>LOGOUT</a></div>"; } else { echo '<div class="dynamic"><a href="http://epiklayouts.com/login.php">LOGIN</a> | <a href="mywebsite/login.php"> REGISTER</a></div>'; } ?> I don't think you can compare isset(variable)==FALSE if the variable isn't even set. Quote Link to comment Share on other sites More sharing options...
ElectricShaka Posted February 13, 2008 Author Share Posted February 13, 2008 Thanks for the idea but it still does it when using that method. ??? Quote Link to comment Share on other sites More sharing options...
haku Posted February 13, 2008 Share Posted February 13, 2008 Make sure you have session_start() at the top of each page that is checking to see if the user is logged in. If sessions haven't been turned on, the program cannot see that the user has a session variable set, and will assume they are not logged in. Quote Link to comment Share on other sites More sharing options...
ElectricShaka Posted February 13, 2008 Author Share Posted February 13, 2008 I have session_start() in the template so basically any page that includes the template (every page) shouldn't have a problem with the session not being started. And like I said, if you manually enter the address of say index.php in the address bar the dynamic link works properly, but if you click on the navigation bar link to index.php (which is part of template.php) then it doesn't work. Quote Link to comment Share on other sites More sharing options...
ElectricShaka Posted February 13, 2008 Author Share Posted February 13, 2008 I solved the problem. My links in template.php were linking to http://mysite.com and the session variables were set to http://WWW.mysite.com so when I clicked a link in the template it couldn't find the correct session variables without the WWW...I didn't know the www would make a difference but apparently it does. Thanks guys for your help again. Quote Link to comment Share on other sites More sharing options...
haku Posted February 13, 2008 Share Posted February 13, 2008 I didn't even think about that, even though I knew it! There are a couple ways to prevent this from happening in the future. First, you should alway use relative links, not absolute ones. Relative links apparently process quicker anyways. The second is thing is to change your .htaccess file so that your site always defaults to either the www version, or the non-www version. Quote Link to comment Share on other sites More sharing options...
ElectricShaka Posted February 13, 2008 Author Share Posted February 13, 2008 Thank you for the tip haku I will do that. 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.