Session question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikeT
Forum Newbie
Posts: 7
Joined: Sun Jul 14, 2002 4:32 pm

Session question

Post by mikeT »

Hi

I am having trouble using session variables with Class functions and I have trawled the documentation and faq lists and I just can't find a solution.

I am trying to set up a global class for a community site that would allow a member to link from their own site to the community and then dynamically generate their content. This content wold be across several pages so I figured the best way to do this would be to take a user reference querystring and assign a session variable that would then be used to figure out what content to get on subsequent page requests.

Here is what index.php looks like...
------------------------------------------------------------------------------

Code: Select all

<?

Class CGlobal
&#123;
	function CGlobal()
	&#123;
		global $HTTP_GET_VARS;
		
		session_start();
		
		if (isset($HTTP_GET_VARS&#1111;'user']))
		&#123;
			session_register('USER');
			$HTTP_SESSION_VARS&#1111;'USER'] = $HTTP_GET_VARS&#1111;'user'];
		&#125;
	&#125;
	
	function initPage()
	&#123;
		global $HTTP_SESSION_VARS;
		
		echo session_id();
		
		echo '<br>session_is_registered(USER) = '. session_is_registered('USER');
		echo '<br>count($HTTP_SESSION_VARS) = '. count($HTTP_SESSION_VARS);
		
		if (isset($HTTP_SESSION_VARS&#1111;'USER']))
		&#123;
			echo $HTTP_SESSION_VARS&#1111;'USER'];
		&#125;
	&#125;
&#125;

$global = new CGlobal;

?>

<html>
	<head></head>
<body>

<p><b>index.php</b></p>

<p><a href="index.php?user=a1">Add user</a></p>

<?
	$global->initPage();
?>

<br><br>

<p><a href="index.php">index</a> | <a href="page1.php">page1</a></p>

</body>
</html>
---------------------------------------------------------------------------

And another page, page1.php

---------------------------------------------------------------------------

Code: Select all

<?

Class CGlobal
&#123;
	function CGlobal()
	&#123;
		global $HTTP_GET_VARS, $HTTP_SESSION_VARS;
		
		session_start();
		
		if (isset($HTTP_GET_VARS&#1111;'user']))
		&#123;
			session_register('USER');
			$HTTP_SESSION_VARS&#1111;'USER'] = $HTTP_GET_VARS&#1111;'user'];
		&#125;
	&#125;
	
	function initPage()
	&#123;
		global $HTTP_SESSION_VARS;
		
		echo session_id();
		
		echo '<br>session_is_registered(USER) = '. session_is_registered('USER');
		echo '<br>count($HTTP_SESSION_VARS) = '. count($HTTP_SESSION_VARS);
		
		if (isset($HTTP_SESSION_VARS&#1111;'USER']))
		&#123;
			echo $HTTP_SESSION_VARS&#1111;'USER'];
		&#125;
	&#125;
&#125;

$global = new CGlobal;

?>

<html>
	<head></head>
<body>

<p><b>page1.php</b></p>

<p><a href="index.php?user=a1">Add user</a></p>

<?
	$global->initPage();
?>

<br><br>

<p><a href="index.php">index</a> | <a href="page1.php">page1</a></p>

</body>
</html>
---------------------------------------------------------------------------------

If you click the Add user link it will register the $HTTP_SESSION_VARS['USER'] when the CGlobal class initiates, however it is not available in the initPage() function. The strange thing, for me at least, is that the session_is_registered() function returns true but the array count of $HTTP_SESSION_VARS returns 0.

This is being used with the cgi version of PHP on IIS/win2k.

Got any ideas?

Thx in advance.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

php manual wrote:If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered() and session_unregister().
Could it be that mixing the two together is causing problems? Which version of PHP are you using? If it's v4.1 or higher then I'd just switch to using $_SESSION.

Mac
mikeT
Forum Newbie
Posts: 7
Joined: Sun Jul 14, 2002 4:32 pm

Post by mikeT »

Thanks for that twigletmac, I am using PHP version 4.2.1 and the manual I have is considerably older than that, so that extract you have included is not in the copy of the manual I have. Naughty me for not loooking at the manual online, sorry.

But that did the trick, with the following changes...

Class CGlobal
{
function CGlobal()
{
global $HTTP_GET_VARS;

session_start();

if (isset($HTTP_GET_VARS['user']))
{
$_SESSION['USER'] = $HTTP_GET_VARS['user'];
}
}

function initPage()
{
global $HTTP_SESSION_VARS;

echo session_id();

echo '<br>count($HTTP_SESSION_VARS) = '. count($HTTP_SESSION_VARS);

if (isset($_SESSION['USER']))
{
echo '<br>'. $_SESSION['USER'];
}
}
}

$global = new CGlobal;

Thanks again.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Glad to help, I'm sure that bit only recently appeared in the manual anyway.

Bit of additional info - since you're using 4.2.1 check out the information on pre-defined variables. Basically unless you need them for backwards compatibility with installations of PHP less than version 4.1 then you can replace the old $HTTP_XXX_VARS arrays with the new $_XXX arrays:

Code: Select all

$HTTP_SESSION_VARS == $_SESSION
$HTTP_GET_VARS == $_GET
$HTTP_POST_VARS == $_POST etc...
Loads less typing and because they are auto-globals (regardless of register_globals on or off) they don't need to be declared as globals in functions and classes.

Mac
Post Reply