1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development PHP - Fatal error: Cannot break/continue

Discussion in 'Software' started by Silver51, 28 Nov 2011.

  1. Silver51

    Silver51 I cast flare!

    Joined:
    24 Jul 2006
    Posts:
    2,962
    Likes Received:
    287
    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; }
     
  2. Zoon

    Zoon Hunting Wabbits since the 80s

    Joined:
    12 Mar 2001
    Posts:
    5,888
    Likes Received:
    824
    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.
     
    Silver51 likes this.
  3. faugusztin

    faugusztin I *am* the guy with two left hands

    Joined:
    11 Aug 2008
    Posts:
    6,953
    Likes Received:
    270
    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.
     
  4. Silver51

    Silver51 I cast flare!

    Joined:
    24 Jul 2006
    Posts:
    2,962
    Likes Received:
    287
    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?
     
  5. faugusztin

    faugusztin I *am* the guy with two left hands

    Joined:
    11 Aug 2008
    Posts:
    6,953
    Likes Received:
    270
    I guess it is because you read $bot, not $_POST["bot"]. $bot being uninitialized variable has length of 0.
     
    Silver51 likes this.
  6. Silver51

    Silver51 I cast flare!

    Joined:
    24 Jul 2006
    Posts:
    2,962
    Likes Received:
    287
    Thanks, that's sorted it. :thumb:
     
  7. Zoon

    Zoon Hunting Wabbits since the 80s

    Joined:
    12 Mar 2001
    Posts:
    5,888
    Likes Received:
    824
    Or put $bot = $_POST['bot'] somewhere above it of course.
     

Share This Page