Coalission Coalission - 1 year ago 87
PHP Question

if $_GET && condition not working, only checking if 2nd condition is true

I am trying to check that BOTH $_GET conditions are true before executing a redirect, but my code is only checking if the 2nd one is true.

This is what I have:

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

if (strpos($url,'this-is-an-example-post') !== false) {
if($_GET['utm_campaign']==testing123 && $_GET['utm_source']==testing456) {
function preserve_qs() {
if (empty($_SERVER['QUERY_STRING']) && strpos($_SERVER['REQUEST_URI'], "?") === false) {
return "";
return "?" . $_SERVER['QUERY_STRING'];
header("Location:" . preserve_qs());

What it's doing is that if I set utm_campaign to = testing123 by itself, it won't redirect. That's good, I want it to require both. If I set both utm_campaign to = testing123 and utm_source to = testing456 then it does redirect, good so far. Now if I set ONLY utm_source to = testing456 it ALSO redirects, which means it's only checking for the 2nd condition to be true, but I need both to be true or for the script to exit, and I can't seem to figure out why it's not working the way it should.

This is in a Wordpress header.php file, not sure if it makes a difference.

Answer Source

Use quotes arround string

  if($_GET['utm_campaign']== 'testing123' && $_GET['utm_source']=='testing456') {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download