Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 11th Sep 2011, 04:27 AM   #1
Mr SuperTips
War Room Member
 
Join Date: 2002
Location: United Kingdom.
Posts: 2,176
Thanks: 104
Thanked 1,090 Times in 493 Posts
Default
Simple (hopefully) PHP problem
Share on: 
fb share twitter share gplus share more share

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
Harvey Segal is offline  
Unread 11th Sep 2011, 07:01 AM   #2
HyperActive Warrior
War Room Member
 
Join Date: 2009
Location: Costa Rica
Posts: 164
Thanks: 63
Thanked 44 Times in 41 Posts
Default
Re: Simple (hopefully) PHP problem
Share on: 
fb share twitter share gplus share more share

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

I MARKET STUFF

imarketstuff is offline  
Unread 11th Sep 2011, 08:04 AM   #3
Mr SuperTips
War Room Member
 
Join Date: 2002
Location: United Kingdom.
Posts: 2,176
Thanks: 104
Thanked 1,090 Times in 493 Posts
Default
Re: Simple (hopefully) PHP problem
Share on: 
fb share twitter share gplus share more share

Hi marketstuff

Thanks - that worked


I realise I also need this

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

Harvey
Harvey Segal is offline  
Unread 12th Sep 2011, 08:48 AM   #4
VIP Warrior
War Room Member
 
SteveJohnson's Avatar
 
Join Date: 2007
Location: Caldwell, Idaho, USA.
Posts: 1,967
Thanks: 1,024
Thanked 2,043 Times in 1,183 Posts
Default
Re: Simple (hopefully) PHP problem
Share on: 
fb share twitter share gplus share more share

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.

The 2nd Amendment, 1789 - The Original Homeland Security.

Gun control means never having to say, "I missed you."
SteveJohnson is offline  
Unread 12th Sep 2011, 09:33 AM   #5
Mr SuperTips
War Room Member
 
Join Date: 2002
Location: United Kingdom.
Posts: 2,176
Thanks: 104
Thanked 1,090 Times in 493 Posts
Default
Re: Simple (hopefully) PHP problem
Share on: 
fb share twitter share gplus share more share

Originally Posted by SteveJohnson View Post

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
Harvey Segal is offline  
Unread 14th Sep 2011, 11:56 PM   #6
Warrior Member
 
Join Date: 2011
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
Default
Re: Simple (hopefully) PHP problem
Share on: 
fb share twitter share gplus share more share

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.
Oliot is offline  
Unread 15th Sep 2011, 04:57 AM   #7
Mr SuperTips
War Room Member
 
Join Date: 2002
Location: United Kingdom.
Posts: 2,176
Thanks: 104
Thanked 1,090 Times in 493 Posts
Default
Re: Simple (hopefully) PHP problem
Share on: 
fb share twitter share gplus share more share

Originally Posted by Oliot View Post

It's no so important, but I would write:
setcookie(,, ,'/',,0);
It's no need to use double quotes here.
Thanks !!

Harvey

Harvey Segal is offline  
Closed Thread


Bookmarks

Tags
php, problem, simple


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 04:54 AM.