Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-07-08 15:58:03

driz
Member
From: Huddersfield, UK
Registered: 2008-03-18
Posts: 441
Website

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

#2 2009-07-08 16:33:46

MattD
Plugin Author
From: Monterey, California
Registered: 2008-03-21
Posts: 1,254
Website

Re: PHP Cookie/Session Help

Is this for users logged into Textpattern? Otherwise they won’t have a session.


My Plugins

Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker

Offline

#3 2009-07-08 16:39:31

driz
Member
From: Huddersfield, UK
Registered: 2008-03-18
Posts: 441
Website

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

#4 2009-07-08 16:41:50

MattD
Plugin Author
From: Monterey, California
Registered: 2008-03-21
Posts: 1,254
Website

Re: PHP Cookie/Session Help

Did you ever get the browser warning cookie thing working? It did pretty much the same thing right?


My Plugins

Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker

Offline

#5 2009-07-08 16:44:40

driz
Member
From: Huddersfield, UK
Registered: 2008-03-18
Posts: 441
Website

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

#6 2009-07-08 17:39:08

juanjonavarro
Plugin Author
From: Valencia, Spain
Registered: 2005-05-16
Posts: 485
Website

Re: PHP Cookie/Session Help

Driz, your code works on my installation

Offline

#7 2009-07-08 18:25:29

driz
Member
From: Huddersfield, UK
Registered: 2008-03-18
Posts: 441
Website

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

#8 2009-07-08 18:30:41

juanjonavarro
Plugin Author
From: Valencia, Spain
Registered: 2005-05-16
Posts: 485
Website

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

Board footer

Powered by FluxBB