Jump to content

[SOLVED] I give up - validation test not working


leothelion

Recommended Posts

why does the following test not work only if the number 0 is entered?

 

if ($_POST['ipaddr'] >= '1' && $_POST['ipaddr'] <= '254')  {

  echo "<br>good number";

  }

else {

  echo "<br>Please enter a number between 1 and 254";

  }

exit();

when 0 is entered I see a blank screen - if I enter 255 or greater I see "please enter a number...

 

I would try this and see how it goes...

if (intval($_POST['ipaddr'])>=1 && intval($_POST['ipaddr'])<=254) {

I can't see anything else that could be up with this - it's probably the actual contents of 'ipaddr' not being a single integer. I'm thinking that it stands for "IP Address" and if so, might want to try using echo to show the contents of the $_POST var immediately before the above if().

echo 'DEBUG: '.$_POST['ipaddr'];

here's the entire program as requested...thanks

<html><head><title>jim ></title></head>

<body>

 

<?php

function check_ipaddr($arg) {

  // checks to see if dns record exists for ipaddr passed

  // if yes returns true

  exec("dig -x $arg +short",$diga);

  if ($diga) return true;

  }

function check_hostname($arg1) {

  // checks to see if dns record exists for hostname passed

  // if yes returns true

  exec("dig $arg1 +short",$diga);

  if ($diga) return true;

  }

 

if($_SERVER['REQUEST_METHOD'] == 'GET') {

?>

 

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">

Enter hostname:

<input type=text name=hostname><br>

Enter IP number: 123.56.134.

<input type=text maxlength=3 size=3 name=ipaddr><br>

<input type=submit value="Check It">

<input type=reset>

</form>

 

<?php

  } else {

        if (!empty($_POST['hostname'])) {

          $hostname = $_POST['hostname'].".example.com";

          if (check_hostname($hostname)) {

              echo "<br>That hostname $hostname is already in use";

              }

          else {

              echo "<br>That hostname $hostname is free";

              }

          }

        if (!empty($_POST['ipaddr'])) {

          echo $_POST['ipaddr'];

          if ($_POST['ipaddr'] >= 1 && $_POST['ipaddr'] <= 254)

{

              $ipaddr = "123.56.134.".$_POST['ipaddr'];

              if (check_ipaddr($ipaddr)) {

                echo "<br>That ipaddr ".$ipaddr." is already in use";

                }

              }

          else {

              echo "<br>Please enter a number between 1 and 254";

              exit();

              }

          }

        }

?>

sorry...i'm working from home now and screwed up the cut/paste from a dos telnet session.

the last piece of code looks like this

 

        if (!empty($_POST['ipaddr'])) {

          echo $_POST['ipaddr'];

          if ($_POST['ipaddr'] >= 1 && $_POST['ipaddr'] <= 254) {

              $ipaddr = "204.62.134.".$_POST['ipaddr'];

              if (check_ipaddr($ipaddr)) {

                echo "<br>That ipaddr ".$ipaddr." is already in use";

                }

              else {

                echo "<br>That ipaddr $ipaddr is free";

                }

              }

          else {

              echo "<br>Please enter a number between 1 and 254";

              exit();

              }

          }

        }

?>

 

</body></html>

~

I should have been using isset rather than !empty.

!empty will not work for 0

0 is considered empty in php even if the user typed in a numeric 0 as input.

this was causing the program to fall through all the logic producing the blank screen.

not exactly intuitive....

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.