Makeing a persistent cookie

Hello all,

I would like to know how to make a persistent cookie.

I would like to use it on my entire website, the cookie should do the following:

First (when there is no cookie yet).
Show a .php page (ex: index.php).
Then store a cookie that expires after one hour.
When the cookie expired and visitors visit any of my webpages,
I want it to show the index.php page again,
and so on

Hope anyone could help me out, thanks in advance! :slight_smile:

There is plenty out there in google land. Just search for it!

Hey thanks for your reply, actually a have been using google but really didnt find much usefull.
I searched for stuff like:“How to make a persistent cookie”, maybe u can suggest some better keywords.
I found this website when i was searching Google. =)

What is your definition of a persistent cookie? Never heard the term before. Since cookies have an expiration date, YOu can make them last for quite some time or until th euser deletes them all. What do you want to achieve?

Well thats what i found out about cookies here: http://www.allaboutcookies.org/cookies/cookies-the-same.html
What I want to achieve is in my first post.

Well, that is not a very persistent cookie. You just want a basic time-out cookie. So what’s the difficulty you are having?

I have no idea how to send it to the user, probably by a script on my webpage, and i have no idea how to make the cookie.

This should do the job (hope so anyway).

This should go at the top of your pages. A better option would be to make a page called cookie.php, put the “cookie.php” code below in it and include it on every page.

include ("cookie.php");

Note: that this should be called before anything is sent to the browser.

I set a “test” time of 10 seconds. Change it to 3600 when testing is done. The constant NUM_HOURS is set at 1 at the top of the page. Change as needed.

Also note, this is a stand alone option that calls the function from the same page, which should work if all pages are within same directory as index.php.
IF however you are using multiple directories where index.php is not located, then you should:

  1. Remove the part that CALLS the function and put it on each page adjusting the path to index.php accordingly.
  2. Remove the place where the path to the index.php is set to variable $SiteURL.

How function is called(bottom of cookie.php)


if (!isset($_COOKIE[COOKIE_KEY])){
	cookieLogin($SiteURL);
}

cookie.php

<?php
	//Cookie.php
	///////////////////////////////	
	/// Cookie Settings
    const COOKIE_KEY = 'HomePageCookie';
    const NUM_HOURS = 1; // number of hours key is valid
	///////////////////////////////	
	/// Path to redirect page
	$SiteURL = "index.php";	
	///////////////////////////////	

	function cookieLogin($SiteURL) {
		// load cookie
	       if (isset($_COOKIE[COOKIE_KEY])) {
	           $parts = explode('|', $_COOKIE[COOKIE_KEY]);
	
	           $Token     = $parts[0];
	           $sessionKey = $parts[1];
	       } else {
	           $Token = $sessionKey = false;
	       }
	
	       // No cookie at all
		if (empty($sessionKey) || empty($Token)) {
			/////////////////////////////////		
		 	function generateKey() {
		        // get the largest 999999999 we can rand to.
		        $max = (int)str_pad('', strlen(mt_getrandmax()) - 1, 9);
		        $min = (int)str_pad('1', strlen($max), 0, STR_PAD_RIGHT);
		
		        $key = '';
		
		        while (strlen($key) < 39) {
		            $key .= mt_rand($min, $max);
		        }
		
		        return substr($key, 0, 39);
		    }
			/////////////////////////////////	
		 	function generateToken() {
		        // get the largest 999999999 we can rand to.
		        $max = (int)str_pad('', strlen(mt_getrandmax()) - 1, 9);
		        $min = (int)str_pad('1', strlen($max), 0, STR_PAD_RIGHT);
		
		        $key = '';
		
		        while (strlen($key) < 4) {
		            $key .= mt_rand($min, $max);
		        }
		
		        return substr($key, 0, 4);
		    }
			/////////////////////////////////	
	
	        $key = generateKey();
	        $Token = generateToken();
			
	        // Store key in cookie
	        setcookie(COOKIE_KEY,
	                "$Token|$key", 	
					//FOR TESTING set time for 10 seconds
	                time() + (10 * NUM_HOURS));	
					//ELSE USE 3600
	                //time() + (3600 * NUM_HOURS));
		header("location: $SiteURL");
		exit;
		}
	}

if (!isset($_COOKIE[COOKIE_KEY])){
	cookieLogin($SiteURL);
}
?>

You may not even need all that’s here as I was using part of this in another program, saving keys and tokens in DB for match but it should do the job.

Thank you verry much!
Well i use a vBulletin forum, do i need to place it in every single .php file?

Well you said you wanted them to be able to go to any page, so a link-to or I should say include should be on every page like so.

include ("cookie.php");
    ///////////////////////////////
    /// Path to redirect page
    $SiteURL = "index.php";
    ///////////////////////////////
if (!isset($_COOKIE[COOKIE_KEY])){
    cookieLogin($SiteURL);
}  

Note: Remove above lines from cookie.php