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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session Problems

Status
Not open for further replies.

ronnyjljr

IS-IT--Management
Joined
Nov 23, 2003
Messages
249
Location
US
I keep getting a fatal error when I do this:

$a = $_SESSION[$session_name];
$b = $_SESSION['secondcount'];
$a += $b;
$_SESSION[$session_name] = $a;

Why?

After a while of studying Engineering at PSU you just want to sit down and eat a bowl of cereal. Thats all you really want.
 
Well Specifically
it is on the line with $a += $b, Apparently I can not add these session variables together? They are both numbers I have no idea why it would be doing this.

-Ron

After a while of studying Engineering at PSU you just want to sit down and eat a bowl of cereal. Thats all you really want.
 
if you output $_SESSION[$session_name] and $_SESSION['secondcount'] to the screen, do they show up as the values you expected?
 
LaundroMat:
hmmm, I think your right. session_name returns the current name of the session according to the php docs
ronnyjljr:
Try renaming your variable $session_name to something else
 
Hi Guys!

Thanks for all the replies. I think my server was acting up but here was one solution:

$tmp = "second";
$_SESSION[$session_var] = $_SESSION[$tmp];

instead of
$_SESSION[$session_var] = $_SESSION['second'];

Secondly, another question?

What is it that you can't access _SESSION vars directly? You have to load them in to seperate vars in order to use them. For Example:

$temp = $_SESSION['mofo'];
echo "$temp"; --> This Works


echo "$_SESSION['mofo'];"; --> This Doesn't? I get an error referring to _STRING vars.

Any ideas?

After a while of studying Engineering at PSU you just want to sit down and eat a bowl of cereal. Thats all you really want.
 
I see the problem.

It's not that you can't manipulat the elements of $_SESSION. As far as your PHP code is concerned, it's just another associative array.

The problem as demonstrated by this line:

echo "$_SESSION['mofo']";

is the fact that you are trying to reference an associative array inside quotes. PHP doesn't like that, and will usually throw a parse error on the line.

The following line will work:

echo $_SESSION['mofo'];


I recommend strongly that you use quotes only as the delimiters for string literals. Although like perl, PHP will interpolate variables inside double-quotes, it can get you into trouble, as the associative array reference from your code demonstrates.


Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top