With our last article on debugging PHP with UDP sockets we created a new class able to send debugging information from PHP to a remote netcat client. The limit of the code was that it could only send strings and not arrays or an object. Today we will modify the code to send arrays to our remote listening client.

First, I will show you what happens if we try to send an array using the UDPDEBUG class that we currently have. To do this we will create a very simple array and then attempt to send it.

 php (udptest2.phps) download
<?
	$ThisWeek = array(1 => 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',  'Saturday', 'Sunday');

	require_once("UDPDEBUG_CLASS.php");
	$UDPDEBUG = new UDPDEBUG("127.0.0.1", 4545);
	$UDPDEBUG->SEND($ThisWeek, 'LOW', TRUE);
?>

    

This test code results in no useful information other then the fact we are dealing with an array.

What we really want is to output the array in much the way print_f does; nice and formatted for easy viewing. To do this we are going to modify our send function from our UDP class and add in some array checking.

Here is our send function before and after the edit we are going to make:

 php (udpdebug_send_function_before.phps) download
                function SEND($STRING, $LEVEL = 1, $VERBOSE = FALSE)
                {
                        $LEVEL = $this->LEVEL[$LEVEL];
                        if($VERBOSE == TRUE){
                                $OUTPUT = sprintf("[%s]:%s:%s%s\n",$this->ESC_CHR.$this->ANSI_FG['CYAN'].$this->PORT,$this->ESC_CHR.$this->ANSI_FG[$this->LEVEL_COLORS[$LEVEL]].str_pad($this->LEVEL_WORDS[$LEVEL], 9),$this->ESC_CHR.$this->ANSI_SPECIAL['NORMAL'], $STRING);
                        }else{
                                $OUTPUT = sprintf("%s\n",$this->ESC_CHR.$this->ANSI_FG[$this->LEVEL_COLORS[$LEVEL]].$STRING.$this->ESC_CHR.$this->ANSI_SPECIAL['NORMAL']);
                        }
                        $this->UPD_OBJECT = fsockopen("udp://" . $this->IP_ADDRESS, $this->PORT, $this->ERR_NO, $this->ERR_STR);
                        if (!$this->UPD_OBJECT) {
                                echo "ERROR IN UDP SYSTEM: $this->ERR_NO - $this->ERR_STR<br />\n";
                                return 0;
                        }
                        fwrite($this->UPD_OBJECT, $OUTPUT);
                        fclose($this->UPD_OBJECT);
                }

    
 php (udpdebug_send_function_after.phps) download
                function SEND($STRING, $LEVEL = 1, $VERBOSE = FALSE)
                {
                        $LEVEL = $this->LEVEL[$LEVEL];
			if(is_array($STRING))
			{
				$STRING = print_r($STRING, true);
			}
                        if($VERBOSE == TRUE){
                                $OUTPUT = sprintf("[%s]:%s:%s%s\n",$this->ESC_CHR.$this->ANSI_FG['CYAN'].$this->PORT,$this->ESC_CHR.$this->ANSI_FG[$this->LEVEL_COLORS[$LEVEL]].str_pad($this->LEVEL_WORDS[$LEVEL], 9),$this->ESC_CHR.$this->ANSI_SPECIAL['NORMAL'], $STRING);
                        }else{
                                $OUTPUT = sprintf("%s\n",$this->ESC_CHR.$this->ANSI_FG[$this->LEVEL_COLORS[$LEVEL]].$STRING.$this->ESC_CHR.$this->ANSI_SPECIAL['NORMAL']);
                        }
                        $this->UPD_OBJECT = fsockopen("udp://" . $this->IP_ADDRESS, $this->PORT, $this->ERR_NO, $this->ERR_STR);
                        if (!$this->UPD_OBJECT) {
                                echo "ERROR IN UDP SYSTEM: $this->ERR_NO - $this->ERR_STR<br />\n";
                                return 0;
                        }
                        fwrite($this->UPD_OBJECT, $OUTPUT);
                        fclose($this->UPD_OBJECT);
                }


    

As you can see, what we added was a simple if clause that will redefine our $STRING as $STRING = print_r($STRING, true) if our passed item is an array.

With this simple modification our test code above now produces some useful output when the passed object is an array.

This modification will work even with very large and complicated multidimensional arrays.

Till next time, code sexy.
–Terry Valladon

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkArena
  • LinkedIn
  • Technorati
  • Tumblr
  • TwitThis
  • StumbleUpon

Technorati Tags: , , , , , ,

Leave a Reply

Spam protection by WP Captcha-Free