Jump to content

Recommended Posts

I am having a problem getting an included file to read a session variable. I have done a similar set-up before and never encountered this sort of problem.

 

Any references to it in the main page file work fine (so the session is being created correctly) but references in the included file are ignored.

 

There is probable something simple I am missing but if anyone can spot it I would be grateful!!

 

Steve

 

Top of Main Page

<?php session_start();
require_once('inc/cnct.php');
require_once('inc/config.php');
require_once($root.'/inc/constants.php');

$title = 'Home'
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<?php include ($includes."/commonhead.php"); ?>
<title>Test Site</title>
</head>


<body>
<?php if ($loggedin==1){echo '<b>LOGGED-IN!</b><br /><br />';} ?>
<?php include ($includes."/header.php"); ?>
<?php include ($includes."/menu.php"); ?>


<div id="main"> <span class="tm_bottom"></span>
<!-- Start Content -->
<?php if ($loggedin==1){echo '<b>LOGGED-IN!</b><br /><br />';} ?>

There are two different places on that page where I have put if ($loggedin==1){echo '<b>LOGGED-IN!</b><br /><br />'; (just for testing purposes) and they work correctly.

However, when I try to use it in header.php the result is blank. Even echoing $loggedin or $_SESSION['loggedin'] comes up blank.

 

header.php (in full)

<div id="header">

<div id="site_title">

<a href="<?php echo $home ?>" target="<">This will be my tag line</a>

</div> <!-- end of site_title -->

<div id="header_right">
<?php echo 'Logged in: '.$_SESSION['user'];
echo 'Logged In = '.$loggedin; ?>
<div id="newsletter_box">
<nav>
<ul>
<li id="login">
<?php
if ($loggedin==1)
{echo '<b>LOGGED-IN!</b><br /><br />';}

if ($loggedin==1)
{echo ' Hello (<a href="/inc/logout.php">LOGOUT</a>)';}
else
{?><a id="login-trigger" href="#" class="">
Log in <span>▼</span>
</a>
<div id="login-content" style="display: none; ">
<form action="/login/loginscript.php" method="post">
 <fieldset id="inputs">
 <input id="Username" type="" name="Username" placeholder="Your Username" required="">
 <input id="Password" type="password" name="Password" placeholder="Password" required="">
 </fieldset>
 <fieldset id="actions">
 <input type="submit" id="submit" value="Log in">
 <label><input type="checkbox" checked="checked" name="Remember"> Keep me signed in</label>
 </fieldset>
</form>
</div>
</li>
<li id="signup">
<a href="">Register</a>
</li>
</ul>
</nav>
<?php
}
?>
</div>

</div>

<div class="cleaner"></div>

</div> <!-- end of header -->

 

Login Script

if(!empty($_POST['Username']) AND !empty($_POST['Password']))
{//Log-in Attempted so check against dbase
//Set usable strings
$username = mysql_escape_string($_POST['Username']);
$password = mysql_escape_string(md5($_POST['Password']));
$remember = $_POST['Remember'];


$users = "SELECT userid, username, password, active, level
FROM users
WHERE username like binary '".$username."' AND password='".$password."' AND active='1'";
$search = mysql_query($users) or die(mysql_error());
$match = mysql_num_rows($search);
$level = mysql_fetch_assoc($search);


if ($match==1)/*Checks for a match*/
{//Username and Password are correct, Log them in!
//START MANUAL LOGIN & SET SESSION VARIABLES
$_SESSION['user'] = $username;
$_SESSION['level'] = $level['level'];
$_SESSION['loggedin'] = '1';
$_SESSION['id'] = $level['userid'];
$_SESSION['pass'] = $level['password'];

if($remember==1)
{//User has asked to be remembered so set cookies
setcookie('username', $username, time()+60*60*24*365, "/");
setcookie('password', $password, time()+60*60*24*365, "/");
}//End set Cookies
header( 'Location: template.php' ) ;
}//End log-In Procedure

 

constants.php

<?php  
if($_SESSION['loggedin']==1){$loggedin='1';}
else
{
if(isset($_COOKIE['username']) AND isset($_COOKIE['password']))
{// Cookies are set so check if they are still active 
$username = mysql_escape_string($_COOKIE['username']);  
$password = mysql_escape_string($_COOKIE['password']);

$search = mysql_query(" SELECT 
userid, username, password, active, level 
FROM 
users 
WHERE username='".$username."' AND password='".$password."' AND active='1'
") 
or die(mysql_error());  

$match  = mysql_num_rows($search);  
$level = mysql_fetch_assoc($search);

if($match > 0) //There are valid cookies so use that to log the user in 
  {//START AUTO LOGIN & SET SESSION VARIABLES  
  $_SESSION['user'] = $username;
  $_SESSION['level'] = $level['level'];
  $_SESSION['pass'] = $level['password'];
  $_SESSION['loggedin'] = '1';
  $loggedin = '1';
  $_SESSION['id'] = $level['userid'];
  }//END AUTO LOGIN 
}
}
?>

Edited by MargateSteve
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.