Session variable confusion

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

brownie124
New php-forum User
New php-forum User
Posts: 23
Joined: Thu Feb 13, 2003 1:58 pm
Location: Peterborough, NH

Session variable confusion

Postby brownie124 » Mon Jun 02, 2003 3:51 am

Hi,

According to the PHP book I have been reading, you register session variables by doing:
<?php
session_start();
session_register("foo");

$foo = "foo value";
?>

Then, supposedly I can start another page:
<?php
session_start();

echo $foo;
?>

This does not work for me at all. On the first page $foo has a value, but it does not on the second page. I look at the session file and the foo is there but it has a value of null (N).

Now, I can use
$_SESSION[foo] = "foo value";

on the first page and then access

$_SESSION[foo]

on the second with no problems.

I actually have a couple of questions:
1) Should the initial way work? If so, why is mine not working. Does it have anything to do with register_globals being turned off?
2) Which way is the more acceptable (better coding) way?

Thanks,
- Brownie

User avatar
bezmond
Moderator
Moderator
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK
Contact:

Postby bezmond » Mon Jun 02, 2003 5:49 am

1) Yes, that is because of register_globals being off I believe... someone will probably correct me knowing my luck :evil:

2) Personally I prefer the second method, it is a little more secure than the 1st method!

Andrew

brownie124
New php-forum User
New php-forum User
Posts: 23
Joined: Thu Feb 13, 2003 1:58 pm
Location: Peterborough, NH

Postby brownie124 » Mon Jun 02, 2003 6:14 am

Thanks Andrew. I was reading through the php.ini file and they mentioned the security risk of turning it on.

The only problem with the second is there is more typing. it would be cool if there were a macro expansion capability, like C. Something like:

#define _S(a)
{
$_SESSION($a);
}

Then I could do something like:
echo _S(foo);

and it would automatically expand for me.

- Brownie

User avatar
bezmond
Moderator
Moderator
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK
Contact:

Postby bezmond » Mon Jun 02, 2003 7:08 am

lol - yeah that would be cool... send them a feature request perhaps? http://www.php.net ;)

Andrew


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest