Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
PHP Cookie/Session Help
Hi, I’m testing out this code. The idea is for it to test for a cookie. If the condition is true then run the page as normal, IF NOT then include my warning.php (will use a TXP FORM when I get this working) page and EXIT the current page right there!
Here is the code for index.php
<?php
$warned = isset($_COOKIE['warned']) ? $_COOKIE['warned'] : $_GET['warned'];
if($warned != 'true') {
include('warning.php');
exit;
}
else {
if(!isset($_COOKIE['warned'])) {
setcookie('warned', 'true');
}
}
?>
<html>
<head>
<title></title>
</head>
<body>
<h1>If you can see this then you have the cookie!</h1>
</body>
</html>
and here is the warning.php file that will be shown IF the user has not yet being warned
<html>
<head>
<title></title>
</head>
<body>
<h1>If you can see this then you DON'T have the cookie!</h1>
<p><a href="<?php echo $_SERVER['PHP_SELF']; ?>?warned=true">Click here for the cookie</a></p>
</body>
</html>
At the moment it does not work. Could you please point me in the right directions on how to get this to work and if need be any enhancements to improve the efficiency of the code. Also could you also share how I could easily edit this to use Sessions instead of cookies, so I can learn both ways of doing this. Thanks
Last edited by driz (2009-07-08 16:32:35)
~ Cameron
Offline
Re: PHP Cookie/Session Help
Is this for users logged into Textpattern? Otherwise they won’t have a session.
Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker
Offline
Re: PHP Cookie/Session Help
MattD wrote:
Is this for users logged into Textpattern? Otherwise they won’t have a session.
No. This is to check if they have agreed to something. So for example agree that the site is for 18+ or something and then create the cookie that knows that they have been before and been warned.
~ Cameron
Offline
Re: PHP Cookie/Session Help
Did you ever get the browser warning cookie thing working? It did pretty much the same thing right?
Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker
Offline
Re: PHP Cookie/Session Help
MattD wrote:
Did you ever get the browser warning cookie thing working? It did pretty much the same thing right?
:) You have a good memory. Well that used code I disliked by wrapping the HTML in braces. I’m trying to just check if a cookie exists and if not getting the code to exit right there and show an alternate page. Trying to learn from the newer example.
Last edited by driz (2009-07-08 16:44:54)
~ Cameron
Offline
Offline
Re: PHP Cookie/Session Help
juanjonavarro wrote:
Driz, your code works on my installation
Yes the code works in that it does NOT throw errors, but it doesn’t work as it should do. It needs to check for the cookie and then allow a user to first set the cookie before the normal page will execute.
~ Cameron
Offline
Re: PHP Cookie/Session Help
driz wrote:
Yes the code works in that it does NOT throw errors, but it doesn’t work as it should do. It needs to check for the cookie and then allow a user to first set the cookie before the normal page will execute.
This is just how it works here :-)
Offline
Pages: 1