11 Replies - 596 Views - Last Post: 18 July 2012 - 11:02 AM Rate Topic: -----

#1 adolf625  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 161
  • Joined: 22-November 08

Not returning an IP address in form

Posted 17 July 2012 - 05:48 AM

I am having issues trying to get the form to return a IP address whenever I go to run the query for the database. It keeps returning a blank string, here is the functions for the ip address if someone has some pointers, thanks.

  Function validateIP($fieldSuffix, $bolRequired, $formType="POST", $requireAllParts=TRUE) {
      global $strError, $HTTP_POST_VARS;

      $ip1  = "txtIP1".$fieldSuffix;
      $ip2  = "txtIP2".$fieldSuffix;
      $ip3  = "txtIP3".$fieldSuffix;
      $ip4  = "txtIP4".$fieldSuffix;

      If ($formType == "GET") {
        global $HTTP_GET_VARS;
        $ip1  = Trim(strip_tags($HTTP_GET_VARS[$ip1]));
        $ip2  = Trim(strip_tags($HTTP_GET_VARS[$ip2]));
        $ip3  = Trim(strip_tags($HTTP_GET_VARS[$ip3]));
        $ip4  = Trim(strip_tags($HTTP_GET_VARS[$ip4]));
      } Else {
        $ip1  = Trim(strip_tags($HTTP_POST_VARS[$ip1]));
        $ip2  = Trim(strip_tags($HTTP_POST_VARS[$ip2]));
        $ip3  = Trim(strip_tags($HTTP_POST_VARS[$ip3]));
        $ip4  = Trim(strip_tags($HTTP_POST_VARS[$ip4]));
      }

      $ip1 = str_replace(".", "", $ip1);
      $ip2 = str_replace(".", "", $ip2);
      $ip3 = str_replace(".", "", $ip3);
      $ip4 = str_replace(".", "", $ip4);

      If ($bolRequired OR (($ip1 OR $ip2 OR $ip3 OR $ip4) AND $requireAllParts)) {
        $ipRequired = TRUE;
      }

      If ($ipRequired AND (($ip1=="") OR ($ip2=="") OR ($ip3=="") OR ($ip4==""))) {
         fillError("Please specify <u>all</u> parts of the IP Address.");
      }

      $strIP1  = validateExactNumber("Each part of the IP Address", $ip1, 0, 255, $ipRequired, 0);
      $strIP2  = validateExactNumber("Each part of the IP Address", $ip2, 0, 255, $ipRequired, 0);
      $strIP3  = validateExactNumber("Each part of the IP Address", $ip3, 0, 255, $ipRequired, 0);
      $strIP4  = validateExactNumber("Each part of the IP Address", $ip4, 0, 255, $ipRequired, 0);

      If ($strIP1 OR $strIP2 OR $strIP3 OR $strIP4) {
        return $strIP1.".".$strIP2.".".$strIP3.".".$strIP4;
      } Else {
          return "";
      }
  }

  Function buildIP($value, $fieldSuffix) {
     If ($value) {
         $dot1 = strpos($value, ".", 0);
         $dot2 = strpos($value, ".", ($dot1+1));
         $dot3 = strpos($value, ".", ($dot2+1));
     } else {
         $dot1 = $dot2 = $dot3 = "";
     }

     $strIP1 = substr($value, 0, $dot1);
     $strIP2 = substr($value, ($dot1+1), (($dot2-$dot1)-1));
     $strIP3 = substr($value, ($dot2+1), (($dot3-$dot2)-1));
     $strIP4 = substr($value, ($dot3+1));
?>
     <input type='text' name='txtIP1<?php echo $fieldSuffix; ?>' value='<?php echo $strIP1; ?>' size='3' maxlength='3'> <b>.</b> 
     <input type='text' name='txtIP2<?php echo $fieldSuffix; ?>' value='<?php echo $strIP2; ?>' size='3' maxlength='3'> <b>.</b>
     <input type='text' name='txtIP3<?php echo $fieldSuffix; ?>' value='<?php echo $strIP3; ?>' size='3' maxlength='3'> <b>.</b>
     <input type='text' name='txtIP4<?php echo $fieldSuffix; ?>' value='<?php echo $strIP4; ?>' size='3' maxlength='3'>
<?php
  }


Is This A Good Question/Topic? 0
  • +

Replies To: Not returning an IP address in form

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Not returning an IP address in form

Posted 17 July 2012 - 05:52 AM

Holy crap, where did you dig up this old code??? It's very, very bad.

What version of PHP are you using?

EDIT: Ah, you found it here.
Was This Post Helpful? 0
  • +
  • -

#3 adolf625  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 161
  • Joined: 22-November 08

Re: Not returning an IP address in form

Posted 17 July 2012 - 05:56 AM

It is from an open source program I found phpMyInventory v2.81. I am using version 5.3.3 on Rehat, I have already had to make several changes to make some of the other things work, but I can't seem to figure what I need to do to this to get it working and up to date.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Not returning an IP address in form

Posted 17 July 2012 - 06:00 AM

filter_var

Validation filters.

That code is shit. If you're going to copy/paste code, then you need to pay attention. From the page for that abortion:

Quote

Currently stable on PHP version 4xx only! (Not PHP 5).


Welcome to the pitfalls of random copy/paste.
Was This Post Helpful? 2
  • +
  • -

#5 adolf625  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 161
  • Joined: 22-November 08

Re: Not returning an IP address in form

Posted 17 July 2012 - 06:10 AM

I had downloaded the updated one from the patches page that the guy said he updated it to 5.3 and that it was working, but obviously that isn't the case, thanks.
Was This Post Helpful? 0
  • +
  • -

#6 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Not returning an IP address in form

Posted 17 July 2012 - 06:23 AM

No this is not PHP 5.3 and if this is meant to be PHP 5.3 code then he deserves to be shot (no, really).

Explain to us what it is that you're trying to do exactly? Get the client's IP? Validate some IPs that are sent through on a form?

For the former, use $_SERVER['REMOTE_ADDR'], for the latter, like JackOfAllTrades wrote, use (filter_var($_POST['ip_field'], FILTER_VALIDATE_IP) !== false)

This post has been edited by RudiVisser: 17 July 2012 - 06:24 AM

Was This Post Helpful? 1
  • +
  • -

#7 adolf625  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 161
  • Joined: 22-November 08

Re: Not returning an IP address in form

Posted 17 July 2012 - 06:31 AM

I just need to enter an ip address into the form and convert that IP to a string so I can store it in the database, but it comes back blank, I am trying to use the functions y'all have showed me, but it is giving me an HTTP 500 Internal server error, I am trying to convert it all now, thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#8 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Not returning an IP address in form

Posted 17 July 2012 - 06:44 AM

This? Why do you need a big script for that?

<input type="text" name="ipaddr" />

<?php
if (filter_var($_POST['ipaddr'], FILTER_VALIDATE_IP) !== false) {
    $ip_addr = $_POST['ipaddr'];
    // .. insert into DB
} else {
    die('Not a real IP');
}


EDIT: Oh right, you want individual boxes?

<input type="text" name="ip_p1" />.<input type="text" name="ip_p2" />.<input type="text" name="ip_p3" />.<input type="text" name="ip_p3" />

<?php
if (!isset($_POST['ip_p1']) || !is_int($_POST['ip_p1']) || strlen($_POST['ip_p1']) > 3)
    die('IP Part 1 is not valid.');
if (!isset($_POST['ip_p2']) || !is_int($_POST['ip_p2']) || strlen($_POST['ip_p2']) > 3)
    die('IP Part 2 is not valid.');
// 3
// 4
$ip_addr = $_POST['ip_p1'] . '.' . $_POST['ip_p2'] . '.' . $_POST['ip_p3'] . '.' . $_POST['ip_p4'];
if (filter_var($ip_addr, FILTER_VALIDATE_IP) !== false) {
    // .. insert into DB
} else {
    die('Not a real IP');
}


EDIT2: BTW, if you're getting 500 Internal Server error, then what is the error? Either look in the error logs or enable `display_errors` and take a look.

This post has been edited by RudiVisser: 17 July 2012 - 06:49 AM

Was This Post Helpful? 1
  • +
  • -

#9 adolf625  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 161
  • Joined: 22-November 08

Re: Not returning an IP address in form

Posted 17 July 2012 - 07:00 AM

That should do it now, thanks a lot, IDK why the big function was needed, I guess they didn't know what they was doing and then I got it and didn't know what I was doing, this is the first PHP project I've took on, so thanks for the guidance.
Was This Post Helpful? 0
  • +
  • -

#10 adolf625  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 161
  • Joined: 22-November 08

Re: Not returning an IP address in form

Posted 17 July 2012 - 07:00 AM

'

This post has been edited by adolf625: 17 July 2012 - 07:17 AM

Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Not returning an IP address in form

Posted 18 July 2012 - 05:20 AM

Just because I hate code duplication:

for ($i = 1; $i <= 4; ++$i)
{
   if (!isset($_POST['ip_p' . $i]) || !is_int($_POST['ip_p' . $i]) || strlen($_POST['ip_p' . $i]) > 3)
    die("IP Part $i is not valid.");
}

Was This Post Helpful? 2
  • +
  • -

#12 adolf625  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 161
  • Joined: 22-November 08

Re: Not returning an IP address in form

Posted 18 July 2012 - 11:02 AM

Something else I figured out to fix the issue is to change the $HTTP_POST_VARS function to just $_POST and to replace dbquery with mysql_query, these changes made it work with 5.3 also.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1