views:

228

answers:

2

I'm using php to make it so that incoming traffic to my website will append information depending on the search engine. I'm setting $_SESSION['kw'], if it's not already set, to tel me that it's seo and to append the search terms:

  if (isset($result['q']))
   $_SESSION['kw'] = 'seo-'.str_replace('\\"',"X",$result['q']);

  elseif (isset($result['p']))
   $_SESSION['kw'] = 'seo-'.$result['p'];

else {some other stuff}

q is what google, msn and ask use for their queries, p is for yahoo. That covers most of the incoming traffic. OK so now I want to change it a bit so that if the referring page is yahoo, it appends a Y, if it's google a G, msn M etc.

I tried:

if (isset($result['q'])& strstr($_SERVER['HTTP_REFERER'],'google'))
   $_SESSION['kw'] = 'seo-G-'.str_replace('\\"',"X",$result['q']);

elseif (isset($result['q'])& !strstr($_SERVER['HTTP_REFERER'],'google'))
   $_SESSION['kw'] = 'seo-M-'.str_replace('\\"',"X",$result['q']);

  elseif (isset($result['p']))
   $_SESSION['kw'] = 'seo-Y-'.$result['p'];

    else {some other stuff}

But I can't make the first IF true. If I come from ask.com, I get "seo-M appended", but I can't even when coming from google, get it to append. Am I just formatting it wrong?

+4  A: 

Change the & to &&. This is the usual operator for most programming languages for a logical "and"

In PHP you have && or AND for logical bool operations.

& is usually used for bit-operations, which is wrong here.

Sebastian Hoitz
Thank you very much. I get confused by that stuff sometimes.
pg
+1  A: 

I think you're using the wrong AND operator, you should use && not just &. the single ampersand is for bitwise operations.

Try:

if (isset($result['q']) && strstr($_SERVER['HTTP_REFERER'],'google'))
Paolo