Script Dying unexpectedly

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Joel
New php-forum User
New php-forum User
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand
Contact:

Script Dying unexpectedly

Postby Joel » Fri Jul 25, 2003 2:50 pm

I just uploaded a site, which I just finished and I went through testing to make sure it all worked on the web host. And whenever I added a listing or edited a listing the script would stop. I would get no error, just the script will stop. The whole site works perfectly (to my knowing) on my own test server.

By trial and error, I managed to narrow the problem down to my use of validation class. This is the same class I also use on the signup form, which works fine there. This is the code, I have put comment markings in where I commented the code out for it to work.

Code: Select all

dbconnect();
$sql = mysql_query("SELECT userid FROM listings WHERE userid=".$_SESSION['userid']);
if (mysql_num_rows($sql) < 5) {
    $fv = new FormValidator;
/*
    $fv->isLength("company", "Company name must not contain more than 50 characters", 1, 50);
    $fv->isEmpty("type", "Type not specified");
    $fv->isLength("web", "Web address must contain less than 100 characters", 0, 100);
   $fv->isLength("statement", "Company description must not be more than 1500 characters long", 1, 1500);
   $fv->MaxWordLength("statement", "Statement contains a word more than 40 characters long", 40);
   $fv->isLength("description", "Service Description must not be more than 1500 characters long", 1, 1500);
   $fv->MaxWordLength("description", "Description contains a word more than 40 characters long", 40);
*/
         
if ($errors = $fv->getErrorList()) {


I'm am really quite stunned about this, one thing for sure is that I know it is not a problem in the class as I have been using that class for a while, and use it successfully on another page.

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Re: Script Dying unexpectedly

Postby swirlee » Fri Jul 25, 2003 11:17 pm

Joel wrote:I just uploaded a site, which I just finished and I went through testing to make sure it all worked on the web host. And whenever I added a listing or edited a listing the script would stop. I would get no error, just the script will stop.


At exactly which line in the script does it "stop"? You can figure this out by trial-and-error just by echoing something at various places in the script and see which is the last line that the echo can be on and still show up.

Joel
New php-forum User
New php-forum User
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand
Contact:

Postby Joel » Sat Jul 26, 2003 12:44 am

I've found the function of the class that was having problems with.

Code: Select all

   function MaxWordLength($field, $msg, $maxlength=50)
   {
      $value = $this->_getValue($field);
      
      $words = str_word_count($value, 1);
      $numwords = count($words);
      
      $i = 0;
            
      do {
         @$word = $words[$i];
         if (strlen($word) > $maxlength) {
            $this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
            break;
         }
         $i++;
      } while ($i<$numwords);
   }


It was this function, and I realise now str_word_count is only available after PHP 4.3.0 and this host is running 4.2.2

I suppose I can do something the same thing using explode, in fact it may even be a better way of doing it. I'll race you to the function finish. If you're wondering why I need this function: it's because some 'internet vandals' like to hold down letters like this...

fffffffffffffffff - but a whole lot longer (it got annoying for me viewing the post)

which ruins the whole layout of a page, like it does on PHPBB2. So this function will stop people from doing that.

Edit
This is the new function I wrote for it, which is a whole lot better now that I think about it.

Code: Select all

   function MaxWordLength($field, $msg, $maxlength=50)
   {

      $value = $this->_getValue($field);
      $words = explode(' ', $value);
      $i = 0;
      foreach ($words as $w) {
         if (strlen($words[$i]) > $maxlength) {
            $this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
            break;
         }
         else {
            return true;
         }
         ++$i;
      }
                 }

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Sat Jul 26, 2003 12:44 pm

Joel wrote:If you're wondering why I need this function: it's because some 'internet vandals' like to hold down letters like this...

fffffffffffffffff - but a whole lot longer (it got annoying for me viewing the post)

which ruins the whole layout of a page, like it does on PHPBB2. So this function will stop people from doing that.


This is indeed a nasty thing for people to do, and it's good of you to anticipate it. Another thing you can do is in your HTML use the CSS overflow attribute to force the page to hide anything that extends beyond the intended edge of the element.

Also, in writing your script, don't forget that \t and \n also count as word boundaries, in addition to space.


Return to “PHP coding => General”

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest