Forum Moderators: coopster

Message Too Old, No Replies

PHP & Cookies

PHP & Cookies

         

nimonogi

12:05 am on Sep 8, 2006 (gmt 0)

10+ Year Member



Hello,

I'm trying to write a php code that read a cookie and do some actions.
Can you tell me please where i'm wrong?

<?php

$cEC = GetCookie("counExtCookie");

function GetCookie($name)
{
if (isset($_COOKIE[$name]))
return $_COOKIE[$name];
return FALSE;
}

$fullURL = 'http://'.$_SERVER['HTTP_HOST'];
$phpSelf = $_SERVER['PHP_SELF'];

if ($cEC == "") {
if ($fullURL == "http://uk.domain.com/"){
setcookie("counExtCookie", "uk", time()+3600, "/", ".domain.com", "");
}
if ($fullURL == "http://us.domain.com/"){
setcookie("counExtCookie", "us", time()+3600, "/", ".domain.com", "");
}
}

if ($cEC == "eu" && $fullURL!== "http://www.domain.com/"){
header("Location: [domain.com...]
exit;
}

if ($cEC == "uk" && $fullURL!== "http://uk.domain.com/"){
header("Location: [uk.domain.com...]
exit;
}

if ($cEC == "us" && $fullURL!== "http://us.domain.com/"){
header("Location: [us.domain.com...]
exit;
}

?>

Thanks!

ningencat

11:10 am on Sep 8, 2006 (gmt 0)

10+ Year Member



It would help if you would let us know where the problem is exactly :)

I'm guessing the page is not being forwarded where you want?

If thats the case, it could be because setcookie() only runs when the headers are sent, and in the code you have provided you do not send headers till the forwarding.

I would do this:
<?php

$cEC = GetCookie("counExtCookie");

function GetCookie($name)
{
if (isset($_COOKIE[$name]))
return $_COOKIE[$name];
return FALSE;
}

$fullURL = 'http://'.$_SERVER['HTTP_HOST'];
$phpSelf = $_SERVER['PHP_SELF'];

if ($cEC == "")
{
if ($fullURL == "http://uk.domain.com/")
{
setcookie("counExtCookie", "uk", time()+3600, "/", ".domain.com", "");
$cEC = 'uk';
}

if ($fullURL == "http://us.domain.com/")
{
setcookie("counExtCookie", "us", time()+3600, "/", ".domain.com", "");
$cEC = 'us';
}
}
// I'm assuming the following line:
else
{
$cEC = 'eu';
}

if ($cEC == "eu" && $fullURL!== "http://www.domain.com/")
{
header("Location: [domain.com...]
exit;
}

if ($cEC == "uk" && $fullURL!== "http://uk.domain.com/")
{
header("Location: [uk.domain.com...]
exit;
}

if ($cEC == "us" && $fullURL!== "http://us.domain.com/"){
header("Location: [us.domain.com...]
exit;
}

?>