Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   Simple (hopefully) PHP problem (https://www.warriorforum.com/programming/449103-simple-hopefully-php-problem.html)

Harvey Segal 11th September 2011 04:27 AM

Simple (hopefully) PHP problem
 
I wonder if some kind PHP programmer
can point out the error here.

I want to pass data (like an affiliate code) to a script
and display it but not let it overwrite any previous code

So
http://www.supertips.com/ztest.php?x=1
shows ==> 1

Followed by
http://www.supertips.com/ztest.php?x=2
shows ==> 2 but it should be 1


Here is the script

<?php
$cookie_name = "ztest";

if(!isset($_COOKIE['$cookie_name'])) {
$x = $_GET[x];
$cookie_value=$x;
$domain = ".supertips.com";
$expire = time()+60*60*24*30;
setcookie("$cookie_name","$cookie_value","$expire" ,"/","$domain",0);
}
echo "value= $cookie_value ";
?>


Thanks
Harvey

imarketstuff 11th September 2011 07:01 AM

Re: Simple (hopefully) PHP problem
 
hey there Harvey,

you have two options:

1. remove the single quotes surrounding ($cookie_name) in the isset conditional
2. use double quotes surrounding ($cookie_name) in the isset conditional

basically, php parses strings within double quotes and doesn't parse strings when using single quotes. :)

peace

Harvey Segal 11th September 2011 08:04 AM

Re: Simple (hopefully) PHP problem
 
Hi marketstuff

Thanks - that worked


I realise I also need this

else {
$cookie_value= "$_COOKIE[$cookie_name]";
}

Harvey

SteveJohnson 12th September 2011 08:48 AM

Re: Simple (hopefully) PHP problem
 
Not related to your issue, but this line:
$x = $_GET[x];

should have single quotes around the x:
$x = $_GET['x'];

PHP tries to evaluate the unquoted x as a constant, then assumes it to be an index. It will work the way you have it, at least this time. But it's good practice to do it correctly.

Harvey Segal 12th September 2011 09:33 AM

Re: Simple (hopefully) PHP problem
 
Quote:

Originally Posted by SteveJohnson (Post 4661435)
It will work the way you have it, at least this time. But it's good practice to do it correctly.

Thanks Steve

Much appreciated

Harvey

Oliot 14th September 2011 11:56 PM

Re: Simple (hopefully) PHP problem
 
It's no so important, but I would write:
setcookie($cookie_name,$cookie_value,$expire ,'/',$domain,0);
It's no need to use double quotes here.

Harvey Segal 15th September 2011 04:57 AM

Re: Simple (hopefully) PHP problem
 
Quote:

Originally Posted by Oliot (Post 4680944)
It's no so important, but I would write:
setcookie(,, ,'/',,0);
It's no need to use double quotes here.

Thanks !!

Harvey


All times are GMT -6. The time now is 05:28 AM.