Connecting Tech Pros Worldwide Help | Site Map

Problem with sessions (in global scope vs class scope)

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 01:09 AM
Yoyoma_2
Guest
 
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;
}
}





  #2  
Old July 17th, 2005, 01:09 AM
André Næss
Guest
 
Posts: n/a
Default Re: Problem with sessions (in global scope vs class scope)

Yoyoma_2:
[color=blue]
> 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.
>[/color]
[color=blue]
> function getValue( $varName ){
> global $_SESSION;[/color]

$_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


  #3  
Old July 17th, 2005, 01:09 AM
Yoyoma_2
Guest
 
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...[color=blue]
> Yoyoma_2:
>[color=green]
> > 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.
> >[/color]
>[color=green]
> > function getValue( $varName ){
> > global $_SESSION;[/color]
>
> $_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[/color]
looks[color=blue]
> completely meaningless to me, what benefit does it provide? Why do you[/color]
need[color=blue]
> it? Why is
>
> $sessionWrapper->setValue('key', 'value');
>
> better than
>
> $_SESSION['key'] = 'value';
>
> ?
>
> André Næss
>
>[/color]


  #4  
Old July 17th, 2005, 01:09 AM
Matthias Esken
Guest
 
Posts: n/a
Default Re: Problem with sessions (in global scope vs class scope)

"Yoyoma_2" <DontAnswer@somewhere.ca> schrieb:
[color=blue]
> function getValue( $varName ){
> global $_SESSION;
> return $_SESSION[ $varName ];
> }[/color]

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

Regards,
Matthias
  #5  
Old July 17th, 2005, 01:09 AM
Yoyoma_2
Guest
 
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...[color=blue]
> "Yoyoma_2" <DontAnswer@somewhere.ca> schrieb:
>[color=green]
> > function getValue( $varName ){
> > global $_SESSION;
> > return $_SESSION[ $varName ];
> > }[/color]
>
> I dont' really believe that it's the reason for the error, but you
> dont't habe to define $_SESSION as global.
>
> Regards,
> Matthias[/color]


  #6  
Old July 17th, 2005, 01:10 AM
Savut
Guest
 
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...[color=blue]
> "Yoyoma_2" <DontAnswer@somewhere.ca> schrieb:
>[color=green]
> > function getValue( $varName ){
> > global $_SESSION;
> > return $_SESSION[ $varName ];
> > }[/color]
>
> I dont' really believe that it's the reason for the error, but you
> dont't habe to define $_SESSION as global.
>
> Regards,
> Matthias[/color]


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.