Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My class always returns 1 even when it should be 0 1

Status
Not open for further replies.

webdev007

Programmer
Sep 9, 2005
168
I have a class that performs a few actions
I stripped it down to isolate my problem
I know that it works as a standalone function.

But in the class environment when performing a WHOIS
Regardless of the result to be 1 or 0 it always returns 1
it connect ok to the WHOIS servers

The first piece of code is the class

The second code calls it
Thank you to provide some “lights” :)

Code:
<?
class CheckUrl
{
var $url;
var $url_preg;
var $text;
var $badWords;
var $a_server;

// Check for duplicate of URL

// DUPLI CHECK **********

// BAD WORDS FILTER

 
// WHOIS URL checking

function whois ($url, $a_server, $a_port=43) 
{ 
global $result; 


$available = "No match"; 
$available2 = "Not found"; 

$this->url=$url;
$url = str_replace("[URL unfurl="true"]www.",[/URL] "", $url); 

$url = str_replace("[URL unfurl="true"]http://",[/URL] "", $url); 

switch (true) { 
  case preg_match("/.com/i", $url): 
  $a_server="whois.internic.net"; //echo "$a_server"; 
  break;
   
 case preg_match("/.net/i", $url): 
   $a_server="whois.internic.net"; //echo "$a_server"; 
  break;

	case preg_match("/.org/i", $url): 
  $a_server="whois.publicinterestregistry.net"; //echo "$a_server";  
  break; 
   
 case preg_match("/.edu/i", $url): 
 $a_server="whois.educause.edu"; //echo "$a_server"; 
  break;
   
  default: 
   echo 'default'; 
  break; 
}
$sock = @fsockopen($a_server,$a_port); 



   IF (!$sock) { 

   echo ("<b>Could Not Connect To Server.</b>"); 

   } 

   ELSE { 

$send_request =@fputs($sock,"$url\r\n"); 

      IF (!$send_request) { 
      echo ("<B>Unable to send request.</B>"); 
      } 

      ELSE { 

      while(!feof($sock)) { 

      $result .= fgets($sock,128); 
      } 

      $result = str_replace("\n", "<br>", $result); 




         IF (@eregi($available,$result) OR @eregi($available2,$result)) { 

        $result = 1; 

         } 



         ELSE { 

         $result = 0; 

         } 
   @fclose($sock); 

         } 

   } 

return ($result); 

} // ends function   

 									} // ends class 
		
?>

Code:
<? // USING THE CLASS ?>

<?
//error_reporting (E_ALL);

include"check_url.inc.php"; // the class
$test=$_POST['url'];

$url2 = new CheckUrl($test, $a_server);

$a_server = ltrim(substr($test, -3), "."); 
$url2->whois($test, $a_server);
echo "$result"; //used in test mode

if
($result == 1)
{
	 // then it exists, do nothing! // in test mode use echo"$result";
	 }
if($result == 0)
{
echo "Not ok";
	exit();
	}
	
?>
 
As I see it, your code has a number of problems.

First, pick an indentation style and stick with it. It's awfully hard for anyone to follow your code when your indentation style wanders.

Second, don't use the "@" error-supression modifier in development code. The whole purpose of the modifier is to stop errors from being displayed...which is the whole point of development. Also, don't use the "@" operator to stop output that would be better handled in code.

Third, you're using the "global" operator on $result and then returning $result. Do one or othe other, but not both.

Fourth, pick one regular expression system, either preg or ereg, and use it only. Don't mix and match, as it makes your code harder to read.

Fifth, don't use regular expression functions when you don't have to. None of your use of regular expression functions acutally use regular expressions, so they can be replaced with simpler, less resource intensive, and faster-running non-regular express replacements, such as strstr().

Sixth, you're using "OR" in an if-statement condition inappropriately. The "OR" and "||" operators are similar but not equivalent, as one has a higher operator precedence than the other. In your if-statement condition, "||" is more appropriate. In a statement like "function(...) OR die();", then "OR" is the appropriate operator. In 99.9% percent of the time, it won't matter which you use. But in that .1%, you will be driven to distraction spending days trying to find some sublte logic bug brought on by the wrong operator.

Seventh, the output of your function and how the driving script reacts to it don't match.


Here's my version of your class:
Code:
<?
class CheckUrl
{
	var $url;
	var $url_preg;
	var $text;
	var	$badWords;
	var $a_server;

// Check for duplicate of URL

// DUPLI CHECK **********

// BAD WORDS FILTER

 
// WHOIS URL checking

	function whois ($url, $a_server, $a_port=43) 
	{ 
		$result = '';
		$available = "No match"; 
		$available2 = "Not found"; 

		$this->url=$url;
		$url = str_replace("[URL unfurl="true"]www.",[/URL] "", $url); 

		$url = str_replace("[URL unfurl="true"]http://",[/URL] "", $url); 

		switch (true)
		{ 
			case preg_match("/.com$/i", $url): 
		  		$a_server="whois.internic.net"; //echo "$a_server"; 
		  		break;
			
			case preg_match("/.net$/i", $url): 
				$a_server="whois.internic.net"; //echo "$a_server"; 
				break;
				
			case preg_match("/.org$'/i", $url): 
				$a_server="whois.publicinterestregistry.net"; //echo "$a_server";  
				break; 
				
			case preg_match("/.edu$/i", $url): 
				$a_server="whois.educause.edu"; //echo "$a_server"; 
				break;
				
			default: 
				$a_server = '';
				echo 'default'; 
				break; 
		}

		if ($a_server != '')
		{
			$sock = fsockopen($a_server,$a_port); 
			
			IF (!$sock)
			{ 
				echo ("<b>Could Not Connect To Server.</b>"); 
			} 
			ELSE
			{ 
				$send_request = fputs($sock,"$url\r\n"); 
		
				IF (!$send_request)
				{ 
					echo ("<B>Unable to send request.</B>"); 
				} 
				ELSE
				{ 
					while(!feof($sock))
					{ 
						$result .= fgets($sock,128); 
					} 
					fclose($sock); 
					
					$result = str_replace("\n", "<br>", $result); 
					
					IF (strstr($result, $available) || strstr($result, $available2))
					{ 
						$result = 0; 
					} 
					ELSE
					{ 
						$result = 1; 
					} 
	
				} 
			} 
		}
		else
		{
			$result = 0;
		}
		
		return ($result); 
	} // ends function	
} // ends class 
		  
?>

and my version of your class driver:

Code:
<?
error_reporting (E_ALL);

include"test_foo_class.php"; // the class
//$test=$_POST['url'];
//$test = '[URL unfurl="true"]http://www.fjsdlk.com';[/URL]
$test = '[URL unfurl="true"]http://www.tek-tips.com';[/URL]

$url2 = new CheckUrl();

$a_server = ltrim(substr($test, -3), "."); 
$result = $url2->whois($test, $a_server);
echo "$result"; //used in test mode

if ($result == 1)
{
     // then it exists, do nothing! // in test mode use echo"$result";
}
else
{
	echo "Not ok";
}
    
?>[/code}

I don't know what's different, but when send a known-bad domain name, I get 0.  When I send a known-good domain name, I get a 1.



Want the best answers? [url=http://www.catb.org/~esr/faqs/smart-questions.html]Ask the best questions![/url]  TANSTAAFL!
 
sleipnir214,
Thank you for your time and direct to the point explanations, it makes much more sense.
I really appreciated it
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top