The most commonly used custom functions in PHP project development .? Phpalert prompts functionalert ($ msg) {echoscriptalert ($ msg); script;} to convert some predefined characters to the HTML entity functiond_htmlspecialchars ($ string) {if (is_array ( // Alert prompt
Function alert ($ msg ){
Echo "script" alert ('$ msg'); script ";
}
// Convert some predefined characters into HTML objects
Function d_htmlspecialchars ($ string ){
If (is_array ($ string )){
Foreach ($ string as $ key => $ val ){
$ String [$ key] = d_htmlspecialchars ($ val );
}
} Else {
$ String = str_replace ('&', '&', $ string );
$ String = str_replace ('"', '"', $ string );
$ String = str_replace (''', ''', $ string );
$ String = str_replace ('<', '<', $ string );
$ String = str_replace ('>', '>', $ string );
$ String = preg_replace ('/& (# \ d;)/', '& \ 1', $ string );
}
Return $ string;
}
// Add a backslash before a predefined character, including single quotation marks, double quotation marks, backslash, and NULL, to protect database security
Function d_addslashes ($ string, $ force = 0 ){
If (! $ GLOBALS ['Magic _ quotes_gpc '] | $ force ){
If (is_array ($ string )){
Foreach ($ string as $ key => $ val) $ string [$ key] = d_addslashes ($ val, $ force );
}
Else $ string = addslashes ($ string );
}
Return $ string;
}
// Generate a random string that contains uppercase and lowercase letters and numbers
Function randstr ($ length ){
$ Hash = '';
$ Chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxy ';
$ 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 a common date format
Function trans_time ($ timestamp ){
If ($ timestamp <1) echo 'invalid Unix timestamp ';
Else return date ("Y-m-d H: I: s", $ timestamp );
}
// Obtain the IP address
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 ");
Else
$ Ip = "Unknown ";
Return $ ip;
}
// Calculation Time difference: the default return type is minute"
// $ Old_time can only be a timestamp. $ return_type indicates that h is an hour, and s is a second.
Function timelag ($ old_time, $ return_type ='m '){
If ($ old_time <1 ){
Echo 'invalid Unix timestamp ';
} Else {
Switch ($ return_type ){
Case 'H ':
$ Type = 3600; break;
Case'm ':
$ Type = 60; break;
Case's ':
$ Type = 1; break;
Case '':
$ Type = 60; break;
}
$ Dif = round (time ()-$ old_time)/$ type );
Return $ dif;
}
}
// Obtain the URL of the current page
Function url_this (){
$ Url = "http: //". $ _ SERVER ["HTTP_HOST"]. $ _ SERVER ["REQUEST_URI"];
$ Return_url = "$ url ";
Return $ return_url;
}
// Jump function
Function url_redirect ($ url, $ delay = ''){
If ($ delay = ''){
Echo "script window. location. href = '$ url' script";
} Else {
Echo" ";
}
}
} // End func
?>
Http://www.bkjia.com/PHPjc/322611.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/322611.htmlTechArticle? Php // alert prompt function alert ($ msg) {echo "scriptalert ('$ msg');/script ";} // Convert some predefined characters to the HTML object function d_htmlspecialchars ($ string) {if (is_array (...