So I'm having trouble with a small PHP script for this page: http://www.st-ives.cornwall.sch.uk/newsletters/schnews.php On hitting submit it runs a php file (process.php) which take the information from the form and places it in a txt file. The part that's causing errors is the anti-bot field. It's nothing too complex, just supposed to check the bot field for X characters and either stop if there are more or less than the specified number, or continue if the number of characters are correct. I put this in a while ago to cut back on random bots filling in the form with crap. It was working until last week when our host moved everything to a new server. Now hitting submit freaks it out and gives the following error: Fatal error: Cannot break/continue 1 level in /home/r35437/public_html/newsletters/Process.php on line 9 Code: <?php ob_start(); ?> <?php if(isset($_POST['Submit'])){ $name = $_POST['name']; $email = $_POST['email']; $err = ''; if( strlen($bot) > 6){ echo"<div style=\"text-align: center;\">The anti-bot field is too long!</div>"; $CN_HALT = TRUE; break 1; } if( strlen($bot) < 6){ echo"<div style=\"text-align: center;\">The anti-bot field is too short!</div>"; $CN_HALT = TRUE; break 1; } if(trim($name)==''){ $err .= '-Please enter a name<br>'; } if(empty($email)){ $err .= '-Please enter an email address'; } if($err!=''){ echo $err; } else{ $filename = 'file.txt'; $somecontent = $name . ';' . $email . "\n"; if (is_writable($filename)) { if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } header("Location: success.htm"); fclose($handle); function _error($level) { header("Location: schnews.php?error=$level"); } } } } ?> So playing around with it and removing the following lines allow people to submit form data again, but obviously it then ignores the anti-bot field. Code: if( strlen($bot) > 6){ echo"<div style=\"text-align: center;\">The anti-bot field is too long!</div>"; $CN_HALT = TRUE; break 1; } if( strlen($bot) < 6){ echo"<div style=\"text-align: center;\">The anti-bot field is too short!</div>"; $CN_HALT = TRUE; break 1; }
Instead of break 1, try Code: die("You're a bot") I assume you have something in place to simulate a bot's entry - give it a try and see if it works.
Break and continue are for loops only (do/while,while,for,foreach). And because you have no loop, you use a wrong command in a wrong place. As Zoon said, if you want to stop the script, use the die command. The break command probably worked in some prehistoric version of PHP for the example you provided, but since then it was corrected and now shows an error.
Yeah, the script was a relic from my scripts folder. It was a simple fix at the time, but it's worked well enough in the past. Okay, so using die instead of break removes the error, but no matter how many characters are typed in the anti-bot field it trips the second line saying that the field is too short. Is this the correct way of using 'strlen' or am I just being denser than usual?
I guess it is because you read $bot, not $_POST["bot"]. $bot being uninitialized variable has length of 0.