note 60062 deleted from function.fsockopen by danbrown
| From: | danbrown@php.net | Date: | Sun, 23 Jan 2011 16:52:35 +0000 |
| Subject: | note 60062 deleted from function.fsockopen by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176129@lists.php.net to get a copy of this message | ||
Note Submitter: SysCo/al - developer [at] sysco[dot] ch
----
We have implemented a Syslog class in PHP following the RFC 3164 rules. Using this class, it is
possible to send syslog messages to external servers.
We use this class for example to log information, to synchronize some processes or to launch
external "threads".
Class abstract, full class implementation can be found at http://developer.sysco.ch/php/
<?php
// (...)
class Syslog
{
var $_facility; // 0-23
var $_severity; // 0-7
var $_hostname; // no embedded space, no domain name, only a-z A-Z 0-9 and other authorized
characters
var $_fqdn;
var $_ip_from;
var $_process;
var $_content;
var $_msg;
var $_server; // Syslog destination server
var $_port; // Standard syslog port is 514
var $_timeout; // Timeout of the UDP connection (in seconds)
// (...)
function Send($server = "", $content = "", $timeout = 0)
{
// (...)
$actualtime = time();
$month = date("M", $actualtime);
$day = substr(" ".date("j", $actualtime), -2);
$hhmmss = date("H:i:s", $actualtime);
$timestamp = $month." ".$day." ".$hhmmss;
$pri = "<".($this->_facility*8 +
$this->_severity).">";
$header = $timestamp." ".$this->_hostname;
// (...)
$msg = $this->_process.": ".$this->_fqdn."
".$this->_ip_from." ".$this->_content;
$message = substr($pri.$header." ".$msg, 0, 1024);
$fp = fsockopen("udp://".$this->_server, $this->_port, $errno, $errstr,
$this->_timeout);
if ($fp)
{
fwrite($fp, $message);
fclose($fp);
$result = $message;
}
else
{
$result = "ERROR: $errno - $errstr";
}
return $result;
}
// (...)
?>
Example
<?php
require_once('syslog.php');
$syslog = new Syslog();
$syslog->Send('192.168.0.12', 'My first PHP syslog message');
?>