wmfrancia
wmfrancia

Reputation: 1246

PHP Function Executes when not called

Something very bizarre is occurring in my simple script. I have a static function that is passed $_GET data from the server and runs a prepared statement on it.

the odd part is that this function is called inside and if statement and when the if is not met the SQL prepared statement will still run but nothing else in the function will.

if(Filter::isBlacklisted($domain) || $domain == null || Filter::forbiddenCountry($country)) {

    $pid = ($_GET['pid'] == null) ? 0 : $_GET['pid'];
    $tid = ($_GET['tid'] == null) ? 0 : $_GET['tid'];

    //THIS WILL NOT ECHO OUT CAUSE THE IF IS NOT MET
    echo "here";

    //IF I UNCOMMENT THIS THE QUERY WILL NOT RUN
    //exit; 

    //THIS LINE IS THE CULPRIT, ITS QUERY RUNS ON EVERY PAGE REQUESTS
    Filter::blockedRequest($tid,$pid); 

}
else {

    echo "there";

    exit;

}

here is the corresponding static function

 public static function blockedRequest($tid,$pid) {

    //The QUERY executes but this will not echo out
    echo "Hello";


    $date = date("Y-m-d");
    $mysqli = DB::dbConnect();

    if($stmt = $mysqli->prepare("INSERT INTO adserver.requests VALUES(?,1,1,?,?) ON DUPLICATE KEY UPDATE requests=requests+1, blocked=blocked+1")) {

      $stmt->bind_param("isi",$tid,$date,$pid);
      $stmt->execute();
      $stmt->close();
      $mysqli->close();

    }

    return;

  }

Each request being sent does not meet the first if statement and goes to the else statement which echoes out "there". So the If statement works as it should, but the sql statement still executes but the echo in the static function does not run only the sql prepared statement.

The server does not run memcache,opcache or any other caching solution so it's not that.

Also the query is not called anywhere else in the code just here. the containing class does not make any calls to the static function through any other functions.

I am baffled, any advice is greatly appreciated.

Upvotes: 2

Views: 102

Answers (1)

Dan
Dan

Reputation: 11084

It is possible that you are sending multiple requests without realizing it. If the first request entered the if statement and the second one did not, it would overwrite the first echo and produce the result you are seeing.

To check what requests are being sent to the server, watch your access logs while you reproduce the behavior (refresh the page or submit the form).

Typical LAMP: tail -f /var/log/apache/access.log

nginx: tail -f /var/log/nginx/access.log

Upvotes: 1

Related Questions