Forum Moderators: coopster

Message Too Old, No Replies

Problem with PHP and Cookies

Problem with PHP and Cookies

         

nimonogi

9:04 am on Apr 13, 2008 (gmt 0)

10+ Year Member



Hello,

I'm trying to set a cookie based on the url and then include a file based on the cookie but i'm having some problems.


if ($_GET["currency"] = "gbp") {
setcookie("currgbp", "gbp", time()+3600, "/", ".exaxmple.com", "");
}
elseif ($_GET["currency"] = "eur") {
setcookie("currgbp", "gbp", time() - 3600, "/", ".example.com", "");
}

if (isset($_COOKIE["currgbp"])){
$curr = "gbp";
} else {
$curr = "eur";
}

$p = $_GET["page"];
if (strpos($p,"..")) {
die("Bad page request");
}
if (!$p) $p = $curr;
$content_file=$_SERVER["DOCUMENT_ROOT"]."/".$p.".php";
if (!file_exists($content_file)) {
header("Location: {$_SERVER["PHP_SELF"]}");
exit();
}
include($content_file);

Normally with this when someone enter url: domain.com?currency=gbp the cookie "currgbp" will be set and the file "gbp.php" will be included.

The files are included successfully if the cookie is set correctly.
Can anyone take a look and tell me what i have done wrong with cookies?

Thanks in advance.

[edited by: eelixduppy at 3:11 pm (utc) on April 13, 2008]
[edit reason] exemplified [/edit]

cameraman

5:27 pm on Apr 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is here:
if ($_GET["currency"] = "gbp") {

With a single equal sign you're setting the value. To do a comparison, use a double equal sign:
if ($_GET["currency"] == "gbp") {

Don't forget to change the elseif as well.

nimonogi

5:43 pm on Apr 13, 2008 (gmt 0)

10+ Year Member



Thanks for your help. I've already change this and seems to work.
However, the browser needs to be reloaded to work as it should.
When i enter the url in the browser the cookie is set correctly but i'm not getting the new included file. When i reload the browser it works.
Any ideas on how to fix this?