Jump to content

Breaking Out of a foreach loop with header("Location: $self");


Recommended Posts

I have an administrative back end on a secure server that I want to use Basic Authentication for.

 

In my "Add" module, I can't figure out why this works:


$rows = file($passfile);
import_request_variables("gp","x_");

switch($x_action){
  case "Add":

...

   foreach($rows as $row){
    $data = explode(":",$row);
    if($x_userid == $data[0]){
     $dupe = 1;
     $errormsg = "Duplicate Record!";
    }
   }

...

   if($dupe == 0){
    write_rows($passfile,$rows);
    header("Location: $self");
   }else{
    header("Location: $self?errormsg=$errormsg");
   }
  break;

...

 

and this doesn't:

 


$rows = file($passfile);
import_request_variables("gp","x_");

switch($x_action){
  case "Add":

...

   foreach($rows as $row){
    $data = explode(":",$row);
    if($x_userid == $data[0]){
     header("Location: $self?errormsg=Duplicate Record!");
    }
   }

...
  break;

 

Thanks in advance for your constructive comments.

Code produces no error messages in the log or on screen.

 

The if condition passes as if it returns false.  Note that the same if condition passes as true on the first bit of code.

 

IOW: header() statement works as expected in the first code and doesn't work at all in the second snippet.

You're not returning a true/false statement in your second code.  Also, you're using a header to redirect, which only works if the browser hasn't received data yet (if the header has been established).  Turn on errors with the code below, more then likely the redirect isn't working because the header is already established.

 

error_reporting(E_ALL);
ini_set('display_errors','on');

You're not returning a true/false statement in your second code.

 

Am I missing something.  The if statement is exactly the same.  Shouldn't the condition being true kick in the header statement which would reload the page with the error message passed in the get?

 

 

Also, you're using a header to redirect, which only works if the browser hasn't received data yet (if the header has been established).

 

 

Yeah, I checked that previously to verify that headers weren't being sent, and they're not, PLUS that doesn't explain why the header statement works below the foreach loop, right?

 

I till try again with the settings turned on to see if there's something generated.

so what you're trying to tell us is

 

 

This does work

$err = "Something with spaces";
header("Location: somepage.php?err=$err");

 

and this doesn't

header("Location: somepage.php?err=Something with spaces");

 

Am I correct?

Am I correct?

 

No.

 

The issue is that this (psuedo) code doesn't work:

 

$rows[ ] = "eawf:otherstuff\n";
$testdata = "eawf";
foreach($rows as $row){
$data = explode(":",$row);
if($data[0] == $testdata){
  header("Location: $self");
}
}

 

while this one does work:

 

$rows[ ] = "eawf:otherstuff\n";
$testdata = "eawf";
foreach($rows as $row){
$data = explode(":",$row);
if($data[0] == $testdata){
  $flag = true
}
}
if(isset($flag)){
  header("Location: $self");
}

 

actually does work.  So the question is, WHY doesn't the first code snippet work?

 

The psuedo code for the above is:

 

Form submit provides input vars for userid and password.
read all records from .htpasswd and load to an array ($rows)
process array one element at a time
parse array vars via ":" character.
if userid from form matches the userid from .htpassword:
  reload the page with a get var loaded with an error message.
else process next record.

 

I can[t find anything online indicating that the foreach loop is not able to be broken by issuing the header() function, which, at least to my way of thinking, should stop program code execution by simply reloading the page again.  What ends up happening is that the program continues to run and the header statement is not executed.

 

Hope this makes sense?

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.