Problem with sessions (in global scope vs class scope)

This is a discussion on Problem with sessions (in global scope vs class scope) within the PHP Language forums, part of the PHP Programming Forums category; Hello, i'me having a wierd problems with sessions. PHP 4.3.3, Register globals is on, and the sessions ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-16-2003
Yoyoma_2
 
Posts: n/a
Default Problem with sessions (in global scope vs class scope)

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;
}
}




Reply With Quote
  #2 (permalink)  
Old 11-16-2003
Yoyoma_2
 
Posts: n/a
Default Re: Problem with sessions (in global scope vs class scope)

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" <andrena.spamreallysucks@ifi.uio.no> wrote in message
news:bp7891$bnd$1@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
>
>



Reply With Quote
  #3 (permalink)  
Old 11-16-2003
André Næss
 
Posts: n/a
Default Re: Problem with sessions (in global scope vs class scope)

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


Reply With Quote
  #4 (permalink)  
Old 11-16-2003
Matthias Esken
 
Posts: n/a
Default Re: Problem with sessions (in global scope vs class scope)

"Yoyoma_2" <DontAnswer@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
Reply With Quote
  #5 (permalink)  
Old 11-16-2003
Yoyoma_2
 
Posts: n/a
Default Re: Problem with sessions (in global scope vs class scope)

That was the error Thanks!

"Matthias Esken" <muelleimer2003nospam@usenetverwaltung.org> wrote in
message news:bp7j3b.1ag.1@usenet.esken.de...
> "Yoyoma_2" <DontAnswer@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



Reply With Quote
  #6 (permalink)  
Old 11-17-2003
Savut
 
Posts: n/a
Default Re: Problem with sessions (in global scope vs class scope)

.... as $_SESSION is always global

Savut

"Matthias Esken" <muelleimer2003nospam@usenetverwaltung.org> a écrit dans le
message de news:bp7j3b.1ag.1@usenet.esken.de...
> "Yoyoma_2" <DontAnswer@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



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:53 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0