开发者

Setcookie > sniff > output on same page

开发者 https://www.devze.com 2022-12-24 05:28 出处:网络
I wonder if someone can help shed some light on this:开发者_Go百科 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

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

开发者_Go百科

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.


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"; } ?>


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

And learn some HTTP

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号