Php and xml

load xml data then check if it equals a variable

Page 1 of 1

10 Replies - 909 Views - Last Post: 29 June 2010 - 06:12 AM Rate Topic: -----

#1 Guest_Daniel*


Reputation:

Php and xml

Posted 26 June 2010 - 07:37 AM

Here is what Im trying to do,
If an ip/domain name matches one found in an xml database then display a message.

Here is what i have so far.

Php File
<?php
$doc = new DOMDocument();
$doc->load( 'db.xml' );

$ips = $doc->getElementsByTagName( "ip" );
foreach( $ips as $ip )
{
$nums = $ip->getElementsByTagName( "num" );
$num1 = $nums->item(0)->nodeValue;
}
$addy = "localhost";
if ($addy == $num1) {
echo "Match";
} else {
echo "No Match";
}
?>



Xml File
<?xml version="1.0"?>
<ips>
<ip><num>localhost</num></ip>
<ip><num>127.0.0.1</num></ip>
<ip><num>google.com</num></ip>
</ips>


Any help would be great.
Thanks Dan

Is This A Good Question/Topic? 0

Replies To: Php and xml

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3063
  • View blog
  • Posts: 10,736
  • Joined: 08-August 08

Re: Php and xml

Posted 26 June 2010 - 07:53 AM

This:
$ips = $doc->getElementsByTagName( "ip" );


looks to me like it should be:
$ips = $doc->getElementsByTagName( "ips" );


Was This Post Helpful? 2
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6087
  • View blog
  • Posts: 23,597
  • Joined: 23-August 08

Re: Php and xml

Posted 26 June 2010 - 08:14 AM

Using XPath:
<?php
// Load the doc
$doc = new DOMDocument();
$doc->load( 'db.xml' );

// What we're going to search for (first will work, other will fail)
$values = array("localhost", "10.10.10.10");

// Create an XPath object for the doc
$xpath = new DOMXpath($doc);
foreach ($values as $value)
{
    // Create a query (get the count of all nums with the provided value).
    $query = 'count(/ips/ip/num[. = "' . $value . '"])';
    // Print result
    echo $value . ($xpath->evaluate($query) > 0 ? ' exists' : ' does not exist') . "\n"
}
?>


Was This Post Helpful? 4
  • +
  • -

#4 mahcuz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 213
  • Joined: 03-June 10

Re: Php and xml

Posted 26 June 2010 - 08:28 AM

You're using parentheses instead of square brackets.

<?php
$doc = new DOMDocument();
$doc->load( 'db.xml' );

$ips = $doc->getElementsByTagName( "ip" );
foreach( $ips as $ip )
{
$nums = $ip->getElementsByTagName( "num" );
$num1 = $nums->item[0]->nodeValue;
}
$addy = "localhost";
if ($addy == $num1) {
echo "Match";
} else {
echo "No Match";
}
?>


Also, turn on error reporting.

error_reporting(-1);

Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3567
  • View blog
  • Posts: 10,399
  • Joined: 08-June 10

Re: Php and xml

Posted 26 June 2010 - 01:30 PM

View Postmahcuz, on 26 June 2010 - 03:28 PM, said:

You're using parentheses instead of square brackets.

I have to correct you there. ->item(0) is the correct usage here (item() is a method in the NodeList interface). DOM in PHP is way stricter than DOM in Javascript.

This post has been edited by Dormilich: 28 June 2010 - 09:24 PM

Was This Post Helpful? 1
  • +
  • -

#6 Guest_Daniel*


Reputation:

Re: Php and xml

Posted 26 June 2010 - 07:05 PM

Thanks guys,
I was going to use that little script within a port scanner i made awhile ago.
But then I decided that I wanted to stop people from submitting the server its self.

Here is the code for the port script.

<?php if(isset($_POST['Submit'])) {
if (!$_POST['ipaddress'] | !$_POST['portnumber']) {
echo "
<html>
<head>
<title>Online Port Tester.</title>
</head>
<body>
<div id='Content'>
<table width='380' height='110' border='0' cellpadding='0' cellspacing='0'>
<form action='index2.php' method='POST'>
  <tr>
    <td colspan='2'><h3>Please enter the following details.</h3></td>
  </tr>
  <tr>
    <td>Ip Address/Dns:</td>
    <td><input type='text' name='ipaddress' maxlength='20' title='Limited to 20 Characters' />
      <font color='#FF0000'>*</font> Required</td>
  </tr>
  <tr>
    <td>Port:</td>
    <td><input type='text' name='portnumber' maxlength='5' />
      <font color='#FF0000'>* </font>Required</td>
  </tr>
  <tr>
    <td width='102' height='33'>&nbsp;</td>
    <td width='278'><input type='submit' value='Submit' name='Submit' /><input type='reset' value='Reset' /></td>
  </tr>
</form>
<div>$r</div>
</table>
</div>
</body>
</html>
";
} else {
$ipaddress = $_POST['ipaddress'];
$port = $_POST['portnumber'];
if (!$sock = @fsockopen($ipaddress, $port, $num, $error, 1))
echo "
<html>
<head>
<title>Online Port Tester.</title>
</head>
<body>
<div id='Content'>
<table width='380' height='110' border='0' cellpadding='0' cellspacing='0'>
<form action='index2.php' method='POST'>
  <tr>
    <td colspan='2'><h3>Please enter the following details.</h3></td>
  </tr>
  <tr>
    <td>Ip Address/Dns:</td>
    <td><input type='text' name='ipaddress' maxlength='20' title='Limited to 20 Characters' /></td>
  </tr>
  <tr>
    <td>Port:</td>
    <td><input type='text' name='portnumber' maxlength='5' /></td>
  </tr>
  <tr>
    <td width='102' height='33'>&nbsp;</td>
    <td width='278'><input type='submit' value='Submit' name='Submit' /><input type='reset' value='Reset' /></td>
  </tr>
  <tr>
  	<td colspan='2'><strong>$ipaddress:$port</strong> Is currently <font color='#FF0000'>Offline</font></td>
  </tr>
</form>
</table>
</div>
</body>
</html>
"; 
else {
echo "
<html>
<head>
<title>Online Port Tester.</title>
</head>
<body>
<div id='Content'>
<table width='380' height='110' border='0' cellpadding='0' cellspacing='0'>
<form action='index2.php' method='POST'>
  <tr>
    <td colspan='2'><h3>Please enter the following details.</h3></td>
  </tr>
  <tr>
    <td>Ip Address/Dns:</td>
    <td><input type='text' name='ipaddress' maxlength='20' title='Limited to 20 Characters' /></td>
  </tr>
  <tr>
    <td>Port:</td>
    <td><input type='text' name='portnumber' maxlength='5' /></td>
  </tr>
  <tr>
    <td width='102' height='33'>&nbsp;</td>
    <td width='278'><input type='submit' value='Submit' name='Submit' /><input type='reset' value='Reset' /></td>
  </tr>
  <tr>
  	<td colspan='2'><strong>$ipaddress:$port</strong> Is currently <font color='#0000FF'>Online</font></td>
  </tr>
</form>
</table>
</div>
</body>
</html>
"; 
fclose($sock);
}
}
} else {
echo "
<html>
<head>
<title>Online Port Tester.</title>
</head>
<body>
<div id='Content'>
<table width='380' height='110' border='0' cellpadding='0' cellspacing='0'>
<form action='index2.php' method='POST'>
  <tr>
    <td colspan='2'><h3>Please enter the following details.</h3></td>
  </tr>
  <tr>
    <td>Ip Address/Dns:</td>
    <td><input type='text' name='ipaddress' maxlength='20' title='20 Character limit' /></td>
  </tr>
  <tr>
    <td>Port:</td>
    <td><input type='text' name='portnumber' maxlength='5' title='5 Character limit' /></td>
  </tr>
  <tr>
    <td width='102' height='33'>&nbsp;</td>
    <td width='278'><input type='submit' value='Submit' name='Submit' /><input type='reset' value='Reset' /></td>
  </tr>
</form>
</table>
</div>
</body>
</html>
"; 
}
?>

Was This Post Helpful? 0

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3063
  • View blog
  • Posts: 10,736
  • Joined: 08-August 08

Re: Php and xml

Posted 26 June 2010 - 07:43 PM

View PostDaniel, on 26 June 2010 - 09:05 PM, said:

Here is the code for the port script.

And?
Was This Post Helpful? 0
  • +
  • -

#8 Guest_Daniel*


Reputation:

Re: Php and xml

Posted 28 June 2010 - 07:44 PM

Never mind, I got that working.

Have another question.
Say if i used the example JackOfAllTrades posted to search the database.
How would i get the script to display the returned value plus another value within that same parent?

Basically address 50.0.0.1 is found, display the date.

<?xml version="1.0"?>
<people>
	<person>
		<Ipaddress>50.0.0.1</Ipaddress>
		<Time>20:00:53</Time>
		<Date>28/06/10</Date>
		<Name>Test</Name>
		<Email>Test@Test.com</Email>
		<Comments>Comments</Comments>
	</person>
</people>



I have no idea on how to do this, any ideas?

This post has been edited by no2pencil: 03 July 2010 - 07:24 PM
Reason for edit:: Added code tags

Was This Post Helpful? 0

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3063
  • View blog
  • Posts: 10,736
  • Joined: 08-August 08

Re: Php and xml

Posted 28 June 2010 - 07:58 PM

I haven't tested it, but I believe that:
$num1 = $nums->item[0]->nodeValue;


would change to:
$num1 = $nums->item[1]->nodeValue;


Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3567
  • View blog
  • Posts: 10,399
  • Joined: 08-June 10

Re: Php and xml

Posted 28 June 2010 - 09:27 PM

View PostCTphpnwb, on 29 June 2010 - 02:58 AM, said:

I haven't tested it, but I believe that:
$num1 = $nums->item[0]->nodeValue;



DOM-level-3 Core said:

  interface NodeList {
    Node               item(in unsigned long index);
    readonly attribute unsigned long   length;
  };


$num1 = $nums->item(0)->nodeValue;

Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6087
  • View blog
  • Posts: 23,597
  • Joined: 23-August 08

Re: Php and xml

Posted 29 June 2010 - 06:12 AM

If you spent a little time reading the code I provided and perhaps trying to understand what it does, you would find that it does nothing but get how many times the IP address appears in the /ips/ip/num node, and if it's not 0 then the IP address exists. You need to change it considerably to do what you want now. Please don't be a help vampire.

EDIT: Here's a big hint:
$query = '//people/person/Ipaddress[. = "' . $value . '"]/../Name';

This post has been edited by JackOfAllTrades: 29 June 2010 - 06:55 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1