/    Sign up×
Community /Pin to ProfileBookmark

Session works but then it doesn’t

Hi, I have a simple admin section on a website which uses session variables to check if the user is allowed to enter the area. I take the username and the password and check it in the database. If the record exists I do this –

[code=php]if ($passNum == 0)
$errPass = “The password was incorrect please try again.”;
else
{
$_SESSION[‘guser’] = preg_replace(‘/W/’, ”, $trimUser);
$_SESSION[‘gauth’]= “True”;

// log in successful, redirect to success page
//session_write_close();
header(“Location: http://www.xxxxxxxx.xxxx/xxxx/xxxx.php”);
}
exit;[/code]

I have tried different variations on the theme and used different methods, I have included session_write_close() after the sessions have been created. I have tried to put the exit after the header call, I have commented it out but no luck. I call session_start() right at the top it is the 1st bit of code on this and on other subsequent pages.

What I don’t get is my code was working – but now it isn’t one day it allows me in the next it doesn’t.

To destroy the sessions I have a logoff.php page..

[code=php]// check if the user logged in, if so log them out!!!!
session_start();
if ($_SESSION[‘gauth’] == “True”)
{
echo(“destroying the session!”);
session_unset();
session_destroy();
session_write_close();
}[/code]

The other pages which check if the session exists is similar to the code below:

[code=php]// check if the user logged in….!!!!
session_start();

if ( !$_SESSION[‘gauth’] )
{
echo(“session is not registered! – redirect to index”);
//header(“Location: http://www.xxxx.xxxx/xxxx/xxxx.php”);
}[/code]

Is there something I am missing – working one minute then stops the next… no error messages, nothing. Please help I have been tearing my hair out on this for months!

BTW – The PHP version is 4.4.2 and register_globals is set to “on” if that helps.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@The_Little_GuyApr 20.2006 — Here is how I have made my logout script. Try this, for your logout.
[code=php]<?php
// Initialize the session.

// Unset all of the session variables.
$_SESSION = array();

// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-42000, '/');
}

// Finally, destroy the session.
@session_destroy();

echo 'You have successfully loged out!';

?>[/code]


Also, In your code:
[code=php]if ($passNum == 0)
$errPass = "The password was incorrect please try again.";
else
{
$_SESSION['guser'] = preg_replace('/W/', '', $trimUser);
$_SESSION['gauth']= "True";

// log in successful, redirect to success page
//session_write_close();
header("Location: http://www.xxxxxxxx.xxxx/xxxx/xxxx.php");
}
exit; [/code]

Shouldn't there be {} after your if and before your else?
Copy linkTweet thisAlerts:
@adahulauthorApr 20.2006 — For some reason when I try to FTP the editted file my host tells me that I have exceeded the disk space!!! Could this be related? Once I overcome this problem I can try your suggestion.

Also The {} brackets are not necessary there as it is only one code statement - you would need the curly brackets if you had more than 1 line of code in the if fragment.
Copy linkTweet thisAlerts:
@adahulauthorApr 20.2006 — No that didn't work either - any other suggestions?
Copy linkTweet thisAlerts:
@NogDogApr 20.2006 — Disk space *might* be the problem, as session data gets saved on the server.
Copy linkTweet thisAlerts:
@adahulauthorApr 21.2006 — As I have been developing this for someone else I have told them to contact the host and ask about the disk space problem. When this is resolved I will let you guys know what's happening.

I do have a question though - when a session variable is created does it store this inforation in a file on the server? I would have thought that once the session has been destroyed does it also get rid of it on the server file?
Copy linkTweet thisAlerts:
@NogDogApr 21.2006 — Yes, the session values are stored in a file on the server. There is a time limit defined in php.ini after which the session data may be deleted by the "garbage collection" routine. It's set by the session.gc_maxlifetime in your php.ini file, and the default is 1440 seconds. (See http://www.php.net/manual/en/ref.session.php for more info.)

You could change this value either via .htaccess, or on a script-by-script basis via an ini_set() call prior to your session_start() call.
×

Success!

Help @adahul spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...