Jump to content

[SOLVED] $_SESSION not sent across multiple pages....


bobbyM

Recommended Posts

Hi,

 

I am using the following:

Apache version 1.3.37 (Unix)

PHP version 4.4.4

MySQL version 4.1.22-standard

 

I have a page which sets a few $_SESSION variables as such:

$_SESSION['username'] = Bobby;

$_SESSION['authentic'] = 1;

 

I didn't use session_start(); at the beginning of this page because from what I read elsewhere there is no need to when using $_SESSION[](only when using session_register()). Now, when I click a link and go to a new page (same domain and same window) none of the $_SESSION variables are read. I just get empty when I echo $_SESSION['username'].

 

Anybody have any ideas? Thanks :)

Well, you read wrong :P

 

You MUST call session_start(); before any use of the $_SESSION superglobal. And it MUST be called before any output to the browser. Otherwise, you'll get an error (or a blank page as seems to be the case - im getting display_errors is turned off in your php setup).

Hrm, i added it to the top of my index.php page and I get the same result. I'll try showing you what I have done.

 

Index.php

 

<?php
session_start();	
  	include('../constant.php');
  	include('../incl.php');
?> 
<html><head></head>
<?php
$password = $_POST['password'];
$username = $_POST['username'];

$enc_password = md5(sha1($password));

$query = "SELECT users_id, username, admin
		  FROM users
		  WHERE username = '". $username ."' AND password = '". $enc_password ."'";
$result = mysql_query($query);

$row = mysql_num_rows($result) or die(mysql_error());

if($row)
{
	while($rows = mysql_fetch_row($result))
	{	    
		$_SESSION['id'] = $rows[0];
		$_SESSION['username'] = $rows[1];
		$_SESSION['authentic'] = 1;
		$_SESSION['admin'] = $rows[2];
              }
             }
             include(menu.php); //bad formto include like this? No clue.....

 

 

menu.php simplified

<html><head></head>
<body>
<?php
echo '<a href="cart.php">Cart</a>';
?>
</body></html>

 

finally, cart.php where the $_SESSION variables aren't showing

<html>
<head>
<body>
<?php 	 
echo "hi";
echo $_SESSION['admin'].'is admin';
echo $_SESSION['username'].'is user'; ?>
</body></head></html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.