tags:

views:

41

answers:

3

Hey there,

I'm implementing some affiliate tracking to my site. The affiliate network has asked that I hide the telephone number on the site.

When an affiliate clicks on a link to my site, any of the site URLs will be appended wth something like /?source=affiliate&siteid=XXXX for example; mydomain.com/?source=affiliate&siteid=XXXX

I've been trying to do this to hide the telephone number;

<?php
 if (!array_key_exists('affiliate', $_GET)){
  //Show telephone number
  echo "<li>+44 (0)1234 567891</li>";
 }
?>

However, this doesn't seem to be working. Ideally I need to show the number by default, but if the URL contains the affilaite part of the url, then the telephone number should be hidden.

Could someone please advise where I might be going wrong? :)

Thank you.

+4  A: 

in your $_GET array, source is the key, affiliate is the value. you want:

<?php
 if (!in_array('affiliate', $_GET)){
  //Show telephone number
  echo "<li>+44 (0)1234 567891</li>";
 }
?>

or

<?php
 if (!array_key_exists('source', $_GET)){
  //Show telephone number
  echo "<li>+44 (0)1234 567891</li>";
 }
?>
sleepynate
The first piece of code checks that affiliate is the value of SOMETHING in the URL, not specifically the source key. The second piece of code just checks that there *is* a source key, not that it has the value affiliate.
Michael Madsen
Correct. The asker requested "if the URL contains the affilaite part of the url". The first uses a different function to find what he was asking for, while the second is the closest to his code, changing only the particular key he's looking for. More importantly, the two together illustrate the difference between the two. :)Cheers!
sleepynate
+3  A: 

It sounds like you've mixed up keys and values. In a URL, the key is the part to the left of the =, while the value is on the right side.

The condition you want should be if (!array_key_exists('source', $_GET) || $_GET['source'] != 'affiliate'). This checks that the source key is defined, and that it has the value affiliate.

Michael Madsen
Awesome, thanks for the quick replies. :D Much appreciated.
Neil Bradley
A: 

The conclusion:

if (array_key_exists('source', $_GET) && $_GET['source'] == 'affiliate') {
    //Show telephone number
    echo "<li>+44 (0)1234 567891</li>";
}
iroel