Jump to content

bug using fsockopen in for loop


bluesoul

Recommended Posts

I'm not sure where the exact problem is here.  I'm trying to make a script to compare expiration dates of domains in our database against the dates on the actual WHOIS servers, it takes an array (which appears to be fine in print_r) and does the following

 

function array_whois($array) 
{
if (!is_array($array)) { die('WHOIS script was not passed an array.'); }
else 
{
$count = count($array);
$i = 0;

	for($i = 0; $i < $count; $i++)
	{
		$domain[$i] = $array[$i];
		$tempdomain = $domain[$i];
		$q = "SELECT tld, exp_date FROM Domain WHERE domain_name = '$tempdomain'";
			$query = mssql_query($q) or die(mssql_get_last_message());
				$info = mssql_fetch_array($query);
		$topleveldomain[$i] = strtolower($info["tld"]); // necessary to avoid conflict with $tld for whoisservers.php
		$tld = $topleveldomain[$i]; // $tld gets used to find the right WHOIS server
		$exp_date[$i] = $info["exp_date"];


		include('whoisservers.php'); // there's a case-switch with TLDs and their corresponding registries.

		if($comnet == 1) //$comnet is defined as 0 in whoisservers.php and set to 1 in the instances of com and net.
			{ // .COM and .NET domains operate differently, Network Solutions will redirect you to another registrar.
				$fp = fsockopen($server,43);
				fputs($fp, "$tempdomain\r\n");
				$num = 10; // Retrieving the line that specifies the correct whois server, in this case line 10.
				for ($i=0; $i<$num; $i++)
				$line = fgets($fp);

				if (strlen($line) > 1) 
					{ // Actual Network Solutions-owned names have a blank line 10 so comparing string length works.
					$server = substr($line,17); // Snip everything before the start of the correct whois server.
					$server = substr($server,0,-1); // Snip the trailing linebreak and this is now the server we'll check against, and the user never sees any of this.
					}
			}

		/*echo '$array'; print_r($array);
		echo '$domain'; print_r($domain[$i]);
		echo '$tld'; print_r($tld);
		echo '$exp_date'; print_r($exp_date[$i]);
		echo '$server'; print_r($server);		
		*/


		$fp = fsockopen($server,43); // we define $server in whoisservers.php and later refine it if comnet = 1
		fputs($fp, "$tempdomain\r\n");
		while(!feof($fp))
		{
			echo stristr(fgets($fp),"expir");

		}
		fclose($fp);

	} //end for loop
} //end else
}//end function

 

This works fine with an array with 1 element but any more than that and the script times out.  Any ideas?

Link to comment
Share on other sites

One, put the include inside the for loop outside of it, including it multiple times could probably be causing it, I am guessing it just defines some variables, so yea no need to have it included each loop.

 

Second you are re-using $i for the inside for loop, change this to be $x or something like that.

 

Link to comment
Share on other sites

Good catch on the second $i, I completely overlooked it.

 

I've turned the case-switch in whoisservers.php into a function and call it now, and I'm having the same error.

 

 

Notice: Undefined variable: server in expwhois.php on line 49

 

Warning: fsockopen() [function.fsockopen]: unable to connect to :43 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ) in expwhois.php on line 49

 

Fatal error: Maximum execution time of 30 seconds exceeded in expwhois.php on line 49

 

EDIT:  It's now not working for even one element.

Link to comment
Share on other sites

1. Show us the new script.

 

2. I'd suggest a foreach loop rather than a for loop for iterating an array...

 

2. I tried foreach but I've never used it before and made a mess of it.  :P  Guess I didn't do much better here but I at least got a result with 1 element.

 

1.

 

<?php

function array_whois($array) 
{
if (!is_array($array)) { die('WHOIS script was not passed an array.'); }
else 
{
$count = count($array);
$i = 0;
			include('whoisservers2.php'); // there's a case-switch with TLDs and their corresponding registries.

	for($i = 0; $i < $count; $i++)
	{
		$domain[$i] = $array[$i];
		$tempdomain = $domain[$i];
		$q = "SELECT tld, exp_date FROM Domain WHERE domain_name = '$tempdomain'";
			$query = mssql_query($q) or die(mssql_get_last_message());
				$info = mssql_fetch_array($query);
		$topleveldomain[$i] = strtolower($info["tld"]); // necessary to avoid conflict with $tld for whoisservers.php
		$tld = $topleveldomain[$i]; // $tld gets used to find the right WHOIS server
		$exp_date[$i] = $info["exp_date"];
		$comnet = 0;
		$server = getserver();

		if($comnet == 1) 
			{ // .COM and .NET domains operate differently, Network Solutions will redirect you to another registrar.
				$j = 0;
				$fp = fsockopen($server,43);
				fputs($fp, "$tempdomain\r\n");
				$num = 10; // Retrieving the line that specifies the correct whois server, in this case line 10.
				for ($j=0; $j<$num; $j++)
				$line = fgets($fp);

				if (strlen($line) > 1) 
					{ // Actual Network Solutions-owned names have a blank line 10 so comparing string length works.
					$server = substr($line,17); // Snip everything before the start of the correct whois server.
					$server = substr($server,0,-1); // Snip the trailing linebreak and this is now the server we'll check against, and the user never sees any of this.
					}
			}

		/*echo '$array'; print_r($array);
		echo '$domain'; print_r($domain[$i]);
		echo '$tld'; print_r($tld);
		echo '$exp_date'; print_r($exp_date[$i]);
		echo '$server'; print_r($server);		
		*/


		$fp = fsockopen($server,43); // we define $server in whoisservers.php and later refine it if comnet = 1
		fputs($fp, "$tempdomain\r\n");
		while(!feof($fp))
		{
			echo stristr(fgets($fp),"expir");
		}
		fclose($fp);

	} //end for loop
} //end else
}//end function
?>

 

And a relevant snippet of whoisservers2.php:

 

<?
function getserver() {
//nothing in here should need modifying unless you find a particular whois server is either missing or incorrect.
global $comnet;
global $tld;
switch($tld) {
case 'ac':
$server = 'whois.nic.ac';
break;
//etc
}
return $server;
}
?>

Link to comment
Share on other sites

Made some additional changes and the fact that it's still not working is surprising.

 

expwhois.php:

 

<?php

function array_whois($array) 
{
if (!is_array($array)) { die('WHOIS script was not passed an array.'); }
else 
{
	$count = count($array);
	$i = 0;
	include('whoisservers2.php'); // there's a case-switch with TLDs and their corresponding registries.

	for($i = 0; $i < $count; $i++)
	{
		$domain[$i] = $array[$i];
		$tempdomain = $domain[$i];
		$q = "SELECT tld, exp_date FROM Domain WHERE domain_name = '$tempdomain'";
			$query = mssql_query($q) or die(mssql_get_last_message());
				$info = mssql_fetch_array($query);
		$topleveldomain[$i] = strtolower($info["tld"]); // necessary to avoid conflict with $tld for whoisservers.php
		$tld = $topleveldomain[$i]; // $tld gets used to find the right WHOIS server
		$exp_date[$i] = $info["exp_date"];
		$comnet = 0;
		getserver();

		if($comnet == 1) 
			{ // .COM and .NET domains operate differently, Network Solutions will redirect you to another registrar.
				$j = 0;
				$fp = fsockopen($server,43);
				fputs($fp, "$tempdomain\r\n");
				$num = 10; // Retrieving the line that specifies the correct whois server, in this case line 10.
				for ($j=0; $j<$num; $j++)
				$line = fgets($fp);

				if (strlen($line) > 1) 
					{ // Actual Network Solutions-owned names have a blank line 10 so comparing string length works.
					$server = substr($line,17); // Snip everything before the start of the correct whois server.
					$server = substr($server,0,-1); // Snip the trailing linebreak and this is now the server we'll check against, and the user never sees any of this.
					}
			}

		echo '$array: '; print_r($array);
		echo '<br>$domain: '; print_r($domain[$i]);
		echo '<br>$tld: '; print_r($tld);
		echo '<br>$exp_date: '; print_r($exp_date[$i]);
		echo '<br>$server: '; print_r($server);		



		$fp = fsockopen($server,43); // we define $server in whoisservers.php and later refine it if comnet = 1
		fputs($fp, "$tempdomain\r\n");
		while(!feof($fp))
		{
			echo stristr(fgets($fp),"expir");
		}
				fclose($fp);

	} //end for loop
} //end else
}//end function
?>

 

whoisservers2.php:

<?
function getserver() {
//nothing in here should need modifying unless you find a particular whois server is either missing or incorrect.
echo "function called";
global $comnet;
global $tld;
global $server;
switch($tld) {
case 'ac':
$server = 'whois.nic.ac';
break;
...
case 'com':
echo "hi";
$server = 'whois.internic.net';
$comnet = 1;
...
default:
echo "default";
$server = 'whois.internic.net';
$comnet = 1;
break;

 

A submission of an array containing level-up.com yields this:

 

function called

default

$array: Array ( [0] => level-up.com )

$domain: level-up.com

$tld: com

$exp_date: Aug 2 2009 12:00AM

$server:

Notice: Undefined variable: server in expwhois.php on line 45

 

Notice: Undefined variable: server in expwhois.php on line 49

 

Warning: fsockopen() [function.fsockopen]: unable to connect to :43 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ) in expwhois.php on line 49

 

Fatal error: Maximum execution time of 30 seconds exceeded in expwhois.php on line 49

 

----

 

So it seems like it's getting stuck somewhere in the case switch.

Link to comment
Share on other sites

unable to connect to :43

 

$server is not being defined, hence the :43.

 

Right, which makes the logical question WHY is it not being defined when the switch uses the default case which assigns it a $server.  It's gotta be a scope problem but I just don't see where.

Link to comment
Share on other sites

You hit the nail on the head.

 

$server is defined in a function and is not considered a global. Any item defined in a function for it to be used elsewhere needs to be defined a global variable, or assigned to a session variable.

 

Which ever you choose, I would use the session, globals are just messy. But that is just me. The same will be true for tld and comnet inside the function.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.