By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,022 Members | 856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,022 IT Pros & Developers. It's quick & easy.

Problem with sessions (in global scope vs class scope)

P: n/a
Hello, i'me having a wierd problems with sessions.
PHP 4.3.3, Register globals is on, and the sessions module is installed.

if i have a page like this:
<?
session_start();
$_SESSION[ 'color' ]="blue";
?>

On the next page i can see that $_SESSION[ 'color' ] is actually equal to
"blue". So i know that my sessions work in global scope.

Now i tried to make a nice little session wrapper to help in the code
development. Using this wrapper, for some odd reason, it doesn't work.

I posted the class after this text for verification. Did i do a stupid
mistake somewhere? OR am i missing something?

Thanks for all your help! Php4evr! :)

----------------
<?php
class SessionWrapper{
function SessionWrapper( $params = array() ){
session_start();
}

/**
* Gets a session variable, for example,
* $session->getValue( "FavoriteColor" ) would return "blue". Objects
are even serialized.
* so you can set a session string, integer, object, array, whatever. It
will be
* serialized by the session API.
*/
function getValue( $varName ){
global $_SESSION;
return $_SESSION[ $varName ];
}

/**
* Sets a session variable, for example,
* $session->setValue( "FavoriteColor", "blue" ) would set the
* "FavoriteColor" session variable to "blue". Be carefull not to write
over eachother's
* session variables.Objects are even serialized.
* so you can set a session string, integer, object, array, whatever. It
will be
* serialized by the session API.
*/
function setValue( $name, $value ){
global $_SESSION;
$_SESSION[ $name ] = $value;
}
}


Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Yoyoma_2:
Hello, i'me having a wierd problems with sessions.
PHP 4.3.3, Register globals is on, and the sessions module is installed.

Now i tried to make a nice little session wrapper to help in the code
development. Using this wrapper, for some odd reason, it doesn't work.
function getValue( $varName ){
global $_SESSION;


$_SESSION is a superglobal, and there is no need for the global statement.

I'm not sure if this is why it doesn't work. But I must say your class looks
completely meaningless to me, what benefit does it provide? Why do you need
it? Why is

$sessionWrapper->setValue('key', 'value');

better than

$_SESSION['key'] = 'value';

?

André Næss
Jul 17 '05 #2

P: n/a
This is under a project for a "Software Engineering" class where we learn
about desing methodologies so i would like to have proper desing. Since the
rest of the app is done in an OO aproach, i think specifing this entity in
its own class is probably a good idea.

You can imagine the day that you want to add extra pre/post operations when
setting sessions, that could be done easily if it waer a class. But if you
simply do $_SESSION[ 'foo' ] =.. Then you can't do that handling.

Same thing for collision or any other extra handling you would want to do,
etc...

That did the trick! THANKS ALOT!!!!
I guess global $foo; if $foo is superglobal created a new variable called
$foo that wasen't instantiated.. Wow..

Here's a beer. |_|D

It's gotta be cause we have the same first name :) (but mine is french hehe)

TTYL!

"André Næss" <an*********************@ifi.uio.no> wrote in message
news:bp**********@maud.ifi.uio.no...
Yoyoma_2:
Hello, i'me having a wierd problems with sessions.
PHP 4.3.3, Register globals is on, and the sessions module is installed.

Now i tried to make a nice little session wrapper to help in the code
development. Using this wrapper, for some odd reason, it doesn't work.
function getValue( $varName ){
global $_SESSION;


$_SESSION is a superglobal, and there is no need for the global statement.

I'm not sure if this is why it doesn't work. But I must say your class

looks completely meaningless to me, what benefit does it provide? Why do you need it? Why is

$sessionWrapper->setValue('key', 'value');

better than

$_SESSION['key'] = 'value';

?

André Næss

Jul 17 '05 #3

P: n/a
"Yoyoma_2" <Do********@somewhere.ca> schrieb:
function getValue( $varName ){
global $_SESSION;
return $_SESSION[ $varName ];
}


I dont' really believe that it's the reason for the error, but you
dont't habe to define $_SESSION as global.

Regards,
Matthias
Jul 17 '05 #4

P: n/a
That was the error Thanks!

"Matthias Esken" <mu******************@usenetverwaltung.org> wrote in
message news:bp**********@usenet.esken.de...
"Yoyoma_2" <Do********@somewhere.ca> schrieb:
function getValue( $varName ){
global $_SESSION;
return $_SESSION[ $varName ];
}


I dont' really believe that it's the reason for the error, but you
dont't habe to define $_SESSION as global.

Regards,
Matthias

Jul 17 '05 #5

P: n/a
.... as $_SESSION is always global

Savut

"Matthias Esken" <mu******************@usenetverwaltung.org> a écrit dans le
message de news:bp**********@usenet.esken.de...
"Yoyoma_2" <Do********@somewhere.ca> schrieb:
function getValue( $varName ){
global $_SESSION;
return $_SESSION[ $varName ];
}


I dont' really believe that it's the reason for the error, but you
dont't habe to define $_SESSION as global.

Regards,
Matthias

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.