简体   繁体   中英

Infinite redirect loop from PHP code in Wordpress

I have a PHP script which I will post below. It is a voting website, and my client only wants one user to be able to vote once based on their cookies and IP address.

After voting once, if the cookie or IP is detected as the same they are redirected to a fake voting pg which allows multiple votes. The browser loops between both the legal and duplicate vote pages.

Here is the code, I only added in the exit and die functions after getting this error and seeing online that might be the cause - however adding those functions made no difference.

$q = mysql_query("SELECT * FROM votelog");

while($row = mysql_fetch_array($q))
{
     if(($ip = $_SERVER['REMOTE_ADDR']) == $row['ip'])
     {
            $duplicateIP = true;
     }//end if

     if(($row['pollid'] == 8))
     {
         $duplicatePoll = true;
      }//end if
   }//end while

   //check cookies
   if(isset($_COOKIE['poll']))
   {
       $cookieCheck = true;
    }//end if

     if((($duplicateIP == true) && ($duplicatePoll == true)) or ($cookieCheck == true)) 
     {
         show this pg
      }//end if
      else
      {
            echo '<meta http-equiv="refresh" content="0; url=/polls/legit" />'; //redirect to legal pg
      exit();
      die();

       }//end else

Any ideas? The other page is the same except that the if and else are switched, like this:

     if((($duplicateIP == true) && ($duplicatePoll == true)) or ($cookieCheck == true)) 
     {
          echo '<meta http-equiv="refresh" content="0; url=/polls/dupe" />'; //redirect to duplicate
      exit();
      die();
      }//end if
      else
      {
          show this pg
       }//end else

PS - I'm operating in a Wordpress environment

It is hard to guess whats going on there. But if your code is fine, i would expect that you have an caching issue.

Setting the headers (header()) correct, would help to solve that issue. But be carefull, you can make it more worse with setting wrong headers.

So an easy workaround could be to add an ?time() to your url. So the redirected URL would change each second.

echo '<meta http-equiv="refresh" content="0; url=/polls/dupe?'.time().'" />'; //redirect to duplicate

Just a side note:

exit();
die(); // this will never reached, as exit() and die() is the same

About exit() and die()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM