You are on page 1of 6

Hàm lấy IP chính xác, hiện tại chưa thể phát hiện sock

function getip() {
if (isset($_SERVER)) {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$realip = $_SERVER['HTTP_CLIENT_IP'];
} else {
$realip = $_SERVER['REMOTE_ADDR'];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")) {
$realip = getenv( "HTTP_X_FORWARDED_FOR");
} elseif (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}
Hàm lấy random str:
function RandomStr($Length)
{
$Chars = array("a","A","b","B","c","C","d","D","e","E","f","F","g",
"G","h","H","i","I","j","J","k",
"K","l","L","m","M","n","N","o","O","p","P","q","Q",
"r","R","s","S","t","T","u","U","v",
"V","w","W","x","X","y","Y","z","Z","1","2","3","4",
"5","6","7","8","9");
$RandCode = "";
for ($i=0; $i<$Length; $i++) {
$RandCode .= $Chars[rand(0, count($Chars)-1)];
}

return $RandCode;
}
Upload file từ 1 direct URL:
function UploadFromUrl($Url)
{
define("URL_1",$Url);

$UploadDir = "upload/files/";

$FileName = explode("/", $Url);

$FileName = $FileName[count($FileName)-1];

define("URL_2",$UploadDir.$FileName);

$f1 = @fopen ( URL_1, "rb");

$f2 = @fopen ( URL_2, "w");

while ( $Buff = @fread( $f1, 1024 ) )


{
@fwrite($f2, $Buff);
}

@fclose($f1);

@fclose($f2);
}
Dinh dang ngay thang
function FormatDateTime($datetime) {
return date('d/m/Y H:i:s', strtotime($datetime));
}
Chuyển ngày tháng dạng: dd-mm-yyyy -> định dạng ngày tháng để insert vào csdl
rồi ta có thể đọc ra từ hàm date(d-m-Y, $row['date'])
function parseDay($day)
{
$myDay = explode('-',$day);
$dateParse = mktime(0,0,0,$myDay[1],$myDay[0],$myDay[2]);
return $dateParse;
}
Một class nữa có đầy đủ hết các thao tác về file
class FileSystem
{
/**
* Method Download File Content
* @param File Content, File Name, File Type, Extension
*/
public function DownloadFile($FileString, $FileName, $FileType, $Ext)
{
header('Content-Type: ' . $FileType);
header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Content-Disposition: attachment; filename="' . $FileName . "." . $Ext . '"');
header('Content-Length: ' . strlen($FileString));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');

echo $FileString;
exit;
}

/**
* Method Get File Content
* @param str
* @return str
*/
public function GetContent($File)
{
return file_get_contents($File);
}

/**
* Method Write File Content
* @param File path, Write Type, Content
*/
public function WriteFile($File, $Type, $Content)
{
$Handle = @fopen($File,$Type);
fwrite($Handle, $Content);
@fclose($Handle);
}

/**
* Method Read and Show Folder Item
* @param Folder path
* @return array
*/
public function ShowDir($Folder)
{
$ItemArray = array();
if (is_dir($Folder))
{
if ($Handle = @opendir($Folder))
{
while (($Item = readdir($Handle)) !== false)
{
$ItemArray[] = $Item;
}
@closedir($Handle);
}
}

return $ItemArray;
}

/**
* Method Delete File
* @param File path
*/
public function DelFile($File)
{
@unlink($File);
}

/**
* Method Creat Folder
* @param Folder path
*/
public function CreatFolder($Folder,$chmod=0775)
{
@mkdir($Folder,$chmod);
}

/**
* Method Remove Dir
* @param Folder path
*/
public function RemoveDir($Dir)
{
$ItemArray = $this->ShowDir($Dir);
for ($i=0; $iRemoveDir($Dir."/".$ItemArray[$i]);
}
else
{
$this->DelFile($Dir."/".$ItemArray[$i]);
}
}
}

@rmdir($Dir);
}
}
Hàm lấy địa chỉ IP người dùng (copy từ Google)
function getUserIP()
{
if (isset($_SERVER)) {

if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
return $_SERVER["HTTP_X_FORWARDED_FOR"];
}

if (isset($_SERVER["HTTP_CLIENT_IP"])) {
return $_SERVER["HTTP_CLIENT_IP"];
}

return $_SERVER["REMOTE_ADDR"];
}

if (getenv('HTTP_X_FORWARDED_FOR')) {
return getenv('HTTP_X_FORWARDED_FOR');
}

if (getenv('HTTP_CLIENT_IP')) {
return getenv('HTTP_CLIENT_IP');
}

return getenv('REMOTE_ADDR');
}
Hàm loại bỏ toàn bộ khoảng trắng
function removeSpaces($str)
{
$str = trim($str);

$str = str_replace("\r\n", " ", $str);


$str = str_replace("\n", " ", $str);
$str = str_replace("\t", " ", $str);

while (strstr($str, " ")) {

$str = str_replace(" ", " ", $str);


}

return $str;
}
Lấy thời gian hiện tại dựa trên giờ GMT
/**
* Lay thoi gian hien tai theo GMT
*
* @param int $offsetHours
* @return int
*/
function getCurrentTime($offsetHours = 0)
{
$time = mktime(gmdate('H'), gmdate('i'), gmdate('s'), gmdate('m'), gmdate('d'),
gmdate('Y')) + ($offsetHours * 60 * 60);

return $time;

You might also like