Jump to content

In search of some tutorials/information


Alendar

Recommended Posts

Please forgive me if I am being lazy or if I did not find what I was looking for but I am looking for the following:1) How does one make an image using PHP?2) How does one ping (find out if a server is running) using PHP?Thank you,Alendar

Link to comment
Share on other sites

Well, you could use get_headers() on the server's address to see whether there is anything there.
The "anything" part Synook refers to is the "X-Powered-By: PHP/*.*.*" header, where each "*" in the value represents the version number. Note that this can be hidden in the server configuration (or in php.ini's "expose_php" directive to be more precise), so if you don't see the X-Powered-By" header, that doesn't guarantee the site is not using PHP.
Link to comment
Share on other sites

I wrote myself a script a while ago to check if various servers are online and email my phone if anything is down.

<?php$email = $_GET['email'];$nooutput = $_GET['nooutput'];$page_mode = $_POST['page_mode'];if ($_POST['idle_time'] != "" && is_numeric($_POST['idle_time'])){  $time_limit = time() + ($_POST['idle_time'] * 60);  $fp = @fopen("/usr/local/apache/htdocs/servertest/time.txt", "w");  if ($fp)  {	@fwrite($fp, $time_limit);  }  @fclose($fp);}if ($page_mode == "test"){  $email_str = "This is a test message from server.domain.com";  mail("6025555555@tmomail.net", "Test", $email_str . "\n\nSent as a test");  mail("6235555555@vtext.com", "Test", $email_str . "\n\Sent as a test");}if ($nooutput == ""){  echo "<html><head><title>Server Status</title><meta http-equiv=\"Expires\" content=\"Sat, 01 Jan 2000 12:00:00 GMT\"></head>";  echo "<body style=\"font-family: verdana, sans-serif; font-size: 14px;\">";  echo "<span style=\"font-size: 10px;\">Executing <b>" . $_SERVER["SCRIPT_FILENAME"] . "</b> on <b>" . $_SERVER["HTTP_HOST"] . "</b>:</span><br /><br /><br />";  echo "<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\">";  echo "<tr style=\"background-color: #F0F0F0;\"><td><b>Host</b></td><td><b>Port</b></td><td><b>Status</b></td></tr>";}$errno = "";$errstr = "";$email_str = "";include("server_list.php");for ($i = 0; $i < count($server_list); $i++){  if ($nooutput == "")	echo "<tr height=\"5\"><td colspan=\"3\" style=\"background-color: #C0C0C0;\"></td></tr>";  $conn = @fsockopen ($server_list[$i]['hostname'], $server_list[$i]['port'], $errno, $errstr, 10);  if ($nooutput == "")	echo "<tr><td valign=\"top\">{$server_list[$i]['hostname']}</td><td valign=\"top\">{$server_list[$i]['port']}</td><td>";  if (!$conn)  {	if ($email != "")	  $email_str .= "{$server_list[$i]['hostname']} ({$server_list[$i]['ip']}) port {$server_list[$i]['port']}, offline by hostname\n";	if ($nooutput == "")	  echo "<span style=\"color: #CC0000; font-weight: bold;\">offline</span><br />Error code is '$errno'<br />Error message is '$errstr'";	$errno = "";	$errstr = "";  }  else  {	if ($nooutput == "")	  echo "<span style=\"color: #00CC00; font-weight: bold;\">online</span>";	fclose($conn);  }  if ($nooutput == "")	echo "</td></tr>";  $conn = @fsockopen ($server_list[$i]['ip'], $server_list[$i]['port'], $errno, $errstr, 10);  if ($nooutput == "")	echo "<tr><td valign=\"top\">{$server_list[$i]['ip']}</td><td valign=\"top\">{$server_list[$i]['port']}</td><td>";  if (!$conn)  {	if ($email != "")	  $email_str .= "{$server_list[$i]['hostname']} ({$server_list[$i]['ip']}) port {$server_list[$i]['port']} offline by IP\n";	if ($nooutput == "")	  echo "<span style=\"color: #CC0000; font-weight: bold;\">offline</span><br />Error code is '$errno'<br />Error message is '$errstr'";	$errno = "";	$errstr = "";  }  else  {	if ($nooutput == "")	  echo "<span style=\"color: #00CC00; font-weight: bold;\">online</span>";	fclose($conn);  }  if ($nooutput == "")	echo "</td></tr>";}$send_mail = false;$fp = @fopen("/usr/local/apache/htdocs/servertest/time.txt", "r");if ($fp){  $timest = trim(@fread($fp, filesize("/usr/local/apache/htdocs/servertest/time.txt")));  if (is_numeric($timest))  {	if (time() >= $timest)	  $send_mail = true;  }  else  {	$send_mail = true;  }}else{  if ($nooutput == "")  {	echo "Could not open time file for checking.";  }  $email_str .= "The timestamp file could not be opened.\n";  $send_mail = true;}if ($email_str != "" && $send_mail){  mail("6025555555@tmomail.net", "Offline", $email_str . "\n\nSent via the web");  mail("6235555555@vtext.com", "Offline", $email_str . "\n\Sent via the web");}@fclose($fp);if ($nooutput == ""){  echo "</table>";  echo "<br /><form action=\"index.php\" method=\"post\">";  echo "Suppress notifications for the next <input type=\"text\" name=\"idle_time\" size=\"2\" value=\"0\" /> minutes. <input type=\"submit\" value=\">\" />";  echo "</form>";  echo "<br />";  echo "<form action=\"index.php?email={$email}\" method=\"post\">";  echo "<input type=\"hidden\" name=\"page_mode\" value=\"test\" />";  echo "<input type=\"submit\" value=\"Send Test Message\" />";  echo "</form>";  if ($timest != "")	echo "<br />No notifications will be sent before " . strftime("%a %b %d, %Y %H:%M:%S %Z", trim($timest));  echo "<br />Current server time is " . strftime("%a %b %d, %Y %H:%M:%S %Z");  if ($send_mail)	echo "<br /><br />Mail is OK to send";  else	echo "<br /><br />Mail is not OK to send";  echo "</body></html>";}?>

That file uses sockets to check if the server is available using both the host name and IP on whichever port you want. The server_list.php file that it includes looks like this:

<?php$server_list = array();$nr = 0;$server_list[$nr]['hostname'] = "domain.com";$server_list[$nr]['ip'] = "64.233.167.99";$server_list[$nr]['port'] = "21"; //ftp $nr++;$server_list[$nr]['hostname'] = "mailbox.domain.com";$server_list[$nr]['ip'] = "64.233.187.99";$server_list[$nr]['port'] = "25"; //smtp$nr++;$server_list[$nr]['hostname'] = "domain.com";$server_list[$nr]['ip'] = "64.233.167.99";$server_list[$nr]['port'] = "80"; //http$nr++;?>

The time.txt file is just a text file with a timestamp in it. If you want to run that exact script you'll want to change the hardcoded path to time.txt (do a search in the code for "time.txt"), change the server_list file, and change the email addresses that notifications get sent to. If you don't name that script index.php you'll also need to change a few links in the forms. Ask if you have any questions about what it's doing.There's also a shorter command-line version that I run as a cron job:

<?php$errno = "";$errstr = "";$email_str = "";$send_mail = false;$fp = @fopen("/usr/local/apache/htdocs/servertest/time.txt", "r");if ($fp){  $timest = trim(@fread($fp, filesize("/usr/local/apache/htdocs/servertest/time.txt")));  if (is_numeric($timest))  {	if (time() >= intval($timest))	  $send_mail = true;  }  else  {	$email_str .= "The timestamp is not numeric.\n";	$send_mail = true;  }}else{  $email_str .= "The timestamp file could not be opened.\n";  $send_mail = true;}if (!$send_email)  //quit if email wont be sent anyway{  exit();}include("server_list.php");for ($i = 0; $i < count($server_list); $i++){  $conn = @fsockopen ($server_list[$i]['hostname'], $server_list[$i]['port'], $errno, $errstr, 30);  if (!$conn)	$email_str .= "{$server_list[$i]['hostname']} port {$server_list[$i]['port']}\n";  else	fclose($conn);  $conn = @fsockopen ($server_list[$i]['ip'], $server_list[$i]['port'], $errno, $errstr, 30);  if (!$conn)	$email_str .= "{$server_list[$i]['ip']} port {$server_list[$i]['port']}\n";  else	fclose($conn);}if ($email_str != ""){  if ($send_mail)  {	mail("6025555555@tmomail.net", "Offline", $email_str . "\n\nAutomatic"); 	mail("6235555555@vtext.com", "Offline", $email_str . "\n\nAutomatic");  }  @fclose($fp);}?>

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...