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

Why dosen't this session work?

Status
Not open for further replies.

Naits

Programmer
Oct 10, 2001
90
0
0
NO
Why dosen't this session work?
login.php:
------------start of file------------------
<?
if ((!$username) && (!$password)) login_form($username, $err_msg);
elseif(($username) && (!$password)) login_form($username, &quot;Missing password!&quot;);
else pass_check($username, $password);

function login_form($username, $err_msg) {
?>
<form action=&quot;<? $php_self ?>&quot; method=post>
<table boder=0 cellspacing=0 cellpadding=1>
<tr><td bgcolor=#000000><table border=0 cellspacing=0 cellpadding=2 bgcolor=#ffffff>
<tr><td colspan=2> Login</td></tr>
<tr><td colspan=2><font color=red><? echo $err_msg ?></font></td></tr>
<tr><td>User Name: </td><td><input type=text size=20 name=username value=<? echo $username ?>></td></tr>
<tr><td>Password: </td><td><input type=pass size=20 name=password> <font size=2>Forgot password?</font></td></tr>
<tr><td></td><td> Remember me</td></tr>
<tr><td colspan=2><font size=2>Not yet a member? register HERE</font></td></tr>
<tr><td colspan=2 align=right><input type=submit value=Login></td></tr>
</table></td></tr>
</table>
</form>
<?
}

function pass_check($username, $password) {
include(&quot;config.php&quot;);
$dbconn = mysql_connect(&quot;$db_host&quot;, &quot;$db_user&quot;, &quot;$db_pass&quot;);
$result = mysql_select_db(&quot;$db_name&quot;, $dbconn);
if ( $result == false )
{
echo mysql_error();
} else {
$sql = &quot;SELECT username, password, on_hold, id FROM accounts WHERE username='$username'&quot;;
$result = mysql_query( $sql );
if ( $result != false )
{
$data = mysql_fetch_assoc( $result );
if (!$data['username']) {
login_form($username,&quot;The username <b>$username</b> is not registred!&quot;);
}
else {
if (!$data['on_hold']) {
if ($password == $data['password']) {
session_start();
session_unset();
session_register(&quot;TesT&quot;);
$TesT = &quot;100% working - JiPPii!!&quot;;
echo &quot;Password accepted<br>Logging you in!&quot;;
?>
<a href='manager.php'>manager</A>
<?
//header(&quot;location: manager.php&quot;);
}
else {
login_form($username,&quot;Incorrect password!&quot;);
}
}
else {
login_form($username,&quot;You cannot login to <b>$username</b> because the account is<br>set on hold. Contact the webmaster for more help!&quot;);
}
}
} else {
echo mysql_error();
}
}
}
?>
-------------end of file-----------

manager.php:
-------------start of file---------
<?
session_start();
echo $TesT;
?>
--------------end of file---------- __________________
Visit my homepage
.: Game universE :.
 
Using session_register($TesT) is good. Assigning the session variable as &quot;$TesT = <value>;&quot;, then recalling the value with &quot;echo $TesT;&quot; will only work if you have registar_globals set to on in php.ini. Is this the case?

If not, and you are running PHP v.4.1.0 or later, try accessing the variable as $_SESSION[&quot;TesT&quot;]. If you have an earlier version of PHP, try accessing it at $HTTP_SESSION_VARS[&quot;TesT&quot;]. ______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
I know my PHP (v4.1.2) is supporting the session_register function because I made a script that just had the session lines from both files and that script worked, but it seem's like login.php dosen't register the variables when I'm having all the other stuff in the script. The same happens with the two other methods.

Does anybody know what's wrong here? __________________
Visit my homepage
.: Game universE :.
 
Check the file where PHP stored the data. Does TesT exist in the file? Does it have a value? ______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
This is the only content of the session file:
!TesT|
Nothing more. What's wrong with my script? __________________
Visit my homepage
.: Game universE :.
 
We know that the session_register is working.

Wait a minute. It's a scoping problem. The variables created by session_register are regular global. You would have to use the global statement in your function. You're gonna have to either rearrange your code, or use the $_SESSION superglobal to set and read the value.

Try: Assigning and reading the value of $TesT through the $_SESSION superglobal variable rather than directly. ______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
Thanks! But what is a superglobal variable? could you please give me an example?
 
It's all a part of PHP's scoping rules.

The PHP manual does a better job of explaining it than I could. Point your browser to

______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
OK I'l check that out. Thanks!
 
Sorry. Hit the &quot;enter&quot; button by mistake.

When you performed session_register on the $TesT, that made it global. It would be available to anything in the main body of the script. When you reference $TesT in your function and not use the global directive on it first, the function instantiates the variable locally to the function. This variable, though named the same as the global, is a different variable than the $TesT outside the function.

The superglobals are special. PHP has been told that the superglobal variables are available everywhere, even inside a function. ______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
Also, you may want to take a look at the faq I wrote:
faq434-2036

I briefly explain autoglobals and using the $_SESSION array.
 
I got it working now. This is how I did it:
Just change: $TesT = &quot;100% working - JiPPii!!&quot;;
to: $GLOBALS['TesT'] = &quot;100% working - JiPPii!!&quot;;
 
I strongly recommend using $_SESSION. Although the data is available in both variables, using $_SESSION makes it a lot more clear what you are doing. That creates much more maintainable code.

It may or may not matter in this file, but it's a good habit to get into. ______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
I was trying to use $_SESSION at once but I figured out that my PHP dosen't support it even tought my PHP version is 4.1.2 ......
 
Read my faq. When you use $_SESSION, you DO NOT use session_register() and you always set and retrieve data by the $_SESSION autoglobal array.

My FAQ:
FAQ434-2036
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top