[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 973
  • Last Modified:

switching between $_SESSION and $HTTP_SESSION_VARS

I am maintaining a big PHP application.

To fix a bug I removed the deprecated function call session_register() and replaced it with the $_SESSION superglobal, as per advice on php.net (under session_register() manual page).

However I now note that the rest of the code uses $HTTP_SESSION_VARS to access the session vars, not $_SESSION !!

It looks like I made a booboo here, as the session vars I set via $_SESSION will not be in $HTTP_SESSION_VARS automatically (unless I place them there explicitly). Correct ?

I need to know the answer to this, as if the answer is NO, then I don't need to worry about the rest, but if the answer is YES (as I strongly suspect) then what follows becomes important...

If the answer is YES, then considering that there are 200+ accesses of $HTTP_SESSION_VARS in 30+ files, rather than change all them yet, I'm tempted to change the $_SESSION access to $HTTP_SESSION_VARS (only one access as its inside a wrapper function, that gets called multiple times).

This raises a new issue. $HTTP_SESSION_VARS is not a true superglobal, so can it be accessed from inside a function?

many thanks!
0
zorba111
Asked:
zorba111
  • 8
  • 2
4 Solutions
 
Beverley PortlockCommented:
Putting a value into $_SESSION does not populate $HTTP_SESSION_VARS

Since there are 30 scripts, would a global replace not do the job quite quickly? An afternoon should do 30 scripts. If you're handy with BASH and SED then an hour might do it.
0
 
a1aaitCommented:
Probably best to update your var names now while you are already working on it.
0
 
zorba111Author Commented:
@bportlock

thanks for the clarification - suspected as much, but good to get a 2nd opinion. Didn't want to go changing stuff if I didn't need it.

Would rather change 1 file than 30. Using dreamweaver so the replace would be no probs, but I have already various outstanding (i.e. not checked in / uncommitted) changes, so would like to deal with them first, as the 30 files are likely to need merging with the other changes, whereas 1 file = no probs.

The question then is how to use $HTTP_SERVER_VARS inside a function.

While I was waiting I tried this little experiment (see code panel), and the output was:

A
A
C

Which shows that if I use the global keyword, I can access $HTTP_SERVER_VARS in a function.

n'est ce pas?
<?php
session_start();

function set_sesh_vars_B() {
	$HTTP_SESSION_VARS['fred'] = "B";
}

function set_sesh_vars_C() {
	global $HTTP_SESSION_VARS;
	$HTTP_SESSION_VARS['fred'] = "C";
}

$HTTP_SESSION_VARS['fred'] = "A";
echo $HTTP_SESSION_VARS['fred']."\n";

set_sesh_vars_B();
echo $HTTP_SESSION_VARS['fred']."\n";

set_sesh_vars_C();
echo $HTTP_SESSION_VARS['fred']."\n";
?>

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
zorba111Author Commented:
BTW once I clear my decks I WILL go back and do a global replace from $HTTP_SESSION_VARS to $_SESSION.

Just want a way to quickly prevent any issues while I clear another few bugs I'm working on.... hence emphasis on the 1file "quick fix" :-)
0
 
zorba111Author Commented:
enhanced example, outputs:

A
local version = B
A
C

<?php
session_start();

function set_sesh_vars_B() {
	$HTTP_SESSION_VARS['fred'] = "B";
	echo "local version = ".$HTTP_SESSION_VARS['fred']."\n";

}

function set_sesh_vars_C() {
	global $HTTP_SESSION_VARS;
	$HTTP_SESSION_VARS['fred'] = "C";
}

$HTTP_SESSION_VARS['fred'] = "A";
echo $HTTP_SESSION_VARS['fred']."\n";

set_sesh_vars_B();
echo $HTTP_SESSION_VARS['fred']."\n";

set_sesh_vars_C();
echo $HTTP_SESSION_VARS['fred']."\n";
?>

Open in new window

0
 
zorba111Author Commented:
below is now my application code, after $_SESSION changed to $HTTP_SESSION_VARS, and global keyword used...
(apols for verbose comments - learning as I go along and find it useful to keep these in for a while till it all becomes as obvious as "day follows night")

...now I'm nervous about using unset() on $HTTP_SESSION_VARS, as I read that:

"If a globalized variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called....
If you would like to unset() a global variable inside of a function, you can use the $GLOBALS array to do so: " (php.net)

Hmmm, not sure how I would reference the $HTTP_SESSION_VAR inside the GLOBALS array. Let me cogitate on that one....

Might have to check in those 30 files after all :-)


function bx_session_register($variable) {
    //return session_register($variable);
    // nowadays should use $_SESSION[$variable]
/* from php.net:
// Use of session_register() is deprecated
$barney = "A big purple dinosaur.";
session_register("barney");

// Use of $_SESSION is preferred, as of PHP 4.1.0
$_SESSION["zim"] = "An invader from another planet.";

// The old way was to use $HTTP_SESSION_VARS
$HTTP_SESSION_VARS["spongebob"] = "He's got square pants.";
*/
/*
	$nb_args=func_num_args();
    $arg_list=func_get_args();
    for($i=0;$i<$nb_args;$i++) {
        global $$arg_list[$i];
        $_SESSION[$arg_list[$i]] = $$arg_list[$i];
    }*/
	// code above off comments under session_register()
	
	global $$variable;
	// If £variable = for example 'userid', then £userid will be seen as local
	// but we want to refer to the one in the global scope = use GLOBAL keyword
	// NB not sure if this will work for variable declared in calling scope, but
	// NOT the global scope...

	global $HTTP_SESSION_VARS;
    $HTTP_SESSION_VARS[$variable] = $$variable;
	// opportunity to use variable variables as per p86 of Gilmore book
}


function bx_session_is_registered($variable) {
	//return session_is_registered($variable);
	global $HTTP_SESSION_VARS;	
	return isset($HTTP_SESSION_VARS[$variable]);
}


function bx_session_unregister($variable) {
	//return session_unregister($variable);
	global $HTTP_SESSION_VARS;	
	unset($HTTP_SESSION_VARS[$variable]);
}

Open in new window

0
 
zorba111Author Commented:
COMMENT: I'm beginning to see that $HTTP_SERVER_VARS is a pain in the b*** to use...

no wonder its being deprecated !!!
0
 
zorba111Author Commented:
there we go, just completed checking in 45 files containing 220+ instances of $HTTP_SERVER_VARS replaced with $_SESSION. Not as simple as it sounds, cos we have a test local dir, test remote dir, live local dir, live remote dir, and repository... long story....
0
 
Beverley PortlockCommented:
LOL!

I think you should award yourself the points for this one! In fact this would almost make a nice little EE Article.

I understand your reluctance to do the changeover as it looks like a lot of disruption. We have several old pieces of code here that we would dearly love to ditch, but as you say, the effort is huge.

I hesitate in offering the following idea because it is a complete bodge and I hate those, but sometimes.....

What about adding a bit of code near the top of each script that simply writes $_SESSION into $HTTP_SESSION_VARS one variable at a time? Basically instead of writing to $_SESSION directly you use a small function instead that writes to BOTH arrays. Then as you work your way through the system hopefully you can go one script at a time. Eventually when you are done, comment out the bit in the function that does HTTP_SESSION_VARS.  Something like this

function writeSession( $name, $value ) {
     global $HTTP_SESSION_VARS;
     $HTTP_SESSION_VARS [$name] = $value;
     $_SESSION[$name] = $value;
}

To "unset" a value just clear it

writeSession("variableToClear", "");

Far from ideal, but "... needs must when the devils drives"
0
 
zorba111Author Commented:
@bportlock:

thanks! that is a good idea and I can use it again.

Not sure if my last post (3am) communicated well (might have been something to do with the time of night), but I went ahead and bit the bullet and made the changes. Thankfully none of the files I was already working on (or very few) were also in the subset that needed the find and replace.

It was a pain, as I had to synchronise local and remote sites (and our hosters had "touched" some of our remote files so dreamweaver thought they were out of synch - grrrr!!!!), but we got there !

Will share points between u & me as u answered the first part of my Q, and I answered the 2nd part.
0
 
zorba111Author Commented:
Just for completion, I'd love to know how to do this bit (see above)

"Hmmm, not sure how I would reference the $HTTP_SESSION_VAR inside the GLOBALS array. Let me cogitate on that one...."

At this stage its purely academic as I've already gone down the $_SESSION route. Its more intellectual curiosity really....
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now