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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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