15, October 2008

More error checking in the fsockopen stage - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  PHP classes @finalwebsites.com  >  PHP Whois script (Moderator: Olaf)
Topic: More error checking in the fsockopen stage
« previous next »
Pages: [1] Print

Author Topic: More error checking in the fsockopen stage  (Read 1444 times)
Google dot what?
*
Posts: 2
16 credits
Members referred : 0



« on: Oct 25, 2006, 07:57:38 PM »

The script is very useful - better than others I've found on the internet. I've been trying it out, and sometimes the fsockopen fails to open a socket. The domain cannot be checked, but currently the domain comes back as registered rather than as an error.

Here are places I've made a few changes to add error checking. Hopefully people can follow this. I've used the scripts since, but mostly the multi_whois_list, so there could be some errors introduced with other ways of checking the domains.

I hope this is useful!

David.
----------------------------------------------------
In the whois_class.php, at top of class, add:

Code:
var $success = false;

In the whois_class.php, function get_whois_data (indentation also changed in this copy, but that's not important. Also, I used the socked all the time, not just on Windows, but again that doesn't affect this change):

Code:
$connection = fsockopen($this->whois_server, 43, $errno, $errstr);

if (!$connection) {
  unset($connection);
  $this->success = false;
  $this->msg = "Can't connect to the server!";
  return;
} else {
  sleep(2);
  fputs($connection, $this->whois_param.$this->compl_domain."\r\n");

  $buffer = array();
  while (!feof($connection)) {
    $buffer[] = fgets($connection, 4096);
  }
  fclose($connection);
}

if (count($buffer) > 0) {
  //print_r($buffer);
  $this->success = true;
  return $buffer;
} else {
  $this->success = false;
  $this->msg = "Can't retrieve data from the server!";
}

In the whois_class.php, function check_only:

Code:
function check_only() {
  //Return values 1 - not registered, 0, already registered, -1 connection error.

$data = $this->get_whois_data();
if (is_array($data) && $this->success) {
foreach ($data as $val) {
if (eregi($this->free_string, $val)) {
return 1;
}
}
return 0;
} else {
$this->msg = "Error, please try it again.";
return -1;
}
}

In the whois_class.php, function process:

Code:
function process() {
if ($this->create_domain()) {
if ($this->full_info == "yes") {
$this->get_domain_info();
} else {
                                $result = $this->check_only();
if ($result  == 1) {
$this->msg = "The domain name: <b>".$this->compl_domain."</b> is free.";
return true;
} elseif ($result  == 0) {
$this->msg = "The domain name: <b>".$this->compl_domain."</b> is registered";
return false;
} else {
$this->msg = "There was something wrong, try it again.";
}
}
} else {
$this->msg = "Only letters, numbers and hyphens (-) are valid!";
}
}

In multi_whois_list:

Code:
function create_dom_list() {
if ($this->create_domain()) {
                        $result = $this->check_only();
if ($result == 1) {
$this->dom_str .= "<b>".$this->compl_domain."</b> is free.<br>\n";
} elseif ($result == 0) {
$this->dom_str .= "<b>".$this->compl_domain."</b> is registered";
$this->dom_str .= " - <a href=\"multi_whois_detail.php?det=".$this->compl_domain."\" target=\"_blank\">Detail</a><br>\n";
}else{
        $this->dom_str .= "<b>".$this->compl_domain."</b> Error checking this domain. Please try it again.<br>\n";
}
} else {
$this->msg = "Only letters, numbers and hyphens (-) are valid!";
}
}


« Last Edit: Oct 26, 2006, 01:18:50 AM by difdif »
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6357
38966 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Oct 26, 2006, 01:11:40 AM »

Thanks for your suggestions to make this class better!

It's funny I got some function today from Nick how to parse whois results for browser only whois and it looks like a good idea to combine both features to make from them a rock solid class (you're right I doesn't like the other whois scripts, they are the reason that I wrote this class)


Last blog : Upload images for usage in TinyMCE
Google dot what?
*
Posts: 2
16 credits
Members referred : 0



« Reply #2 on: Oct 26, 2006, 01:28:13 AM »

Hi olaf,

I've made a couple of changes by editing my original code posting above - there was a mistake, and a way to increase efficiency by calling $this->check_only() fewer times.

A couple of other ideas I have found useful:

1) Using non-blocking mode for the fsockopen, to speed things up if a server is busy or down. Also a timeout of 10 seconds on the fsockopen.

Code:
    $connection = fsockopen($this->whois_server, 43, $errno, $errstr, 10);

    if (!$connection) {
      unset($connection);
      $this->success = false;
      $this->msg = "Can't connect to the server!";
      return;
    } else {
      sleep(2);
      //Use a non-blocking connection to avoid massive script hangs
      stream_set_blocking ($connection, 0);
      $stop = time() + 10; //10 second timeout on each connection
     
      fputs($connection, $this->whois_param.$this->compl_domain."\r\n");
     
      $buffer = array();
      while (!feof($connection) && time() < $stop ) {
$result = trim(fgets($connection, 1024));
if($result != ''){
  $buffer[] = $result;
}
      }
      //Uncomment for error checking info on the stream
      //print_r(stream_get_meta_data ($connection));
     
      fclose($connection);
    }

2) I think the code for show_tlds() can be improved. Here is my suggestion:

  function show_tlds() {
    //Show a comma separated list of top level domains considered.
    $tlds = implode(', ', $this->possible_tlds);
    return $tlds;
  }


David.
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=4558
Tags : domains whois browsers Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  PHP classes @finalwebsites.com  >  PHP Whois script (Moderator: Olaf)
Topic: More error checking in the fsockopen stage
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Oct 15, 2008, 10:17:17 PM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!





Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.