Forum Moderators: coopster
I have a script which is like this:
<?
$result=1;
if (preg_match("'DigExt'",$HTTP_USER_AGENT))
{
$result=0;
}
if (preg_match("'smi'",$SCRIPT_URI))
{
$result=0;
}
if (preg_match("'cltreq.asp'",$SCRIPT_URI))
{
$result=0;
}
//
//and so on............ with a lot of "if"
//
if ($result==1){
//
//active script:send a PHP mail
//
}
?>
// if "no", write only HTML page
<html>
<head>
<title>erreur404</title>
</head>
<body>
</body>
</html>
Is there a shortest and easy way to write the "if" sequence?
Thank's for your cooperation.
I don't know a lot about PHP but the way I would want the script works is:
case "1" : mail() to webmaster to tell him content (URL, USER_AGENT...) of error404 AND write a HTML 404 page to surfer for information and valid links.
case "0" : no mail() to webmaster BUT the same HTML page to the surfer.
In fact there is no "default" case.
How do you write the script in this case?
default: {}
default: {
// do stuff
break;
}
That means nothing is done per default. But you should be sure to catch every possible case with "case x:".
And if so, default{} will never be used.
So there's no reason for default{} at all and the final answer is just to write your cases and no default{}.
Each preg_match will still have to be done in the if statements, which is what Maleville is trying to correct. The switch only replaces the simple if ... else at the end.
A thought that comes to mind is using arrays and loops to cut down on code. You could put all of your patterns in an array and then loop through them.
$patternarr[0] = array('HTTP_USER_AGENT','DigExt');
$patternarr[1] = array('SCRIPT_URI','smi','cltreq.asp');
$counter1 = 0;
while (is_array($patternarr[$counter1])) {
$counter2 = 1;
while (isset($patternarr[$counter1][$counter2])) {
if (preg_match($patternarr[$counter1][$counter2],${$patternarr[$counter1][0]})) {
$result=0;
}
$counter2++;
}
$counter1++;
}
Seeing you responded while I was cooking this it seems you only wanted to get rid of the little if .. else. I haven't tested the above so it may need a little tweak but in theory it should work.
the { } braces are only needed if you have more than one statement...
so eg:
if (preg_match("'DigExt'",$HTTP_USER_AGENT))
{
$result=0;
}
can be written just as well (better) as:
if (preg_match("'DigExt'",$HTTP_USER_AGENT)) $result=0;
and SECONDLY...
if you are just looking for whether a substring exists within a longer string - don't use preg_match... it's much much faster to use strpos(), which works much the same way, [but doesn't use regular expressions (which you don't need)]
$result = preg_match($blah1, $blah2);
$result &= preg_match($blah3, $blah4);
$result &= preg_match($blah5, $blah6);
$result &= preg_match($blah7, $blah8);
$result &= preg_match($blah9, $blah10);
$result &= preg_match($blah11, $blah12);
$result &= preg_match($blah13, $blah14);
Or just stick them all in one long, rather horrible line:
$result = preg_match($blah1, $blah2) && preg_match($blah3, $blah4) && preg_match($blah5, $blah6) && preg_match($blah7, $blah8) && .....etc.....
toodle-pip,
Michael