473,785 Members | 2,498 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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( "FavoriteCo lor" ) 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
* "FavoriteCo lor" session variable to "blue". Be carefull not to write
over eachother's
* session variables.Objec ts 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
5 5397
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
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.u io.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
"Yoyoma_2" <Do********@som ewhere.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
That was the error Thanks!

"Matthias Esken" <mu************ ******@usenetve rwaltung.org> wrote in
message news:bp******** **@usenet.esken .de...
"Yoyoma_2" <Do********@som ewhere.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
.... as $_SESSION is always global

Savut

"Matthias Esken" <mu************ ******@usenetve rwaltung.org> a écrit dans le
message de news:bp******** **@usenet.esken .de...
"Yoyoma_2" <Do********@som ewhere.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
23331
by: Doug | last post by:
Say I have a class with a constant in it. I also have a variable in that class that I would like to set to that constant as the initialization value. Why doesn't the following work? class Square { const DEFAULT_SIZE=5; var $size=self::DEFAULT_SIZE; // how do i set $size to DEFAULT_SIZE? }
3
3020
by: Neil Zanella | last post by:
Hello, It seems to me that using too many variables at class scope in C++ (e.g. private data members) can be just as bad as having a C program with lots of global variables. This is especially true for large classes with lots of methods. The worse part is that even if conventions are used to assign names to class data members to so as to distinguish them from other variables, then it still becomes difficult to tell from the class...
3
1565
by: Andy | last post by:
Someone supposedly knowledgeable tells me that any member function declared in class scope is automatically inlined. I am a little skeptical about this claim. Is this true (I know it cannot be ... I might decide to write a 1000 line function in the class scope). Cheers, Andy
8
2536
by: jose luis fernandez diaz | last post by:
Hi, I am reading Stroustrup's book 'C++ Programming Language'. In the 10.4.9 section (Nonlocal Store) he says: "A variable defined outside any function (that is global, namespace, and class static variables) is initializated (constructed) before main is invoked . . ." .. . .
9
2452
by: Steven T. Hatton | last post by:
It was once suggested to me that I could accomplish much the same thing that modules would accomplish (if C++ had modules) by writing my entire program - except for main() - inside of a class. When it was suggested, I didn't take it very seriously, but I have recently begun wondering if it is an idea worth considering. I'm now trying to think of what fundamental differences might exist between namespace scope, and class scope. One that...
2
3525
by: Michael | last post by:
Hello, I have a newbie question about class scope. I am writting a little program that will move files to one of two empty folders. I am having a hard time understanding scope. So this will be a two part question. Part 1: I have a form with a text box on it that I want to spit out results to. Sample code:
3
5453
by: pelleas | last post by:
Hello to all! 1a. I'm a newbie having trouble accessing a pointer from one .cpp file which is defined within a member function found in a separate .cpp file. I'm looking for a line or two of sample correct syntax, and/or a philosophical hint or two about structuring such requests. Consider the following code snippet from "theFrame.cpp" (written for VC++ 2005 Express), attached. Here, a pointer 'grid' is defined, which is then used a few...
5
2036
by: Steven T. Hatton | last post by:
If find the following excerpt from the Standard a bit confusing: <quote> 3.3.6 - Class scope -1- The following rules describe the scope of names declared in classes. 1) The potential scope of a name declared in a class consists not only of the declarative region following the name's declarator, but also of all function bodies, default arguments, and constructor ctor-initializers in that class (including such things in nested classes).
4
2581
by: AndrewD | last post by:
Hey C++ folks, I created this today, just for fun. You can make object allocation for any class around 6 times faster, simply by doing the following. class MyClass : public TXpQAlloc<MyClass,N> N is a chunking factor (defaults to 10).
3
1822
by: toton | last post by:
Hi, Is it possible to have a class level namespace opening instead of file level . Something like this, I have a long namespace like namespace com { namespace my_company{ namespace my_project{ namespace parser{ class my_parser{ };
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.