-1

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: http://example123.com/this-is-an-example-post" . preserve_qs());
exit;
}
}

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.

  • You declare `preserve_qs` in a branch of if statement, so if it does not execute, the function is not defined. In addition to that, you should change `testing123` and `testing456` to `'testing456'` and `'testing123'` unless you have those constants defined. – Alexander Mikhalchenko Jun 12 '16 at 17:16
  • the preserve_qs is in the same if statement as the header is, so it won't change much ... – user3528269 Jun 12 '16 at 17:18
  • Error reporting would have thrown you something about undefined constants. http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Jun 12 '16 at 18:12

2 Answers2

0

Use quotes arround string

  if($_GET['utm_campaign']== 'testing123' && $_GET['utm_source']=='testing456') {
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
0

I think you should check if the parameters are set and format the code a bit. Also there is no need to declare the function preserve_qs().

if (strpos($url,'this-is-an-example-post') !== false) {

    if(isset($_GET['utm_campaign']) && isset($_GET['utm_source']) && $_GET['utm_campaign']=='testing123' && $_GET['utm_source']=='testing456') {
        $qs = "?" . $_SERVER['QUERY_STRING'];
        if (empty($_SERVER['QUERY_STRING']) && strpos($_SERVER['REQUEST_URI'], "?") === false) 
            $qs = "";

        header("Location: http://example123.com/this-is-an-example-post" . $qs);
        exit;

    }

}
Niket Malik
  • 1,075
  • 1
  • 14
  • 23