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