Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session variable - pulling hair out!

Status
Not open for further replies.

salewit

Programmer
Oct 31, 2002
58
US
I've got ONE session variable that doesn't seem to want to pass over.


file1.php

<?php
session_start();
$ordertotal = $total + $ship + $tax;
$ordertotal = round($ordertotal,2);
printf ("%01.2f",$ordertotal);
session_register("ordertotal");
print "Ordertotal=$ordertotal";
session_register("foo1");
session_register("foo2");
session_register("foo3");
header ("Location: file2.php");
exit;
?>

file2.php

<?php
session_start();
print "Ordertotal=$ordertotal";
print "Foo1=$foo1";
print "Foo2=$foo2";
print "Foo3=$foo3";
?>

File1 displays:
$29.99
Ordertotal=29.99

File2 displays:
Ordertotal=
Foo1=whatever
Foo2=whatever else
Foo3=whatmnot

Now I realize that this can't run exactly like this, but this is the gist of what I'm doing. The variable $ordertotal just seems to dissappear even though I confirmed it's there just before going into the other module. I checked the punctuation 100 times. What's even weirder, is if I insert:

$ordertotal="29.99";

right before the session_register, it DOES get carried over. This leads me to believe it's something I'm doing in the manipulation of the variable. BTW when I use gettype("ordertotal") I get "string" returned.

Thanks
 
i assume that register_globals is switched off. php.net advises that session_register will not work when register_globals is switched off.

php.net suggests that you use $_SESSION['ordertotal'] = $ordertotal; to set the variable and the same to reference it.

if register_globals is switched on in your code i would have thought that the session_register directive would create a *new* variable called $ordertotal which would overwrite the existing variable and be empty. if you reset the value of the $ordertotal (in the sesion register) afterwards you migth find it worked again. BUT NOT: register_globals is a security hole.
 
Thanks for the response. I checked my server and register_globals is ON.

session_register has not created a new variable on my other ones. I've got about 30 session variables, and they all work fine except this one. I get the impression that either the printf or doing the math on the variable is causing some weird thing to happen. I don't know....

I'm not very familiar with $_SESSION, but I'll give it a try.


PRG1
$foo = "teststring";
session_register("foo");

PRG2
print "$foo";


teststring
 
I've just about had it. I can't figure this out. I systematically commented out areas until I got to the math part. I changed:

$ordertotal = $total + $ship + $tax;

to:

$ordertotal = ($total + $ship + $tax);

and it works.... every other time! I close and reopen my browser and it doesn't work. I click back and refresh and then it does work. I've also tried changing to $_SESSION. Same exact thing. I change variable names, same thing. I forced $total, $ship and $tax to doubel variables and same thing.

I'm perplexed!
 
closing your browser destroys the session. it's designed to do that. if you want to change the behavious you need to use a custom designed session management system that uses cookies.
 
Noboday has said this yet, but:
There's a good reason that register_globals is set to OFF by default. It is much more secure that way.
If you ever want to port your code to another system and it is designed with register_globals ON you'll run into a lot of trouble. No serious professional programmer will write code for register_globals on, and if, he'll have a hard time to find a job.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top