PHP and session

windows

Guest
Sup

I have a login page with two variables

userid and password

from there I am forwarded to the login.php page where I start a session and the variable userid is registered using

session_register("userid"); If login is successful,
Im forwarded to an index.php page

In my index page I run query based on the session variable userid, but I'm not getting the userid in the index.php page

Am I doing something wrong? Or how do I retrieve that session variable in the index page?Ok sorry to say this but how is this PHP 5 related it shouldnt really be in this forum.

However how are you trying to get the userid value?Pardon me, didnt realize that I was in the wrong section.

That is my question, how do I refer to or get that value of userid, that I registered with the session?Ok well firstly if your using PHP 5 i would keep away from using session_register();

Use the $_SESSION to initate the variable e.g.


$_SESSION['userid']=1;


From there accessing it like so


echo 'Your userid is '.$_SESSION['userid'];


To destroy the session


unset($_SESSION['userid']);


Do not unset $_SESSION as that will delete the sessions variable.

If you rather not use that you can access it if I remember correctly be using the session name as a variable in your case


echo $userid;


However using session_register and using the variable in that way can cause some harm to the program if you have a variable of the same name.I tried that and it seems to do nothing.

The userid is part of a select statement to MySQL:
The results come up blank as if the value of $userid is not there.

$flavor = "SELECT flavor FROM users WHERE userid = '$userid'";

Thanks alot for the help thoHave you assigned the variable a value or have you only registered it as a session variable? if you have only registered it as a session variable then you will need to assign it a value


session_register("userid");
$userid = 1;


However like i said its best if you use $_SESSIONstill not working. Still clueless.what is the setting for session.autostart in your php.ini?

If it's 0, try adding session_start(); to the top of your page and see what happens.

<!-- m --><a class="postlink" href="http://us2.php.net/session_start">http://us2.php.net/session_start</a><!-- m -->

Also, FYI, session_register will not work as intended with register_globals off.

<!-- m --><a class="postlink" href="http://us2.php.net/manual/en/function.session-register.phpOriginally">http://us2.php.net/manual/en/function.s ... Originally</a><!-- m --> posted by goldbug
what is the setting for session.autostart in your php.ini?

I dont know what this is, and I am connecting remotely to a server under UNIX at my school

If it's 0, try adding session_start(); to the top of your page and see what happens.

I tried this and still the same.

<!-- m --><a class="postlink" href="http://us2.php.net/session_start">http://us2.php.net/session_start</a><!-- m -->

Also, FYI, session_register will not work as intended with register_globals off.

<!-- m --><a class="postlink" href="http://us2.php.net/manual/en/function.session-register.php">http://us2.php.net/manual/en/function.s ... gister.php</a><!-- m -->

Thanks for the tips tho, Im a keep tryinAs of PHP4.2.x and upwards


...
session_register(...);
...


Is not a requirement, you write a session variable with


# write
$_SESSION['userid'] = 1;
...
# read
$userid = (int) $_SESSION['userid'];


A session is not GLOBAL either as of PHP4.2.x, regardless of what setting you have for REGISTER_GLOBALs, so if your using


...
echo $userid;
...


Is obviously going to be empty isn't it? This was a security threat that was eliminated as of PHP4.2.x and upwards btw.
 
Back
Top