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);

}