Gossamer Forum
Home : General : Perl Programming :

PHP else problem

Quote Reply
PHP else problem
I've almost got a handy little PHP snippet. The idea is that when a user access this .htpasswd protected page, the user name is checked against a dbman password file. If the REMOTE_USER name is in the file, Instructional Greeting ONE is shown. And if the REMOTE_USER name isn't in the dbman password file, Instructional Greeting TWO is displayed.

So far, I've got the first part working fine. But I can't seem to get the "else" part right.

Here's what's working so far:

$file="/path and file name";
$fd = fopen ($file, "r");
$contents = file($file);
$number = count($contents);
for($i=0;$i<$number;$i++) { if(strstr($contents[$i],$REMOTE_USER)) print ("Instructional Greeting ONE<P>"); }

If anyone can show me how to add an else factor to this..... Thanks.
Quote Reply
Re: [averick] PHP else problem In reply to
Try this:

Code:
$file="/path and file name";
$fd = fopen ($file, "r");
$contents = file($file);
$number = count($contents);
for($i=0;$i<$number;$i++) {
if(strstr($contents[$i],$REMOTE_USER))
{print ("Instructional Greeting ONE<P>");}
else
{print ("Instructional Greeting TWO<P>");}
}


Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] PHP else problem In reply to
Thanks yogi,
I've done this one already -- and again just now to be sure. For some reason it prints Greeting ONE when it should -- but eight instances of Greeting TWO (four above, four below). And when REMOTE_USER name isn't found in the external file, it prints nine instances of Greeting TWO.

Strange stuff.
Quote Reply
Re: [averick] PHP else problem In reply to
Try changing:

Code:
if(strstr($contents[$i],$REMOTE_USER)) {
print ("Instructional Greeting ONE<P>");
} else {
print ("Instructional Greeting TWO<P>");
}


to....

Code:
if(strstr($contents[$i],$REMOTE_USER)) {
$f = 1;
} else {
$f = 0;
}

....Then underneath all that code add....

if ($f) {
print ("Instructional Greeting ONE<P>");
}
else {
print ("Instructional Greeting TWO<P>");
}

Last edited by:

RedRum: Oct 12, 2001, 7:23 AM
Quote Reply
Re: [RedRum] PHP else problem In reply to
Just being a bit nit picky here, but why bother setting $f=0? It should still work without it - and gets rid of the need for the else.

Code:
if (strstr($contents[$i],$REMOTE_USER)){
print "Instructional Greeting ONE<P>";
$f = 1;
}

if(!$f){
print "Instructional Greeting TWO<P>";
}

Actually, after re-reading this they are almost indentical. Anyhow - thats how I'd do it. They are basically the same. No more coding after mid night for me Smile

Cheers,

Michael Bray
Quote Reply
Re: [Michael_Bray] PHP else problem In reply to
Thanks guys. That's some progress.

RedRum,
With your suggestion, I still get eight instances of Greeting One where they shouldn't be any (four above and below Greeting Two). And it prints nine instances of Greeting One when it should only print one.

Michael,
With your suggestion, I get four instances of Greeting One where there shouldn't be any (four above Greeting Two). And it prints nine instances of Greeting One when it should only print one.

But hey, it's progress right?
Quote Reply
Re: [averick] PHP else problem In reply to
You can't get multiple instances with my code if you put it where it should be...outside the loop.
Quote Reply
Re: [Michael_Bray] PHP else problem In reply to
Michael....there is a reason for doing it that way as your way would still print multiple instances.
Quote Reply
Re: [RedRum] PHP else problem In reply to
I couldn't work out the loop problem. But that's ok...
Plan "B" from another board....
Posted here for others to find/use....

$file="/path and file name";
$f = 0;
$contents = file($file);
foreach($contents as $key => $val) {
if(strstr($val, $REMOTE_USER)) {
$f = 1;
}
}
if($f) {
print "Instructional Greeting ONE<P>";
}
else {
print "Instructional Greeting TWO<P>";
}

Works fine. Credit goes to freddy. Big thanks from me to all. This is going to be a very handy snippet.

Thanks,
averick
Quote Reply
Re: [averick] PHP else problem In reply to
Thats exactly the same as my code. Thats what I was telling you to do.......put the if ($f) { outside the loop.

Last edited by:

RedRum: Oct 12, 2001, 10:04 AM
Quote Reply
Re: [RedRum] PHP else problem In reply to
Ummm... Yea. Ok. Thanks. Your instructions were clear. I just couldn't figure out how to follow them -- or at least not before the above snippet was delivered.

It came in while I was going around in the wrong loop -- hehehe. I need to work on my understanding of php. It's obviously lacking.

But here we are at the end of the day... And we've got a pretty funky little snippet of PHP for the whole world to benefit from. It wouldn't have happened without you folks pointing me in the right direction -- and someone else doing it for me.

Thanks all,
I'm just a tourist,
averick
Quote Reply
Re: [averick] PHP else problem In reply to
Smile I assumed he would put it outside the loop
Cheers,
Michael Bray