Jump to content

[SOLVED] Fill array from text file


boemboem

Recommended Posts

I'm not a coder, but I try to find my way between other mans lines ;)

I had some great help beforre, but I need some more help with filling a array from a text file.

 

I thought I could do it like this here beneath, but it seems it comes in a loop

Can anyone tell me what goes wrong and how I can fix it?

 

<?php

//Fill this in with the list of servers
$array = file('./text.txt');
$servers = implode('', $array);
//$servers = array('./text.txt');
//  'www.google.nl',
  //'www.yahoo.com',
  //'www.klinkklareonzintoch.nl',


// Function to check response time
function pingDomain($domain){
    $starttime = microtime(true);
    $file      = @fsockopen ($domain, 80, $errno, $errstr, 10);
    $stoptime  = microtime(true);
    $status    = 0;

    if (!$file) $status = -1;  // Site is down
    else {
        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<body>
  <table>
<?php    
  foreach($array as $domainbase){
    $status = pingDomain($domainbase);
    if ($status != -1)
      echo "<tr><td><img src=\"images/status/ok.gif\" /> <b>$domainbase </td><tr>";
    else
      echo "<tr><td><img src=\"images/status/dead.gif\" /> <b>$domainbase</td><tr>";
  }
?>
</table>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/140559-solved-fill-array-from-text-file/
Share on other sites

well, it's a server on each line, like:

 

www.coldcharlie.nl

www.google.nl

www.yahoo.nl

 

but there is a tiny problem:

please take a look here: http://www.coldcharlie.nl/beta1/multi.php

 

only the 3th domain is offline, it looks like after the first domain the rest is offline.

 

<?php

//Fill this in with the list of servers
//$array = file('./text.txt');
//$servers = implode('', $array);
$array = file_get_contents("./text.txt");
$servers = explode(',',$array);

//$servers = array(
//'www.google.nl',
//'www.yahoo.com',
//'www.klinkklareonzintoch.nl',
//);

// Function to check response time
function pingDomain($domain){
    $starttime = microtime(true);
    $file      = @fsockopen ($domain, 80, $errno, $errstr, 10);
    $stoptime  = microtime(true);
    $status    = 0;

    if (!$file) $status = -1;  // Site is down
    else {
        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<body>
  <table>
<?php    
  foreach($servers as $domainbase){
    $status = pingDomain($domainbase);
    if ($status != -1)
      echo "<tr><td><img src=\"images/status/alive.gif\" /> <b>$domainbase </td><tr>";
    else
      echo "<tr><td><img src=\"images/status/dead.gif\" /> <b>$domainbase</td><tr>";
  }
?>
</table>
</body>
</html>

hey again...

 

how is the file organized? new server on each line or separated by commas?

 

new server on each line:

$servers = file('./text.txt');

commas:

$servers = explode(',',file_get_contents('./text.txt'));

 

 

thanks again!, that was the sollution :)

hey again...

 

how is the file organized? new server on each line or separated by commas?

 

new server on each line:

$servers = file('./text.txt');

commas:

$servers = explode(',',file_get_contents('./text.txt'));

 

 

thanks again!, that was the sollution :)

 

IU thought it was the sollution, but it seems to accept every url, even false urls

 

<?php

//Fill this in with the list of servers
$servers = file('./text.txt');
$array = explode(",",$array);


// Function to check response time
function pingDomain($domain){
    $starttime = microtime(true);
    $file      = @fsockopen ($domain, 80, $errno, $errstr, 10);
    $stoptime  = microtime(true);
    $status    = 0;

    if (!$file) $status = -1;  // Site is down
    else {
        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<body>
  <table>
<?php    
  foreach($servers as $domainbase){
    $array = pingDomain($domainbase);
    if ($status != -1)
      echo "<tr><td><img src=\"images/status/alive.gif\" /> <b>$domainbase </td><tr>";
    else
      echo "<tr><td><img src=\"images/status/dead.gif\" /> <b>$domainbase</td><tr>";
  }
?>
</table>
</body>
</html>

 

The text.txt = organised as:

 

www.url.com

www.url2.com

etc

etc

<?php

//Fill this in with the list of servers
$servers = file('./text.txt');
$array = explode(",",$array);


// Function to check response time
function pingDomain($domain){
    $starttime = microtime(true);
    $file      = @fsockopen ($domain, 80, $errno, $errstr, 10);
    $stoptime  = microtime(true);
    $status    = 0;

    if (!$file) $status = -1;  // Site is down
    else {
        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<body>
  <table>
<?php    
  foreach($servers as $domainbase){
    $status = pingDomain($domainbase);
    if ($status != -1)
      echo "<tr><td><img src=\"images/status/alive.gif\" /> <b>$domainbase </td><tr>";
    else
      echo "<tr><td><img src=\"images/status/dead.gif\" /> <b>$domainbase</td><tr>";
  }
?>
</table>
</body>
</html>

what does it say without the @ supression before fsockopen?

 

rhodesa actually posted correct code. you were probably getting false positives before since you were saving the result in $array, rather than in $status.

 

I believe there is a problem with your fsockopen call (never used it myself). When working with code its always best never to use suppression symbols ( @ ), until the script is finished, even then its bad practice to need them, or want them.

Can the problem be that I'm behind a proxy atm? I thought it was server sided so that didnt came to my conclusion in the first place.

 

Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/g35003/coldcharlie.nl/subdomains/keurmerk/multi.php on line 11

Warning: fsockopen() [function.fsockopen]: unable to connect to www.coldcharlie.com :80 (Unknown error) in /var/www/g35003/coldcharlie.nl/subdomains/keurmerk/multi.php on line 11

Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/g35003/coldcharlie.nl/subdomains/keurmerk/multi.php on line 11

Warning: fsockopen() [function.fsockopen]: unable to connect to www.coldcharlie.nl :80 (Unknown error) in /var/www/g35003/coldcharlie.nl/subdomains/keurmerk/multi.php on line 11

Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/g35003/coldcharlie.nl/subdomains/keurmerk/multi.php on line 11

Warning: fsockopen() [function.fsockopen]: unable to connect to www.coldcharlie.com :80 (Unknown error) in /var/www/g35003/coldcharlie.nl/subdomains/keurmerk/multi.php on line 11

Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/g35003/coldcharlie.nl/subdomains/keurmerk/multi.php on line 11

Warning: fsockopen() [function.fsockopen]: unable to connect to www.coldcharlie.nl :80 (Unknown error) in /var/www/g35003/coldcharlie.nl/subdomains/keurmerk/multi.php on line 11

Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/g35003/coldcharlie.nl/subdomains/keurmerk/multi.php on line 11

Warning: fsockopen() [function.fsockopen]: unable to connect to www.coldcharlie.notgood :80 (Unknown error) in /var/www/g35003/coldcharlie.nl/subdomains/keurmerk/multi.php on line 11

Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/g35003/coldcharlie.nl/subdomains/keurmerk/multi.php on line 11

Warning: fsockopen() [function.fsockopen]: unable to connect to www.coldcharlie.omg :80 (Unknown error) in /var/www/g35003/coldcharlie.nl/subdomains/keurmerk/multi.php on line 11

Are you hosting off your own computer? or off a public server?

 

If its a remote server either paid or free you may have to ask the administrator to enable allow_url_fopen, and check the HOSTNAME is set correctly in the configuration.

 

If it is a free or paid remote server i think the only ones who can help you now are the server admins.

 

If you do have access to the servers core configuration files then we can help :).

maybe you have extra whitespace that it doesn't like...i added trim here:

<?php

//Fill this in with the list of servers
$servers = file('./text.txt');
//$array = explode(",",$array); //this line wasn't doing anything


// Function to check response time
function pingDomain($domain){
    $starttime = microtime(true);
    $file      = fsockopen ($domain, 80, $errno, $errstr, 10);
    $stoptime  = microtime(true);
    $status    = 0;

    if (!$file) $status = -1;  // Site is down
    else {
        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<body>
  <table>
<?php    
  foreach($servers as $domainbase){
    $status = pingDomain(trim($domainbase));
    if ($status != -1)
      echo "<tr><td><img src=\"images/status/alive.gif\" /> <b>$domainbase </td><tr>";
    else
      echo "<tr><td><img src=\"images/status/dead.gif\" /> <b>$domainbase</td><tr>";
  }
?>
</table>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

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