Page 1 of 1
Session keeps disappering
Posted: Tue Sep 16, 2003 5:48 am
by Telos
I tried to search an answer for this in the earlier topics but couldn't find it so here it is. I tested this system on my own localhost and it works fine but when I uploaded it to my host this problem started. The problem is that it takes about 2-5 minutes and the session becomes obsolete. That is very annoying because you can spend more time in my admin panel than that. And to say again...in localhost it worked fine. Here's how I did it.
I just first start the session with session_start(); and then I see if the the username and password are correct and if they are I session_register some variables like username and userlevel to session. Then at every page I try to see if a $_SESSION['session_userlevel']; still exists and if it does the user is logged in but if it doesn't he's not. So this is the problem. Could it be something related to PHP version? My host just recently updated their PHP version to 4.3.3RC1. If you need more info just ask and hopefully you can help me.
Posted: Tue Sep 16, 2003 6:07 am
by twigletmac
session_register()
If you have something like:
Code: Select all
$variable = $_POSTї'variable'];
session_register($variable);
change this to
Code: Select all
$_SESSION['variable'] = $_POST['variable'];
as session_register() is a deprecated function.
Have a look at Jason's session management sticky:
viewtopic.php?t=6521
Mac
Posted: Tue Sep 16, 2003 6:15 am
by Telos
ok, thanx...somehow I thought there might be a problem something like that but I'll try that and see how it will work then.
Posted: Tue Sep 16, 2003 8:54 am
by Telos
ok, sorry for doublepost but I was celebrating too early. It still looses the session. Here's how I have my code.
login_process.php
Code: Select all
<?php
require_once '../functions.php';
// check that the form fields are not empty, and redirect back to the login page if they are
if(empty($username) || empty($password))
{
redirect('index.php');
}
else
{
$password = md5($password);
// connect to the database
dbConnect();
$sql = mysql_query("SELECT id, nickname, level, userlevel FROM eon_members WHERE nickname = '".$username."' AND password = '".$password."'");
$result = mysql_fetch_array($sql);
$session_nickname = $result['nickname'];
$session_userlevel = $result['userlevel'];
$session_memberid = $result['id'];
$session_level = $result['level'];
//check that at least one row was returned
$rowCheck = mysql_num_rows($sql);
if($rowCheck > 0)
{
//start the session and register a variable
session_start();
$_SESSION['session_nickname'] = $session_nickname;
$_SESSION['session_userlevel'] = $session_userlevel;
$_SESSION['session_memberid'] = $session_memberid;
$_SESSION['session_level'] = $session_level;
//we will redirect the user to another page where we will make sure they're logged in
redirect('index.php');
}
else
{
//if nothing is returned by the query, unsuccessful login code goes here...
echo"
Something went horribly wrong
";
}
}
?>
and this is an example page where you can see how I check the login
Code: Select all
<?php
//start the session
session_start();
require_once 'admintemplate_header.php';
//check to make sure the session variable is registered
if($_SESSION['session_userlevel'] >= 1)
{
}
else
{
//the session variable isn't registered, send them back to the login page
redirect('login.php');
}
require_once 'admintemplate_footer.php';
?>
Posted: Tue Sep 16, 2003 2:52 pm
by m3rajk
q: what is the dev environment? what is the deployment environment?
Posted: Tue Sep 16, 2003 6:03 pm
by JAM
Code: Select all
// check that the form fields are not empty, and redirect back to the login page if they are
if(empty($username) || empty($password))
If your register_globals are off, this if clause will trigger everytime you run the script/page, hence it will never get to the $_SESSION parts.
See the last link in my signature and read about $_POST
Posted: Wed Sep 17, 2003 2:54 am
by Telos
it's not that because a user can login for a while on my admin panel. I think the host just clears out the /tmp folder with crontab every couple of minutes. But I fixed it with cookies so now it works ok.