Reputation: 968
I need to cache a page only if a determined PHP session is set.
To do that, I'm using the following code:
session_start();
if(isset($_SESSION['bot'])){
$etagFile = md5_file(__FILE__);
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 31556926) . " GMT");
header("Etag: $etagFile");
header('Cache-Control: private');
die('You\'ve been banned.');
}
echo 'Some content';
The problem is that, when session expires, user will see 'Some content', while he should see "You've been banned" for a year.
error_reporting(E_ALL);
does not return anything.
What could be the problem?
Upvotes: 0
Views: 113
Reputation: 4373
There is no bug in the code but a logic flaw. How are you implementing the "session expiring" behavior?
When you use a PHP session you are basically using cookies. If the user deletes the cookie or if you end the session by destroying it you lose track of the user. The session is gone with all the session variables and you will need to recreate it somehow.
I can't tell much without knowing what you want to do. But to ban an user you need to you use a login system preferably.
UPDATE: it seems you are trying to ban a bot. You shouldn't use session for that. Your best bet is to use an IP address and check the headers of the request for a bot identifier (in case it's a polite bot it will identify itself)
Upvotes: 1