PHP Word Cloud Generator


Code

<?php
/*
* PHP relay chat daemon -
* To run the daemon in the background from the command line
use `/usr/bin/php server.php > /dev/null 2>&1 & disown` (or similar)
*/
if (strtolower(PHP_SAPI) !== 'cli') exit;    // The daemon must only be allowed to run in CLI
set_time_limit(0);        // Prevent time limit
echo "Daemon starting...\r\n";
/*
* Initialize global variables
*/
$master = array();
$users = array();
$connections NULL;
$serveraddress 'sheriframadan.com'# Enter server address or IP here
$serverport 10000# Port to listen on
$serveruri 'tcp://' $serveraddress ':' $serverport;
$tv_sec 0;
$tv_usec 200000# 200,000 microseconds is the time out on each stream (200ms or 0.2s)
$write = array();
$exceptions = array();
$blocking_mode 0# 0 for non-blocking streams and 1 for blocking streams
$usertimeout 60*15# default user stream timeout in seconds [default should be 15 minutes]
$peername NULL# This will be passed by reference so only use to assign upon new connections
$packetsize 1024# Default packet size is set to 1024 bytes
/*
* Initialize listening socket
*/
if (!$socket stream_socket_server($serveruri$errno$errstr)) {
// Catch startup errors here
echo "Error! Unable to start the daemon on [$serveraddress] using port ($serverport).\r\nError #: $errno\r\nError: $errstr\r\n";
exit;
}
stream_set_blocking($socket$blocking_mode); # Set the stream to non-blocking
echo "Server connected on [$serveraddress] using port ($serverport)!\r\n\r\n";
/*
* Main loop
*/
$master[] = $socket// Add the servers connection to the listening socket in the master array
$connections $master;// We copy the master array for now to avoid modifying it 
//with stream_select as it's arrays are passed by reference
while (true) {
$connections $master# Make sure we have a fresh copy of the master array in each loop
$numconnections stream_select($connections$write$exceptions$tv_sec$tv_usec);
// If an error has been returned by the call to stream_select() we must catch it here
if ($numconnections === FALSE) {
echo "An error occurred capturing the streams.\r\nUnbale to continue work: shutting down...\r\n\r\n";
break;
}
// This is where we read/write to all existing streams and accept new connections
for ($i 0$i $numconnections$i ++) {
// The connection is made to the listening socket
if ($connections[$i] === $socket) {
$newconnection stream_socket_accept($socket$usertimeout$peername);
$master[] = $newconnection# Add the new connection to the master array
$users[$peername] = $newconnection# Define the users peername in the users array
echo "- New connection detected from [$peername]\r\n\r\n";
stream_set_blocking($newconnection$blocking_mode); 
# define the blocking mode for this new connection
if ($socket_data fread($newconnection$packetsize)) { // Connection sent data...
$socket_data_bytes strlen($socket_data);
echo "* Read $socket_data_bytes bytes from [{$peername}]\r\n--- BEGIN MESSAGE --
-\r\n$socket_data\r\n--- END MESSAGE ---\r\n\r\n";
unset($socket_data,$socket_data_bytes,$newconnection);
}
// Write welcome message here
list($ip$port) = explode(':'$peername);
fwrite($newconnection"Welcome to the PHP server!\r\nI see you are connecting to me using 
$serveraddress on port $serverport from IP address $ip on port $port.\r\n");
unset($ip$port);
}
else { // Continue reading from sockets here.
$socket_data fread($connections[$i], $packetsize);
$socket_data_bytes strlen($socket_data);
$peer array_search($connections[$i], $usersTRUE);
$stream array_search($connections[$i], $masterTRUE);
if ($socket_data_bytes === 0) { // The client probably closed the connection
echo "* Client on [$peer] closed the connection.\r\n- Terminating stream...\r\n\r\n";
fclose($connections[$i]); # Close the client connection
unset($master[$stream],$users[$peer]); # unset variables and continue
}
elseif ($socket_data === FALSE) { // Tere's a problem reading from the socket
echo "- There was a problem reading from [$peer]...\r\n* Closing client connection!\r\n\r\n";
fclose($connections[$i]); # Close the client connection
unset($master[$stream],$users[$peer]); # unset variables and continue
}
else {
$numusers count($users);
echo "* Read $socket_data_bytes bytes from [$peer]\r\n--- BEGIN MESSAGE --
-\r\n$socket_data\r\n--- END MESSAGE ---\r\n\r\n";
// Send the message to all users still connected to the server
foreach ($users as $user) {
fwrite($user"* [$peer] said: $socket_data\r\n");
}
echo "* Relayed message to $numusers user(s).\r\n\r\n";
unset($user,$numusers);
}
unset($peer$stream$socket_data$socket_data_bytes);
}
}
}
/*
* Handle anything after main loop breaks here
*/
echo "The daemon has been stopped!\r\n\r\n";
?>

Example Usage

To start the daemon from the command line use nohop or disown to background the proccess. Also don't forget to send STDOUT to /dev/null or some designated logging file along with redirecting STDERR to STDOUT.
You can use something like `php server.php > /dev/null 2>&1 & disown` or similarly `nohup php server.php > /dev/null 2>&1 &`
You can test connecting to the server by opening a terminal and using telnet. Example: `telnet <server address> <server port>`
Once you telnet in you should see the welcome message and anything you type after that will be relayed to anyone else connected after you hit enter.
Please consider that this code is meant for demonstration purposes only. There are no constraints in place to limit the number of connections the server will accept or any mechanisms in place to prevent any form of Direct denial of Services attack. So do implement your own methods and security considerations as this code is not production ready. There is also no protocol in place for a handshake or user sign-on. The server will accept any connection it receives on the designated port and continue relaying messages.
Share this article :
 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. PHP MySQL Online Training - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger