Setcookie > sniff > output on same page

Posted by lokust on Stack Overflow See other posts from Stack Overflow or by lokust
Published on 2010-03-24T19:35:28Z Indexed on 2010/03/24 19:43 UTC
Read the original article Hit count: 417

Filed under:
|
|

Hi, 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.

© Stack Overflow or respective owner

Related posts about cookies

Related posts about php