Sunday, December 14, 2008

Special Character replacement in PHP

$code_entities_match = array('
','--','&quot;','!','@','#','$','%','^','&','*','(',')','_','+','{','}','|',':','"','<','>','?','[',']','\\',';',"'",',','.','/','*','+','~','`','=');

$parent_linkid=str_replace($code_entities_match,"",$parent_linkid);

Thursday, November 13, 2008

Apache SSL Parser

$log='x.x.x.x - - [13/Nov/2008:18:57:54 +0530] "GET
/bigboos2/include/html/menu/menu_tpl.js HTTP/1.1" 304 -
"https://x.x.x.x/bigboos2/dashboard.cgi" "Mozilla/5.0 (X11; U; Linux
i686; en-US; rv:1.8.1.16) Gecko/20080702 SeaMonkey/1.1.11"';


$log=~/(\d+.\d+.\d+.\d+)\s+-\s+(\w+)*[-]*\s+\[\d+\/\w+\/\d+:\d+:\d+:\d+\s+[-+]\d+]\s+"(.*)HTTP\/\d+.\d+"\s+(\d+)\s+[\d+
-]\s+"(.*)"\s+"(\w+)(.*)"/g;

print "IP:$1\n";
print "User:$2\n";
print "Request:$3\n";
print "Code:$4\n";
print "Refer:$5\n";
print "Agent:$6$7\n";

Apache SSL Parser

$log='x.x.x.x - - [13/Nov/2008:18:57:54 +0530] "GET
/bigboos2/include/html/menu/menu_tpl.js HTTP/1.1" 304 -
"https://x.x.x.x/bigboos2/dashboard.cgi" "Mozilla/5.0 (X11; U; Linux
i686; en-US; rv:1.8.1.16) Gecko/20080702 SeaMonkey/1.1.11"';

$log=~/(\d+.\d+.\d+.\d+)\s+-\s+(\w+)*[-]*\s+\[\d+\/\w+\/\d+:\d+:\d+:\d+\s+[-+]\d+]\s+"(.*)HTTP\/\d+.\d+"\s+(\d+)\s+[\d+
-]\s+"(.*)"\s+"(\w+)(.*)"/g;

Friday, July 25, 2008

Hexa to Decimal Conversion in Perl

k1 contains the hexadecimal value . to convert it into decimal we need
to do the following 2 lines coding.

$k1='0015';
$k1=hex($k1);
$dec = sprintf("%d", $k1);

Thursday, July 24, 2008

Three Level Hash in Perl

Here i have tried to put code for the network packet information

in the following pattern.



%ip_port=();

$ip_port{10.1.1.1}{'TCP'}{25}=100;
$ip_port{10.2.1.1}{'TCP'}{23}=10;
$ip_port{10.1.2.1}{'UDP'}{53}=1100;
$ip_port{10.111.1}{'TCP'}{80}=1030;
$ip_port{10.1.1.1}{'UDP'}{53}=1200;
$ip_port{10.1.12.1}{'UDP'}{53}=1050;
$ip_port{10.13.1.1}{'TCP'}{123}=11100;
$ip_port{10.11.1.1}{'TCP'}{443}=10340;


print "<table><tr><td>SouceIP </td><td>Porotocol</td> <td>Port </td><td>Total Packet</td></tr>";

foreach $key(keys %ip_port)
{
       print $key." => ".$ip_port{$key}."\n";

        print "\n".$key." => \n";
                foreach $k1(keys %{$ip_port{$key}})
                {
                       print $k1." => ";

                        foreach $k2(keys  %{$ip_port{$key}{$k1}})
                        {

                               print $k2." => ";

                                $value=$ip_port1{$key}{$k1}{$k2};
                        print "<tr><td>$key</td><td>$k1</td><td>$k2</td><td>$value</td></tr>";
                        }

                }
}
print "</table>";


Example Output

Source IP Protocol Port Total Paclets
x.x.x.x
TCP 491 2
x.x.x.x TCP 25 13
x.x.x.x TCP 25 7
x.x.x.x ICMP 0 3
x.x.x.x ICMP 771 1
x.x.x.x
TCP 25 12
x.x.x.x
ICMP 0 2

Thursday, June 19, 2008

GET / POST in PHP

If you are not sure whether any form submission which is passed to a php is GET or POST , you can try the following line to work in both condition.


----------------------------------------------------------------------------------------------------------------------
$client_id = $_POST['client_id'] ? $_POST['client_id'] : $_GET['client_id'];
----------------------------------------------------------------------------------------------------------------------

Monday, June 16, 2008

Remote User IP Detection in PHP

This function returns the IP of the Remote machine.

function getip()
{

if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"),
"unknown"))

$ip = getenv("HTTP_CLIENT_IP");

else if (getenv("HTTP_X_FORWARDED_FOR") &&
strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))

$ip = getenv("HTTP_X_FORWARDED_FOR");

else if (getenv("REMOTE_ADDR") &&
strcasecmp(getenv("REMOTE_ADDR"), "unknown"))

$ip = getenv("REMOTE_ADDR");

else if (isset($_SERVER['REMOTE_ADDR']) &&
$_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))

$ip = $_SERVER['REMOTE_ADDR'];

else

$ip = "unknown";
return($ip);
}

Numeric value validation in php

Numeric value validation in php

This function return 1 if the argument passed to it is numeric only.


function onlynumber($value)  {
    $american = preg_match ("/^(-){0,1}([0-9]+)(,[0-9][0-9][0-9])*([.][0-9]){0,1}([0-9]*)$/" ,$value) == 1;
    $world = preg_match ("/^(-){0,1}([0-9]+)(.[0-9][0-9][0-9])*([,][0-9]){0,1}([0-9]*)$/" ,$value) == 1;
   return ($american or $world);
}


Trim Function in PHP

This function removes all special characters - \t\n\r\0\x0B?#!@%^&*()\"',.\/;:><[]{}+=-_|`~

and returns the new one.


function trimall($str, $charlist = " \t\n\r\0\x0B?#!@%^&*()\"',.\/;:><[]{}+=-_|`~")
{
 $str=str_replace("$",'',$str);
  return str_replace(str_split($charlist), '', $str);

}