Search code examples
phploopsrequest-uri

URL in loop php, request uri


I have a php code:

if($_SERVER['REQUEST_URI'] == '/?task=entry.add&sid=67'){echo'<script type="text/javascript">window.location.assign("/index.php");</script>';}else{echo"";}
if($_SERVER['REQUEST_URI'] == '/?task=entry.add&sid=68'){echo'<script type="text/javascript">window.location.assign("/index.php");</script>';}else{echo"";}
if($_SERVER['REQUEST_URI'] == '/?task=entry.add&sid=69'){echo'<script type="text/javascript">window.location.assign("/index.php");</script>';}else{echo"";}

and I'm trying to do this in loop (ids should be for example from 67 to 100)

<?php
  $page = $_POST['id'];
  for ($i=1; $i<=$id; $i++){
    $url = "/?task=entry.add&sid=".$i."<br/>";
    echo'<script type="text/javascript">window.location.assign("/index.php");</script>';}else{echo"";
  }
?>

what should i do more, and where is my problem?


Solution

  • Well, you forgot to add your if statement.

    <?php
        $id = $_POST['id'];
        for ($i=1; $i<=$id; $i++)
        {
            $url = "/?task=entry.add&sid=".$i;
    
            if ($_SERVER['REQUEST_URI'] == $url)
            {
                echo '<script type="text/javascript">window.location.assign("/index.php");</script>';
            }
            else
            {
                echo"";
            }
        }
    ?>
    

    Here is a correction.

    • I added the if
    • fixed few ; missing