Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

php session not saved

in my index.php i have at the top:

                   <?php 
                   session_start(); 
                   if (!isset($_SESSION['login'])) {
                         $_SESSION['login']=0;
                        $_SESSION['username']="";
                   }
                   ?>

i also have <?php include("includes/navbar.php"); ?> and in the navbar file i determine if i should display the logged in version of the nav bar, or the normal visitors version using the following code:

                 <?php 
                 if ($_SESSION['login']==true) { ?>
                             showing logged in version
                 <?php } else { ?>
                             show normal
                  <?php } ?>

when I enter a username and password, the login script is called, the script set's $_SESSION['login']=true; and refreshes back to index.php. Now, thats all fine.. however if I now manually refresh index.php my session is lost and i'm logged out. Also If i go to another page on my website that imports (includes) the navbar, it shows the logged out version of it. So either way, sessions are never saved. I have set session_start() to the very top of my index and profile page before anything else.

1 Answer

Matthew Smart
Matthew Smart
12,567 Points

Probably need to see more code to be 100% sure what's going on.