Jump to content

PHP input / text file verification error


DarkShadowWing

Recommended Posts

Hi. I'm having a bit of a problem with my php script. What I need it to do is whatever input for zipcode is entered into zipcode line. i need it to check zipcodes.dat and read + check each & every line in there & tell the webpage if the zipcode entered was 1 of the zipcodes in the zipcodes.dat file.

 

IE:

 

Step 1: Check zipcode that was entered.

 

Step 2: Read from zipcodes dat file to see if zipcode entered matches 1 of the zipcodes in zipcodes.dat file.

 

Step 3: allow or deny zip code entered.

 

 

Here's the data for zipcodes.dat:

 

zipcodes.dat:

 

12346
6789
11111
1111

 

Here's the data for validation.php:

 

validation.php:

 

<?php
/* validation.php */

require_once ('functions.php');

$valid = TRUE;

if (isset ($_POST['submit'])) {
    foreach($_POST as $key=>$value) {
      $$key = $value;
    }
    
    $valid = $fn = checkLength($fname, 1, 30);
    $ln = checkLength($lname, 1, 30);
    $valid = $valid && $ln;
    $em = checkEmail($email);
    $valid = $valid && $em;
    $ps = checkPassword($password);
    $valid = $valid && $ps;
    $ps2 = $password == $password2;
    $valid = $valid && $ps2;
    $hp = checkURL($homepage);
    $valid = $valid && $hp;
    $zp = checkMailCode($zipcode, 'US');
    $valid = $valid && $zp;

    if ($valid) {
        echo "Form filled successfully!";
        exit;
    }
} else {
    $fn = $ln = $em = $ps = $ps2 = $hp = $zp = TRUE;
    $fname = $lname = $email = $zipcode = $homepage = '';
}
?>
<h1>Sample Registration Form</h1>
<?php
if (!$valid) {
?>
<style type="text/css">
td.error {color:C03; font-weight:bold; }
</style>
Please correct the items in red and resubmit.<br /><br />
<?php
}
?>
<form method="POST" action="validation.php">
<table border="0">
<tr>
  <td colspan="2">Please fill out the form for registration.<br /><br />
  Passwords should be a minimum of 8 characters.<br />
  They should be composed of numbers and letters with a number<br />
  somewhere in the middle. You should also avoid using the same<br />
  character repeatedly.<br /><br />
  Your homepage should begin with <b>http://</b><br /><br /></td>
</tr>
<tr>
  <td align="right"<?php if(!$fn) echo 'class="error"'; ?>>
  First Name:
  </td>
  <td>
  <input type="text" name="fname" size="30" value="<?= $fname ?>">
  </td>
</tr>
<tr>
  <td align="right"<?php if(!$ln) echo 'class="error"'; ?>>
  Last Name:
  </td>
  <td>
  <input type="text" name="lname" size="30" value="<?= $lname ?>">
  </td>
</tr>
<tr>
  <td align="right"<?php if(!$em) echo 'class="error"'; ?>>
  Email Address:
  </td>
  <td>
  <input type="text" name="email" size="20" value="<?= $email ?>">
  </td>
</tr>
<tr>
  <td align="right"<?php if(!($ps && $ps2)) echo 'class="error"'; ?>>
  Password:
  </td>
  <td>
  <input type="password" name="password" size="10">
  </td>
</tr>
<tr>
  <td align="right"<?php if(!($ps && $ps2)) echo 'class="error"'; ?>>
  Password Again:
  </td>
  <td>
  <input type="password" NAME="password2" SIZE="10">
  </td>
</tr>
<tr>
  <td align="right"<?php if(!$zp) echo 'class="error"'; ?>>
  Zip Code:
  </td>
  <td>
  <input type="text" name="zipcode" size="10" value="<?= $zipcode ?>">
  </td>
</tr>
<tr>
  <td align="right"<?php if(!$hp) echo 'class="error"'; ?>>
  Home Page:
  </td>
  <td>
  <input type="text" name="homepage" size="40" value="<?= 
    $homepage ?>">
  </td>
</tr>
<tr>
  <td align="right"> </td>
  <td>
  <input type="SUBMIT" name="submit" value="Submit">
  </td>
</tr>
</table>
</form>

 

Here is where the problem is in functions.php:

 

The function I am having a bit of a problem with is validcode($code); .

 

functions.php:

 

<?

function isDigits($element) {
  return !preg_match ("/[^0-9]/", $element);
}

function isLetters($element) {
  return !preg_match ("/[^A-z]/", $element);
}

function checkLength($string, $min, $max) {
  $length = strlen ($string);
  if (($length < $min) || ($length > $max)) {
    return FALSE;
  } else {
    return TRUE;
  }
}

function validcode($code){

    $lines = file($file);
    $l_count = count($lines);

    $find = $_REQUEST['zipcode'];

    $fh = fopen('zipcodes.dat','r') or die($php_errormsg);

    while(! feof($fh)){

   if($s = fgets($fh,1048576)){

  for($x = 0; $x< $l_count; $x++)
    {

            $parts = explode('=', $s);

    if($s <> $parts[$x]){

    return $s or $parts[$x];

    } else if($s == $code) {

    return $s or $parts[$x];

    }

   }

  }

}

}

function checkMailCode($code, $country) {
  
  $code = preg_replace("/[\s|-]/", "", $code);
  $length = strlen ($code);

  switch (strtoupper ($country)) {
    case 'US':
      if (($length <> 5) && ($length <> 9) || ($validate != "12346")) {
        return FALSE;
      }
      return isDigits($code);
    case 'CA':
      if (($length <> 6 || $validate == "") || ( $validate != '12346' )) {
        return FALSE;
      }
      return preg_match ("/([A-z][0-9]){3}/", $code);
  }
}

function checkURL($url) {
  return preg_match ("/http:\/\/(.*)\.(.*)/i", $url);
}

function checkURLandConnect($url) {  
  if (!preg_match ("/http:\/\/(.*)\.(.*)/i", $url)) {
    return FALSE;
  }
  $parts = parse_url($url);
  $fp = fsockopen($parts['host'], 80, $errno, $errstr, 10);
  if(!$fp) {
    return FALSE;
  }
  fclose($fp);
  return TRUE;
}

function checkEmail($email) {
  $pattern = "/^[A-z0-9\._-]+"
         . "@"
         . "[A-z0-9][A-z0-9-]*"
         . "(\.[A-z0-9_-]+)*"
         . "\.([A-z]{2,6})$/";
  return preg_match ($pattern, $email);
}

function checkPassword($password) {
  $length = strlen ($password);
  if ($length <  {
    return FALSE;
  }
  $unique = strlen (count_chars ($password, 3));
  $difference = $unique / $length;
  echo $difference;
  if ($difference < .60) {
    return FALSE;
  }
  return preg_match ("/[A-z]+[0-9]+[A-z]+/", $password);
}
?>

 

Thanks for taking your time to read this.

 

Any help is greatly appreciated.

 

~DarkShadowWing~

Link to comment
https://forums.phpfreaks.com/topic/113396-php-input-text-file-verification-error/
Share on other sites

are these suppose to have the and(&&) or concocted together?

$valid = $valid && $em;

 

concocted:

$valid = $valid . $em;

 

ACE

 

That part I believe is correct. I found this code on a site. If you need the link I can give it to ya. The only thing which I changed was I added my own function in functions.php which would be the validcode($code); function & made it work with the checkMailCode($code, $country); function as after it checks to see if the zip code u entered is 1 of the zipcodes in the zipcodes.dat file, it checks to see whether it is US or CA. The prob is that I can't get it to check the zipcode data u enter with all of the lines in the zipcode.dats file to ensure it is a valid zipcode. I have tried everything and REALLY need a fix for this. It is going on a professional website I am creating for myself so any help would be GREATLY appreciated. =)

 

Thanks! =)

 

~DarkShadowWing~

Here is a small fix for functions.php . I still have the problem that is listed above though.

 

functions.php:

 

<?

function isDigits($element) {
  return !preg_match ("/[^0-9]/", $element);
}

function isLetters($element) {
  return !preg_match ("/[^A-z]/", $element);
}

function checkLength($string, $min, $max) {
  $length = strlen ($string);
  if (($length < $min) || ($length > $max)) {
    return FALSE;
  } else {
    return TRUE;
  }
}

function validcode($code){

$file = "zipcodes.dat";

$handle = @fopen($file, "r");

  $lines = file($file);

  $l_count = count($lines);

while(!feof($handle)) // Loop til the eof
{

if($handle){
  if($buffer = fgets($handle, 2048)){

    $parts = explode("=",$buffer);

    if($code != $parts[0]){

    for($x = 0; $x< $l_count; $x++){
     echo trim($parts[$x]); // If not string show contents.

}

    } else if($code == $parts[0]){
echo "incorrect zipcode";

}

}
}

}

/*  $lines = file($file);
  $l_count = count($lines);

   for($x = 0; $x< $l_count; $x++)
    {

  if($x > 0){ 

    return FALSE;

} else if($x == 0){

    if( $buffer <> $code ){

      return FALSE;

    } else if( $buffer == $code ) {

      echo "<script>alert('zipcode \'".trim($buffer)."\' is correct');</script>";

   }

  }

}*/

}

/*function validcode($code){

$file = "zipcodes.dat";

    $lines = file($file);
    $lcount = count($lines);

$handle = @fopen($file, "r");

while(!feof($handle)) // Loop til the eof
{
  for($x = 0; $x< $lcount; $x++)
    {

     $parts = explode('=', $s);
     
}
}
}

   if($s = fgets($handle,1048576)){

   $lcount++;

   print $lcount or die(php_errormsg);

    } else {

   print $lcount or die(php_errormsg);

      }
    }
  }
} */

function valid_code($code){

//    $line_counter = 0;
//    $desired_line = 0;

    $fh = fopen('zipcodes.dat','r') or die($php_errormsg);

//&& ($line_counter <= $desired_line)) {

     while(! feof($fh)){
         if($s = fgets($fh,1048576)){

            $parts = explode('=', $s);

            $line_counter++;

	 }

if($code <> trim($part[0]) || $code <> trim($part[1]) || $code <> trim($part[2]) || $code <> trim($part[3]) ){

return false;

} else if( $code == trim($part[0]) || trim($part[1]) || trim($part[2]) || trim($part[3]) ) {

return $code;

}

}

}

function checkMailCode($code, $country) {
  
  $code = preg_replace("/[\s|-]/", "", $code);
  $length = strlen ($code);

  switch (strtoupper ($country)) {
    case 'US':
      if (($length <> 5) && ($length <> 9)) {
        return FALSE;
      }
      return isDigits($code);
    case 'CA':
      if (($length <> 6 || $validate == "")) {
        return FALSE;
      }
      return preg_match ("/([A-z][0-9]){3}/", $code);
  }
}

function checkURL($url) {
  return preg_match ("/http:\/\/(.*)\.(.*)/i", $url);
}

function checkURLandConnect($url) {  
  if (!preg_match ("/http:\/\/(.*)\.(.*)/i", $url)) {
    return FALSE;
  }
  $parts = parse_url($url);
  $fp = fsockopen($parts['host'], 80, $errno, $errstr, 10);
  if(!$fp) {
    return FALSE;
  }
  fclose($fp);
  return TRUE;
}

function checkEmail($email) {
  $pattern = "/^[A-z0-9\._-]+"
         . "@"
         . "[A-z0-9][A-z0-9-]*"
         . "(\.[A-z0-9_-]+)*"
         . "\.([A-z]{2,6})$/";
  return preg_match ($pattern, $email);
}

function checkPassword($password) {
  $length = strlen ($password);
  if ($length <  {
    return FALSE;
  }
  $unique = strlen (count_chars ($password, 3));
  $difference = $unique / $length;
  echo $difference;
  if ($difference < .60) {
    return FALSE;
  }
  return preg_match ("/[A-z]+[0-9]+[A-z]+/", $password);
}
?>

 

Thanks!

 

~DarkShadowWing~

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.