Script Dying unexpectedly

A

Anonymous

Guest
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:
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.
 
Joel said:
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.
 
I've found the function of the class that was having problems with.
Code:
	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:
	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;
		}
                 }
 
Joel said:
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.
 
Back
Top