Echo session

please my log in page works, but when i echo the sesion username ,it didnt show , i have started the session on all pages.(painted the line in green)thanks

header mark up


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Reacheasy - Foremost website for easy reach of things globally</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<link href="reasy.css" rel="stylesheet" type="text/css"/>  
</head>
  <body>
<div id="outer">
<div id="page">
<div id="header">

[COLOR="#00FF00"]<h1>Reacheasy <?php echo $_SESSION['username'];?></h1> [/COLOR]
  <ul id="nav">
    <li class="current"><a href="index.php">Home</a></li>
     <li><a href="women.php">Women</a></li>
     <li><a href="men.php">Men</a></li>
     <li><a href="children.php">Children</a></li>
     <li><a href="homeandappliances.php">Home&amp;Appliances</a></li>
     <li><a href="visionandsound.php">Vision&amp;Sounds</a></li>
      <li><a href="motoring.php">Motoring</a></li>
      <li><a href="homemore.php">More</a></li>
      <li><a href="homecontact.php">Contact us</a></li>
  </ul>
</div> <!--end of navigation div -->
</div>
<div id="navigation">
  <div>
<form action='relogin.php' method='POST' class='rl'>
<fieldset>
<legend>Login</legend>
	<div>
	<label for='username' class='fixedwidth'>Username</label>
	<input type='text' name='username' id='username'/>
	</div>
	
	<div>
	<label for='password' class='fixedwidth'>Password</label>
	<input type='password' name='password' id='password'/>
	</div>
<div class='buttonarea'>
	<input type='submit' value='Log in'>
	</div>
</form>
<p>
<a href='reregister.php'>Register </a>
</p>
</div>
</fieldset>

<div id="advert">
<ul>
<li><iframe src="http://rcm-uk.amazon.co.uk/e/cm?t=r0ab-21&o=2&p=20&l=ur1&category=generic&banner=0YCR1B12KQC9EA34FF02&f=ifr" width="120" height="90" scrolling="no" border="0" marginwidth="0" style="border:none;" frameborder="0"></iframe></li>
 <li><iframe src="http://rcm-uk.amazon.co.uk/e/cm?t=r0ab-21&o=2&p=11&l=ur1&category=endless&banner=1BXHM01F8MNJJGXPV2R2&m=endless&f=ifr" width="150" height="600" scrolling="no" border="0" marginwidth="0" style="border:none;" frameborder="0"></iframe></li>
</ul>
</div>
</div> <!--end of navigation div -->

index mark up


[COLOR="#00FF00"]<?php require_once("include/session.php");?>[/COLOR]
<?php require_once("include/functions.php");?>
[COLOR="#00FF00"]<?php include("include/header.php");?>[/COLOR]
<div id="bodycontent">
<div id="bannercontainer">
<a href="img/hp.jpg"></a></div>
<div id="pbanner">
  <ul id="promo-banners">
  <li class="promo-banner">
    <h2>
        <a href="women.html">Women</a>
     </h2>
<a href="women.html"><img height="198" width="228"  src="img/fashion.jpg" alt=" alibi"/>
</a>
<p class="promo-seo">
 glam up for this season hottest party reduction
<a href="men.html">Men</a>
</p>
  </li>
<li class="promo-banner">
    <h2>
        <a href="children.html">Children</a>
     </h2>
<a href="women.html"><img height="198" width="228"  src="img/children.jpg" alt=" alabi"/>
</a>
<p class="promo-seo">
 glam up for this season hottest party reduction
<a href="men.html">Men</a>
</p>
  </li>
<li class="promo-banner">
    <h2>
        <a href="visionandsound.html">Vision&amp;Sounds</a>
     </h2>
<a href="women.html"><img height="198" width="228"  src="img/vands.jpg" alt=" alabi"/>
</a>
<p class="promo-seo">
 glam up for this season hottest party reduction
<a href="men.html">Men</a>
</p>
  </li>
<li class="promo-banner">
    <h2>
       <a href="motoring.html">Motoring</a>
     </h2>
<a href="women.html"><img height="198" width="228"  src="img/cars.jpg" alt=" alabi"/>
</a>
<p class="promo-seo">
 glam up for this season hottest party reduction
<a href="men.html">Men</a>
</p>
  </li>
<li class="promo-banner">
    <h2>
       <a href="homeandappliances.html">Home&amp;Appliances</a>
     </h2>
<a href="women.html"><img height="198" width="228"  src="img/lightning.jpg" alt=" alabi"/>
</a>
<p class="promo-seo">
 glam up for this season hottest party reduction
<a href="men.html">Men</a>
</p>
  </li>
<li class="promo-banner">
    <h2>
        <a href="men.html">Men</a>
     </h2>
<a href="women.html"><img height="198" width="228"  src="img/men.jpg" alt=" alabi"/>
</a>
<p class="promo-seo">
 glam up for this season hottest party reduction
<a href="men.html">Men</a>
</p>
  </li>
  </ul>
  </div>
</div>
</div>
<?php include("include/footer.php");?>

session


<?php session_start();
?>

Hi,

Might sound like a stupid question, but have you set the session value ‘username’?
Try printing your session to see what values are stored in the session


echo '<pre>';
print_r( $_SESSION );
echo '</pre>';

If you don’t see something like the following then username is not defined in the session.
If you do see the key ‘username’ and the value is null, then there is a problem where you are assigning the value to the session.

Array
(
    [username] => 'somevalue'
)

Hope that helps.

Cheers.

i have set the session name in the log in page below

<?php require_once(“include/session.php”);?>
<?php require_once(“include/functions.php”);?>
<?php
$username = $_POST[‘username’];
$password = $_POST[‘password’];

if ($username&&$password)
{

$connect = mysql_connect(“localhost”,“reachea2”,“HYW6QwoV”)
or die(“couldn’t connect!”);
mysql_select_db(“reachea2_registeringmembers”) or die (“couldn’t find db”);

$query = mysql_query(“SELECT * From reusers WHERE username=‘$username’”);

$numrows =mysql_num_rows($query);

if($numrows!=0)
{
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row[‘username’];
$dbpassword = $row[‘password’];

	// chect to see if they match!
	if ($username==$dbusername&&md5($password)==$dbpassword)
	{
	//echo " You are in! &lt;a href='remembers.php'&gt;Click&lt;/a&gt; here to enter the member page";
	//echo " You are in! &lt;a href='index.html'&gt;Click&lt;/a&gt; here to enter the member page";
	redirect_to('index.php');
	
	$_SESSION['username']=$username;
	}
	else
 echo " incorrect password!";
	
}

}
else
die(“That user dosen’t exist!”);

}
else
die(“Please enter a username and a password”);

?>

do i need to do more

still not echoing the username after loging

Hi.

This script should work. I’ve added comments to show you how it works.

<?php

require_once("include/session.php");
require_once("include/functions.php");

$username = $_POST['username'];
$password = $_POST['password'];

if ( $username && $password ){

	// Connect to database
	$connect = mysql_connect("localhost","root","")	or die("couldn't connect!");
	mysql_select_db("development") or die ("couldn't find db");

	// Build query and execute
	$password = md5( $password );
	$query = "SELECT * FROM users WHERE username='$username' AND password='$password' LIMIT 1";
	$query = mysql_query($query) or die( mysql_error() );

	// Check to see if user was returned
	if( mysql_num_rows($query) == 1 ){

		// Get returned record
		$row = mysql_fetch_assoc( $query );

		/**
		 * $dbusername && $dbpassword - What's their purpose?
		 * Do you need them?
		 * If your redirecting straight away then defining these values is pointless 
		 * as $row['username'] is used to set session value.
		 */
		$dbusername = $row['username'];
		$dbpassword = $row['password'];
		
		/**
		 * Set session values BEFORE redirect.
		 * Redirecting BEFORE means that the script will redirect
		 * before the values are set in the session.
		 */  
		$_SESSION['username']= $row['username'];
		redirect_to('index.php');
		
	} else {
		die("That user dosen't exist!");
	}

} else {
	die("Please enter a username and a password");
}

?>

do i need to do more

Your main problem was that you were redirecting BEFORE you are setting ‘username’ in the session.

Hope this helps.

Have you solved the problem?

yes i have solved the problem, i was redirecting BEFORE
setting ‘username’ in the session.

Yep, that was is lol.