Jump to content

Favorite Sites Check Server Script


fryhole329

Recommended Posts

I have written a small script for my own personal use that checks the servers of my favorite sites to see if they are operational.  I'm having trouble getting it to work, I have most of it (I think) and I'm very new to PHP. The script also allows for a form to add any sites to the file, which I think I explode into an array correctly, at a later time.  Here's my code, Please Help!

 

<html>

<head>

<title>project.php</title>

</head>

<body>

<?php

$filename = "websites.txt";

$file = file_get_contents($filename); // reads entire file into a string

$DomainNames = explode("\n", $file); // split the string at new line characters, returns an array

$length = count($DomainNames) - 1; // count is the correct way to get the length of an array

$i=0;

while($i<=$length){

$fp = @fsockopen ($DomainNames[$i], 80, $errno, $errstr, 10);

if (!$fp)

echo "$DomainNames[$i] : <font color='red'>Failed!</font>";

else {

echo "$DomainNames[$i] : <font color='green'>Online!</font>";

echo "<br>";

$i++;

}

}

if (isset($_POST['submit'])) {

  $DomainNames[] = $_POST['url']; // append url to array

  $file = implode("\n", $DomainNames); // convert array to string, delimiting by new lines

  file_put_contents($filename, $file); // save contents to file

}

?>

 

 

<form action="project.php" method="post">

  <fieldset>

      <label for="url">Url: </label>

      <input type="text" name="url" id="url" />

      <input type="submit" name="submit" id="submit" value="Add URL" />

  </fieldset>

</form>

</body>

</html>

Link to comment
Share on other sites

Okay, I found some errors myself (stupid stuff) now my code looks like this:

<html>

<head>

<title>project.php</title>

</head>

<body>

<?php

$filename = "websites.txt";

$file = file_get_contents($filename); // reads entire file into a string

$DomainNames = explode("\n", $file); // split the string at new line characters, returns an array

$length = sizeof($DomainNames) - 1; // count is the correct way to get the length of an array

$i=0;

while($i<=$length){

$fp = @fsockopen ($DomainNames[$i], 80, $errno, $errstr, 10);

if ($fp)

echo "$DomainNames[$i] : <font color='red'>Failed!</font>";

else

echo "$DomainNames[$i] : <font color='green'>Online!</font>";

echo "<br>";

$i++;

}

if (isset($_POST['submit'])) {

  $DomainNames[] = $_POST['url']; // append url to array

  $file = implode("\n", $DomainNames); // convert array to string, delimiting by new lines

  file_put_contents($filename, $file); // save contents to file

}

?>

 

 

<form action="project.php" method="post">

  <fieldset>

      <label for="url">Url: </label>

      <input type="text" name="url" id="url" />

      <input type="submit" name="submit" id="submit" value="Add URL" />

  </fieldset>

</form>

</body>

</html>

 

The problem now is that when it writes to "websites.txt" it's adding it to the end of the last line instead of a new line. Anyone know how to fix this? Also, it's only returning the first two URLs as running, when the other's in the file are clearly running as well. Instead I'm getting the "Failed" option popping up. Any suggestions?

 

[attachment deleted by admin]

Link to comment
Share on other sites

Here's something that can read a text file, check all the urls, and place them back into text file.

 

You will see i connect different.

I placed the alive/dead in text separated with a |

For lines that are merging, see the comment for when data is getting inserted for extra \n

 

<?php
//read the text file
$my_file = "check-alive-urls.txt"; //name your url text file here
//see if exists
if (file_exists($my_file)) {
//contents of text file
$data = file($my_file);
//total lines
$total = count($data);
echo "<br />Total lines: $total<br />";
//loop the data by line
foreach ($data as $line) {
//skip empty lines
if($line != ""){
//explode lines to get each value
$explode_lines = explode("|",$line);
$url = trim($explode_lines[0]);
$status = trim($explode_lines[1]);
//making sure any url has the http protocol
if(substr($url,0,5) != "http:"){
$url = "http://$url";
}
//see how long it takes to connect
$time_start = microtime(1);
//is best to use a stream context for more options
$context = stream_context_create(array(
    'http' => array(
        'timeout' => 3
    )
));
//get the content from url
$the_contents = @file_get_contents($url, 0, $context);
echo "<hr>";
//alive or dead condition
if (empty($the_contents)) {
$status = "dead";
} else {
$status = "alive";
}

//define colors to status and unchecked value
if($status == "dead") {
$color = "#FF0000";
} elseif($status == "alive") {
$color = "#00FF00";
} else {
$status = "unchecked";
$color = "#C0C0C0";
}

//show results on page
echo "<p style='color: $color;'>$status - <a href='$url'>$url</a></p>";
//create array of urls and status for text file
$save_urls[] = "$url|$status";
//end of connect time
$time_end = microtime(1); 
$time_elapsed = $time_end - $time_start; 
echo printf("<br />%f seconds - ", $time_elapsed);
echo "</hr>";
}
}
//prep to save to text file
$save_to_file = implode($save_urls,"\n");

//first option appends a file, second option overwrites file
//uncomment and comment them for which you need

/*
//add new lines to end of text file
$write = fopen($my_file, 'a+');
$message = $save_to_file;
fputs($write, "$message\n"); // that extra \n adds and extra line for next data input
fclose($write);
*/

//note the w, this will overwrite the entire contents of text file
$write = fopen($my_file, 'w');
$message = $save_to_file;
fputs($write, "$message\n");  // that extra \n adds and extra line for next data input
fclose($write);


} else {
echo "No file to display";
}
?>

 

 

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.