Question

I wonder if someone can help shed some light on this:

I drop a cookie if a user arrives to the site with a specific key/value in query string. i.e.: http://www.somesite.com?key=hmm01

The cookie code exists at top of the template before <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ) :

<?php header("Content-Type: text/html; charset=utf-8"); ob_start(); if (isset($_GET['key'])) { setcookie("cookname", $_GET['key'], time()+2592000); /* Expires in a month */ } ob_end_flush(); ?>

On the same page though within the : I have the following php code that sniffs the cookie and outputs some text:

`

switch ($cookievalue) { case hmm01: echo "abc"; break; case hmm02: echo "def"; break; case hmm03: echo "ghi"; break; default: echo "hello"; } ?>`

-- Problem is when the user first arrives the sniffer script doesn't detect the cookie and outputs the default text: hello Only when user refreshes page or navigates to a different page does the sniffer detect the cookie.

Any ideas on how to drop the cookie and output the correct text without a page refresh?

Many thanks.

Was it helpful?

Solution

Simply check $_GET for the first time:

if(isset($_GET['key'])) { $cookievalue = $_GET['key']; }
if(isset($_COOKIE['cookname'])) { $cookievalue = $_COOKIE['cookname']; }

switch ($cookievalue) { case hmm01: echo "abc"; break; case hmm02: echo "def"; break; case hmm03: echo "ghi"; break; default: echo "hello"; } ?>

OTHER TIPS

if (specific key/value in query string) $_COOKIE['cookname'] = "some value";

And learn some HTTP

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top