The most commonly used custom function collation in PHP project development

  • 2020-03-31 21:25:44
  • OfStack

< ? PHP
/ / alert prompts
The function alert ($MSG) {
Echo "< Script> Alert (' $MSG); < / script>" ;
}
// converts some predefined characters into HTML entities
The function d_htmlspecialchars ($string) {
If (is_array ($string)) {
The foreach ($string as $key = > {$val)
$string [$key] = d_htmlspecialchars ($val);
}
} else {
$string = str_replace (' & ', '& amp; '$string);
$string = str_replace (' "', '& quot; '$string);
$string = str_replace (" ', 'the & # 039; '$string);
$string = str_replace (' < ', '< '$string);
$string = str_replace (' > ', '> '$string);
$string = preg_replace (' / & amp; (# \ d) / ', '& \' 1 ', $string);
}
Return $string;
}
// use backslashes before predefined characters, including single quotes, double quotes, backslashes, and NULL to protect the database
Function d_addslashes($string, $force = 0) {
If (! $GLOBALS [' magic_quotes_gpc '] | | ${force)
If (is_array ($string)) {
The foreach ($string as $key = > $val) $string[$key] = d_addslashes($val, $force);
}
The else $string = addslashes ($string);
}
Return $string;
}
// generates a random string containing uppercase, lowercase letters, and Numbers
The function randstr ($length) {
$hash = ' ';
= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz' $chars;
$Max = strlen($chars) -1;
Mt_srand ((double) microtime () * 1000000);
For ($I = 0; $I < $length; ${i++)
$hash. = $chars [mt_rand (0, $Max)];
}
Return $hash;
}
// convert the timestamp to the commonly used date format
The function trans_time ($timestamp) {
If ($timestamp < 1) echo 'invalid Unix timestamp ';
The else return date (" Y -m - d H: I: s ", $timestamp);
}
/ / get the IP
The function get_ip () {
If ($_SERVER [" HTTP_X_FORWARDED_FOR "])
IP = $$_SERVER [" HTTP_X_FORWARDED_FOR "].
Else if ($_SERVER [" HTTP_CLIENT_IP "])
IP = $$_SERVER [" HTTP_CLIENT_IP "].
Else if ($_SERVER [" REMOTE_ADDR "])
IP = $$_SERVER [" REMOTE_ADDR "].
Else if (getenv (" HTTP_X_FORWARDED_FOR "))
$IP = getenv (" HTTP_X_FORWARDED_FOR ");
Else if (getenv (" HTTP_CLIENT_IP "))
$IP = getenv (" HTTP_CLIENT_IP ");
Else if (getenv (" REMOTE_ADDR "))
$IP = getenv (" REMOTE_ADDR ");
The else
$IP = "Unknown";
Return $IP;
}
// calculate time difference: the default return type is "minutes"
//$old_time can only be timestamp, $return_type h is hour, s is second
The function timelag ($old_time, $return_type = 'm') {
If ($old_time < 1) {
Echo 'invalid Unix timestamp ';
} else {
The switch ($return_type) {
Case 'h' :
$type = 3600; Break;
Case 'm' :
$type = 60; Break;
Case 's' :
The $type = 1; Break;
Case ":
$type = 60; Break;
}
$dif = round((time()-$old_time)/$type);
Return $dif;
}
}
// gets the URL of the current page
The function url_this () {
$url = "http://". $_SERVER [r]. "HTTP_HOST" $_SERVER [" REQUEST_URI "];
$return_url = "< A href = '$url > $url< / a>" ;
Return $return_url;
}
// jump function
Function url_redirect ($url, $delay = ' ') {
If ($delay = = ' ') {
Echo "< Script> Window. The location. Href = '$url < / script>" ;
} else {
Echo "< Meta HTTP - equiv = 'refresh' content = '$delay; URL = $URL '/ >" ;
}
}
} / / end func

? >

Related articles: