Friday, April 4, 2008

Ban IP function

/* ==================================
ban ip function by http://php-busy.blogspot.com
================================== */
$iplist[] = "192.111.111.111";
$iplist[] = "10.1.1.33";
$iplist[] = "50.165.12.3"
$iplist[] = "192.168.1.23";

// Get IP
if ($_SERVER["HTTP_X_FORWARDED_FOR"])
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; //Get proxy
else
$ip = $_SERVER["REMOTE_ADDR"]; // Get remote address

// loop for check matching between $iplist and $ip
foreach ($iplist as $key =>$val) {
if ($ip==$val)
exit();
}
?>

SQL operation function

/* ==================================
SQL operation function by http://php-busy.blogspot.com
================================== */

// Insert function
function insert($sField,$sValue,$sTable)
{
$sql = "INSERT INTO $sTable ($sField) VALUES ($sValue)";
$result = mysql_query($sql);
return $result;
}

// Delete fucntion
function delete($sTable,$cCondition)
{
$sql ="delete from $sTable $cCondition";
$result = mysql_query($sql);
return $result;
}


// Update function
function update($sTable,$cCommand,$cCondition)
{
$sql = "UPDATE $sTable SET $cCommand $cCondition";
$result = mysql_query($sql);
return $result;
}

// Select function
function select($sTable,$cCondition)
{
$sql = "select * from $sTable $cCondition";
$query = mysql_query($sql);
$result= mysql_fetch_array($query);
return $result;
}

// num rows function
function numRows($sTable,$cCondition)
{
$sql = "select * from $sTable $cCondition";
$query = mysql_query($sql);
$nrows = mysql_num_rows($query);
return $nrows;
}
?>

Wednesday, April 2, 2008

Date difference using MySQL

/* ==================================
Date difference using MySQL by http://php-busy.blogspot.com
================================== */

$link=mysql_connect("localhost","mysqluser","mysqlpassword");

//$dNow=date("Y-m-d");
$dNow="2008-03-08 00-00-00";
$dDate="1986-09-04 00-00-00";

// Query for get number of day between $dNow and $dDate
$dDayDiff=mysql_query("SELECT DATEDIFF('$dNow','$dDate');");
$iResult=mysql_result($dDayDiff,0)-1;
echo "- Day between $dNow and $dDate is ".$iResult." days
"; // display day between $dNow and $dDate


// Initializing for Year difference
$iYearNow=date("Y");
$iYearSplit=split("-",$dDate);
$iYearDate=$iYearSplit[0];
$iYear=$iYearNow-$iYearDate;

// Query for day of all year
for($i=0;$i<=$iYear-1;$i++)
{
$dYearDiff=mysql_query("SELECT DAYOFYEAR('$iYearDate-12-31');"); // Query for day of year
$iYearResult=mysql_result($dYearDiff,0);
$aYear[$i]=$iYearResult;
$iYearDate++;
}

// Count day of year
$iSumDay=$iResult;
for($i=0;$i<=$iYear-1;$i++)
{
if ($iSumDay>=$aYear[$i]) {
$iSumDay=$iSumDay-$aYear[$i];
} else {
$iYear=$i;
break;
}
}
echo "- Date difference is : ".$iYear." years, ".$iSumDay." days
"; // Display result

/* ==================================
I start it's for using to count year only, other difference is you.
================================== */
?>

Saturday, March 29, 2008

Welcome all & readme first

I'm starting a new blog for store all php function that I'm created. I'm not serious that I will public my source. But you should to have manners by do not remove credit comment. My name are appear in that.

Rules of me

1. I'm still define a first character of variable as data type (e.g. integer must be define as i ; iD, iPersonID, iCategoryID )
2. My code arrange with TAB.
3. I don't know who blogspot support automatically fill code color