Jump to content

dynamic link in template not always working


ElectricShaka

Recommended Posts

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.

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.

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.

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.

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.