eawf Posted January 5, 2010 Share Posted January 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187309-breaking-out-of-a-foreach-loop-with-headerlocation-self/ Share on other sites More sharing options...
p2grace Posted January 5, 2010 Share Posted January 5, 2010 When you say it "doesn't work" what does that imply? Does it produce a php syntax error or warning, or behave unexpectedly? Quote Link to comment https://forums.phpfreaks.com/topic/187309-breaking-out-of-a-foreach-loop-with-headerlocation-self/#findComment-989220 Share on other sites More sharing options...
eawf Posted January 5, 2010 Author Share Posted January 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187309-breaking-out-of-a-foreach-loop-with-headerlocation-self/#findComment-989235 Share on other sites More sharing options...
p2grace Posted January 5, 2010 Share Posted January 5, 2010 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'); Quote Link to comment https://forums.phpfreaks.com/topic/187309-breaking-out-of-a-foreach-loop-with-headerlocation-self/#findComment-989237 Share on other sites More sharing options...
eawf Posted January 6, 2010 Author Share Posted January 6, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187309-breaking-out-of-a-foreach-loop-with-headerlocation-self/#findComment-989264 Share on other sites More sharing options...
Zane Posted January 6, 2010 Share Posted January 6, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/187309-breaking-out-of-a-foreach-loop-with-headerlocation-self/#findComment-989329 Share on other sites More sharing options...
eawf Posted January 6, 2010 Author Share Posted January 6, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/187309-breaking-out-of-a-foreach-loop-with-headerlocation-self/#findComment-989375 Share on other sites More sharing options...
eawf Posted January 7, 2010 Author Share Posted January 7, 2010 Is the answer that you can't escape out of a foreach() loop with the header() function? Quote Link to comment https://forums.phpfreaks.com/topic/187309-breaking-out-of-a-foreach-loop-with-headerlocation-self/#findComment-990047 Share on other sites More sharing options...
mrMarcus Posted January 7, 2010 Share Posted January 7, 2010 both snippets of code will work. and yes, you can "break" out of a loop using a header() redirect. are you setting $self? Quote Link to comment https://forums.phpfreaks.com/topic/187309-breaking-out-of-a-foreach-loop-with-headerlocation-self/#findComment-990052 Share on other sites More sharing options...
eawf Posted January 7, 2010 Author Share Posted January 7, 2010 both snippets of code will work. and yes, you can "break" out of a loop using a header() redirect. are you setting $self? BUT, the first snippet isn't working at all and the second one is. Quote Link to comment https://forums.phpfreaks.com/topic/187309-breaking-out-of-a-foreach-loop-with-headerlocation-self/#findComment-990054 Share on other sites More sharing options...
mrMarcus Posted January 7, 2010 Share Posted January 7, 2010 looking at the last two pieces of code you displayed (post#6), they both work fine as long as $self is defined. i don't know what else to say .. i don't see the problem at all. Quote Link to comment https://forums.phpfreaks.com/topic/187309-breaking-out-of-a-foreach-loop-with-headerlocation-self/#findComment-990059 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.