Link to home
Create AccountLog in
Avatar of CMES-IT
CMES-ITFlag for United States of America

asked on

Session not persisting across pages

Hi,

I have two PHP files (see below). One is setting a session variable, then redirecting to another page that is supposed to print the session variable, however, it's not printing. The output of the scripts below is:

[--]

What am I doing wrong?
<?php
// setSession.php
session_start();
 
$_SESSION["member_ID"] = "Hello";
sleep(3);
header("Location: getSession.php");
 
?>
 
<?php
// getSession.php
session_start();
 
echo("[-");
echo($_SESSION["member_ID"]);
echo("-]");
 
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
could you add
print_r($_SESSION]; in both pages, and in the first page put it before the sleep and make the sleep like 10 so that you can see the $_SESSION array.
Avatar of CMES-IT

ASKER

Hmm... I had no idea PHP sessions require cookies. ASP sessions do not. Weird. I thought the whole point of sessions was that you weren't using cookies. Just as an aside - is it possible to use PHP sessions without cookies?

But anyhow, yes, that was the issue - I was not accepting cookies from the site. As soon as I turned cookies on, it worked fine.
2 bugs has been fixed, replace the whole script.

<?php
	session_start();
	if (isset($_SESSION['username']) == false){
		header("Location:login.php");
		exit();
	}
	require "connect.php";
$customer_ref = $_GET['customer_ref'];
 
 
	$num_records = $_GET['num_records'];
	if(!$num_recorder)
		$num_records = 10; // you could change it to whatever default limit you need at the beggining.
	$query_count = "SELECT count(*) FROM customer";
	$result_count = mysql_query($query_count) or die("Error: " . mysql_error());
	$totalrows = mysql_result($result_count, 0, 0);
	$numofpages = ceil($totalrows/$num_records);
	
	if (isset($_GET['page'])) $page = $_GET['page'];
	else $page = 1;
 
	$offset = ($page - 1) * $num_records;	
 
 
 
 
if (($_GET['text'] == "")  ||  ($_GET['field'] == "*"))		
{
	//$query = "SELECT * FROM customer ORDER BY customer_surname limit 0, $no_of_records_to_display";
	
	$query = "SELECT * FROM customer ORDER BY customer_ref LIMIT $offset, $num_records ";
}
	else
{
	$num_records = $_GET['num_records'];
        $query = "SELECT * FROM customer WHERE ".$_GET['field'].
                                     " like '%".$_GET['text']."%' ORDER BY customer_surname LIMIT $offset, $num_records ";
 
					 
					 //"SELECT filename FROM customer_pdf WHERE customer_ref = '".$customer_ref."'";
}
$result = @mysql_query($query, $connection) 
		or die ("Unable to perform query<br />$query");
?>
 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Integral by Free CSS Templates</title>
<meta name="Keywords" content="" />
<meta name="Description" content="" />
<link href="default.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header">
	<ul id="menu">
		<li><a href="home.php">Home</a></li>
		<li><a href="search_customer.php" accesskey="2" title="">customers</a></li>
		<li><a href="reports.php" accesskey="3" title="">reports</a></li>
		<li><a href="searchpolicy.php" accesskey="4" title="">document library</a></li>
		<li><a href="emailform.php" accesskey="5" title="">email</a></li>
	</ul>
</div>
<div id="content">
	<div id="colOne">
		<div id="logo">
			<h1><a href="#">gss</a></h1>
			<h2>insurance and property services </h2>
		</div>
		<div class="box">
		  <ul class="bottom"><li></li>
			</ul>
		</div>
		<div class="box">
			<h3>&nbsp;</h3>
			<ul class="bottom"><li></li>
			</ul>
		</div>
	</div>
	<div id="colTwo">
		<h2>Search Customer </h2>
	  <p class="indent">
	    
		
<script type="text/javascript">
window.onload=init;
function init()
{
 document.getElementById("text").focus();
}
</script>
		
		<form method="get" action="search_customer.php" name="search_pdf">
<table width="513">
      <tr>
        <td height="20">Search:</td>
          <td><input name="text" type="text" id="text" style="border:groove">
            <select name="field" id="field">
              <option value="*">Choose category</option>
              <option value="customer_forename">Forename</option>
              <option value="customer_surname">Surname</option>
            </select></td>
        </tr>
		<td> <select name="num_records">
<option value="2">2</option>
<option value="4">4</option>
<option value="6">6</option>
<option value="8">8</option>
<option value="10">10</option>
</select></td>
          <td><p>
            <input name="search" type="image" src="images/go.gif" type="submit" id="search" value="Search">
          </p>
		  <td><h1>&nbsp;</h1></td>
		  
      	
		  <p>&nbsp;</p><td width="3"></td>
  	</tr>
 <tr>
   <td height="30">&nbsp;</td>
    <td>&nbsp;</td>
 </tr>
</table>
</form>
<?php if (mysql_num_rows($result) == 0)
{ echo "Your search came back with no results<br>
<br>
Suggestions:<br>
    * Make sure all words are spelled correctly.<br>
    * Try different keywords.<br>
    * Try more general keywords.<br>
    * Try fewer keywords.";
}
  ?>
<div class="box">
  <table width="802" >
<hr />
	<tr>
    <th width="50">Ref</th>
    <th width="136"><div align="left">Title </div></th>
    <th width="141"><div align="left">Forename</div></th>
	<th width="307"><div align="left">Surname</div></th>
	<th width="106"><div align="left">DOB</div></th>
  	</tr>
	
	
	
 
	<?php  
    while($row = mysql_fetch_array($result))
	{ 
        if ($bgcolor == "#E0E0E0")
		{ 
            $bgcolor = "#FFFFFF"; 
        }else
		{ 
            $bgcolor = "#E0E0E0"; 
        } 
 
	echo("<tr bgcolor=".$bgcolor.">\n<td>"); 
    echo($row["customer_ref"]); 
    echo("</td>\n<td>"); 
    echo($row["customer_title"]); 
	echo("</td>\n<td>");
	echo($row["customer_forename"]); 
	echo("</td>\n<td>");
	echo($row["customer_surname"]); 
	echo("</td>\n<td>");
	echo($row["customer_dob"]);
	echo("</td>\n");
	?>
	
	<td><a href="customerview.php?customer_ref=<?php echo $row['customer_ref'];?>"<img src="images/go.gif" alt="view customer" width="38" height="31" /></td>
	<?php
    echo("\n</tr>");
	
    }
 
    echo("</table>"); 
	
//Enables the PREV link if not first page
	if ($page > 1)
	{
    	echo("<a href=\"search_customer.php?page=".($page-1)."&num_records=".$num_records."\">PREV</a> ");
	} 
	else 
	{
    	echo("PREV ");
	}
 
	//Creates the links for each page
	for ($i=1; $i<=$numofpages; $i++)
	{
    	if ($i == $page)
		{
        	echo "$i ";
    	}
		else 
		{
        	echo "<a href=\"search_customer.php?page=$i\">$i</a> ";
    	}
	}
 
	//Enable the Next link if not last page
	if ($page < $numofpages)
	{
    	echo("<a href=\"search_customer.php?page=".($page+1)."\">NEXT</a>");
	}
	else
	{
    	echo("NEXT");
	} 
 
	?>
</table>
</div>
 
<hr>
<a href="home.php">[Home]</a>
		
		</p>
<div class="box">
			<h3>&nbsp;</h3>
			<h4>&nbsp;</h4>
			<p class="bottom">&nbsp;</p>
	  </div>
  </div>
</div>
<div id="footer">
	<p>Copyright (c) 2008 Joe Tzouvanni All rights reserved.</p>
</div>
</body>
</html>
 
 
 
<head>
<title>Search PDF</title>
</head>

Open in new window

sorry, please disregard the previous message.