Jump to content

In need of cookie help


tourbike

Recommended Posts

Hi I am trying to learn php before my university begins in the fall but I am stuck on cookies.

 

What I want to happen is that when a user comes to a page, a cookie is given to that user and moves the user to another page. On this other page it will check if a cookie has been set and move user to x location, however if no cookie has been set then the redirect send the user back to the page that hands out the cookies.

 

So I created to different pages:

 

1. cookie.php (this hands out the cookie)

code as follows:

 

<?php

 

// Set a cookie

// Cookie name: name

// Cookie value: cookie

// Cookie expire: in 1 hours

 

setcookie ('name', 'cookie', time() + (60*60*1));

 

// Now check that cookie is loaded

error_reporting (E_ALL ^ E_WARNING ^ E_NOTICE);

 

// Check if cookie has been set or not

if ($_GET[$_COOKIE['name']] != 'cookie') {

// Set cookie

setcookie ('name', 'cookie', time() + 60);

 

// Reload page

header ("Location: checkcook.php");

} else {

// Check if cookie exists

if (!empty($_COOKIE['name'])) {

echo "Cookies are NOT enabled on your browser";

}

}

?>

 

page two that checks the cookie and if no cookie exists then

 

2. cookiecheck.php

<?

// check cookie is correct value

if(!isset($_COOKIE['cookie'])) {

header("location:correct_location.php");

} else {

// if failed then go back to page

header ("Location:cookie.php");

}

?>

 

I hope someone can just help where I am going wrong with my thinking thanks

 

tourbike

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.