Ok ive been having this major problem with my site. Whats going on is as I’m going from page to page its is not maintaining its SESSION. Here are a couple source codes on the same directory. Ok so I have an index with a modal box that contains the iframe for login.php and logout.php. It logs me in but once I go to a different page It doesn’t carry the session and then when I click login its says Im already logged in and has the session. Also how can I add cookies to my website I find those confusing.
Just snippets:
part of header.php goes on every page
<?php session_start(); if($_SESSION["username"]) { ?>
<div style="display: inline-block; font-size: 14px; padding-left: 20px;">Hello <?php echo $_SESSION['username']; ?>
login.php
<?php session_start(); require_once('connections/Main.php');
if($_SESSION['username']) {
echo '<div class="error_message">Attention! You, '.$_SESSION['username'].' are already logged in.</div>';
echo "<br />";
echo "Go <a target='top' href='index.php'>back</a> to the page you were viewing before this.</li>";
exit();
}
... database funcctions go here then add session
if($rowCheck > 0) {
while($row = mysql_fetch_array($result)) {
// Start the session and register a variable
session_start();
$_SESSION['username'] = $user;
//session_register('username');
echo '<script> parent.document.location.href = "index.php"; </script>';
}
logout.php
<?php session_start(); ?>
<?php if($_SESSION['username']) {
session_unset();
session_destroy();
header("Location: index.php"); }
else { header("Location: index.php"); } ?>
Found out what was really wrong for anybody who has this problem. You must make sure every URL the user goes to is http://website.com or http://www.website.com but not mixed. You cannot have different pages with both of these or sessions will disappear. This is because with www it is stored in www/website/ and not just website/