homepage Welcome to WebmasterWorld Guest from 54.166.228.100
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Session Problem in PHP
Apache : 1.3.26 PHP : 4.0.5
T Suresh Babu




msg:1312056
 9:44 am on Jul 24, 2002 (gmt 0)

While handling session variables unexpected results occurs.
First time a page is accesed session variable is not set.
Second time the same page executes fine.

What is the remedy?

 

Zunixx




msg:1312057
 10:24 am on Jul 26, 2002 (gmt 0)

Please be more specific, I'm not sure I really understand the question.

php sessions dont start automatically, you have to start them once with session_start();

rewboss




msg:1312058
 7:04 pm on Jul 29, 2002 (gmt 0)

Session variables are not set until the entire PHP document has been parsed.

I ran into a more subtle variation on this, which mystified me for ages. Take this code:

<?
session_start();
session_register("my_variable");
$my_variable="Hello world";
header("Location: next.php");
exit;
?>

The session variable will never be set. Why? Because the browser is redirected before the end of the script has been parsed. If you've ever wondered why bulletin boards usually display a "Your message has been sent" page before redirecting with a <meta> refresh... that's the reason.

transistor




msg:1312059
 5:15 pm on Jul 30, 2002 (gmt 0)

Are you sure?
I believe it is because it should be like this:
<?
session_start();
$my_variable="Hello world";
session_register("my_variable");
header("Location: next.php");
exit;
?>
Fist assign the value, then register the variable.
Works for me all the time.
You might try without the session_start(), I never use it and everything works all right.

rewboss




msg:1312060
 5:56 pm on Jul 30, 2002 (gmt 0)

It doesn't matter if you register the variable before or after you set it. I can't think how your session variables can possibly work without session_start() -- they're not supposed to.

transistor




msg:1312061
 11:52 pm on Jul 30, 2002 (gmt 0)

Of course it works! I tell you, I do it all the time! :)
Here, from the PHP.net documentation:
If session_start() was not called before this function (session_register) is called, an implicit call to session_start() with no parameters will be made.

[php.net ]

And yes, the order is imporant, because you can't change the value of a variable after registering it.
Have you tried this?

<?
session_register("variable");
unset($variable);
session_unregister("variable");
$variable = "foobar";
session_register("variable");
header("location: next.php");
exit;
?>

-- next.php --

<?
session_register("variable");
echo $variable;
?>

This must work!
Cheers!

kenetiks




msg:1312062
 5:56 pm on Jul 31, 2002 (gmt 0)

I change the values of registered variables all the time.

page1;

<?php
session_start();
$myVAR = "23";
session_register("$myVAR");
?>

page2;

<?php
session_start();
$myVAR++;
?>

page3;

<?php
echo "$myVAR"; // returns 24
?>

You can use one page for all this using conditional statements or switch.

But as far as I know it always works for me with no problems, unless you try to re-register the variable.

rewboss




msg:1312063
 11:33 am on Aug 2, 2002 (gmt 0)

Of course it works! I tell you, I do it all the time!
Here, from the PHP.net documentation: If session_start() was not called before this function (session_register) is called, an implicit call to session_start() with no parameters will be made.

Ah, but only if your script includes session_register(). It will often be the case that you want to use session variables on a page that doesn't register any; in which case, you must have a call to session_start()

Also, of course you can change the values of a registered session variables. Session variables are variables, which means the values they contained can be changed at any time -- that's what "variable" means. However, I have discovered that if you redirect the browser, under certain conditions which have little to do with the placement of session_register() (I know, because my first thought was that I'd put it in the wrong place, so I moved it), the value of the variable as recorded in the text file that stores session variables is not updated.

Nick_W




msg:1312064
 11:41 am on Aug 2, 2002 (gmt 0)

Yes, you definately change session vars after registering them.

That's kind of one of the key points.

Nick

transistor




msg:1312065
 7:58 pm on Aug 2, 2002 (gmt 0)

Yes, yes! I'm sorry, bad boy! bad boy! :)
They can be changed.
What I wanted to say was, that session variables cannot be changed by the user, that is, if you have a session_register("variable") and its value is 3, and you load a page [domain.com...] $variable will be 3.
I am right on this, am I not?

And if you change a variable after registering it and then, go to another page, the new page will reflect the new value?
I mean:
-- page1.php --
<?
session_start();
$variable = 3;
session_register("variable");
$variable++;
header ("location: next.php");
exit;
?>

-- rext.php --
<?
session_start();
session_register("variable");
echo $variable;
?>
--
It prints 4!
Sessions are so cool!

Nick_W




msg:1312066
 7:04 am on Aug 4, 2002 (gmt 0)

Yes, they are!

No need for the session register on the next page BTW.

Also, there are times when you want a var passed in the url to affect the session: Very easy:

if(£varInUrl="yes") {
$sessionVar="yes";
} else $sessionVsr="no";

Just be careful with sessions and SE's, be aware that it's easy to fall into a 'duplicate content trap'.

Nick

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved