Continue to collect some PHP common functions 1th/2 page _php tips

Source: Internet
Author: User
Tags ereg explode flock get ip php redirect rar gtar

Copy Code code as follows:

?
function GetIP () {//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;
}
?>

Copy Code code as follows:

<?php
function DateAdd ($date, $int, $unit = "D") {//Time increase (can also be improved as time and minutes can be increased, time to fill)
$DATEARR = Explode ("-", $date);
$value [$unit] = $int;
Return date ("Y-m-d", Mktime (0,0,0, $DATEARR [1] + $value [' m '], $DATEARR [2] + $value [' d '], $DATEARR [0] + $value [' Y ']));
}
function Getweekday ($date) {//Calculate the date given is the day of the week
$DATEARR = Explode ("-", $date);
Return Date ("W", Mktime (0,0,0, $DATEARR [1], $DATEARR [2], $DATEARR [0]));
}
?>

Copy Code code as follows:

?
function Check_date ($date) {//check date is legal date
$DATEARR = Explode ("-", $date);
if (is_numeric ($dateArr [0]) && is_numeric ($DATEARR [1]) && is_numeric ($DATEARR [2])) {
return Checkdate ($DATEARR [1], $DATEARR [2], $DATEARR [0]);
}
return false;
}
function Check_time ($time) {//Check time is legal time
$TIMEARR = Explode (":", $time);
if (is_numeric ($timeArr [0]) && is_numeric ($TIMEARR [1]) && is_numeric ($TIMEARR [2])) {
if ($timeArr [0] >= 0 && $timeArr [0] <=) && ($TIMEARR [1] >= 0 && $timeArr [1] <= 59) && ($TIMEARR [2] >= 0 && $timeArr [2] <= 59))
return true;
Else
return false;
}
return false;
}
function DateDiff ($date 1, $date 2, $unit = "") {//Time comparison function, return two date difference a few seconds, minutes, hours or days
Switch ($unit) {
Case ' s ':
$dividend = 1;
Break
Case ' I ':
$dividend = 60;
Break
Case ' H ':
$dividend = 3600;
Break
Case ' d ':
$dividend = 86400;
Break
Default
$dividend = 86400;
}
$time 1 = strtotime ($date 1);
$time 2 = strtotime ($date 2);
if ($time 1 && $time 2)
return (float) ($time 1-$time 2)/$dividend;
return false;
}
?>

PHP redirect
Copy Code code as follows:

?
Method One: Header ("Location:index.php");
Method Two: echo "<scrīpt>window.location =" $PHP _self ";</scrīpt>";
Method Three: Echo "<meta http-equiv=" Refresh "content=" 0; Url=index.php ">";
?>

Get Visitor Browser
Copy Code code as follows:

?
function Browse_infor ()
{
$browser = ""; $browserver = "";
$Browsers =array ("Lynx", "MOSAIC", "AOL", "Opera", "JAVA", "Macweb", "Webexplorer", "OmniWeb");
$Agent = $GLOBALS ["Http_user_agent"];
For ($i =0 $i <=7; $i + +)
{
if (Strpos ($Agent, $Browsers [$i])
{
$browser = $Browsers [$i];
$browserver = "";
}
}
if (Ereg ("Mozilla", $Agent) &&!ereg ("MSIE", $Agent))
{
$temp =explode ("(", $Agent); $Part = $temp [0];
$temp =explode ("/", $Part); $browserver = $temp [1];
$temp =explode ("", $browserver); $browserver = $temp [0];
$browserver =preg_replace ("/([D.] +)/"," 1 ", $browserver);
$browserver = "$browserver";
$browser = "Netscape Navigator";
}
if (Ereg ("Mozilla", $Agent) && ereg ("Opera", $Agent))
{
$temp =explode ("(", $Agent); $Part = $temp [1];
$temp =explode (")", $Part); $browserver = $temp [1];
$temp =explode ("", $browserver); $browserver = $temp [2];
$browserver =preg_replace ("/([D.] +)/"," 1 ", $browserver);
$browserver = "$browserver";
$browser = "Opera";
}
if (Ereg ("Mozilla", $Agent) && ereg ("MSIE", $Agent))
{
$temp = Explode ("(", $Agent); $Part = $temp [1];
$temp = Explode (";", $Part); $Part = $temp [1];
$temp = Explode ("", $Part); $browserver = $temp [2];
$browserver =preg_replace ("/([D.] +)/"," 1 ", $browserver);
$browserver = "$browserver";
$browser = "Internet Explorer";
}
if ($browser!= "")
{
$browseinfo = "$browser $browserver";
}
Else
{
$browseinfo = "Unknown";
}
return $browseinfo;
}
Call Method $browser=browseinfo (), return results directly
?>

Get the visitor's operating system
Copy Code code as follows:

?
function Osinfo () {
$os = "";
$Agent = $GLOBALS ["Http_user_agent"];
if (eregi (' win ', $Agent) && strpos ($Agent, ' 95 ')) {
$os = "Windows 95";
}
ElseIf (Eregi (' Win 9x ', $Agent) && strpos ($Agent, ' 4.90 ')) {
$os = "Windows ME";
}
ElseIf (' Win ', $Agent) && ereg (' Eregi ', $Agent)) {
$os = "Windows 98";
}
ElseIf (eregi (' win ', $Agent) && eregi (' NT 5.0 ', $Agent)) {
$os = "Windows 2000";
}
ElseIf (eregi (' win ', $Agent) && eregi (' NT ', $Agent)) {
$os = "Windows NT";
}
ElseIf (eregi (' win ', $Agent) && eregi (' NT 5.1 ', $Agent)) {
$os = "Windows XP";
}
ElseIf (' Win ', $Agent) && ereg (' Eregi ', $Agent)) {
$os = "Windows 32";
}
ElseIf (eregi (' Linux ', $Agent)) {
$os = "Linux";
}
ElseIf (eregi (' Unix ', $Agent)) {
$os = "Unix";
}
ElseIf (Eregi (' Sun ', $Agent) && eregi (' OS ', $Agent)) {
$os = "SunOS";
}
ElseIf (eregi (' IBM ', $Agent) && eregi (' OS ', $Agent)) {
$os = "IBM os/2";
}
ElseIf (eregi (' Mac ', $Agent) && eregi (' PC ', $Agent)) {
$os = "Macintosh";
}
ElseIf (eregi (' PowerPC ', $Agent)) {
$os = "PowerPC";
}
ElseIf (eregi (' AIX ', $Agent)) {
$os = "AIX";
}
ElseIf (eregi (' HPUX ', $Agent)) {
$os = "HPUX";
}
ElseIf (eregi (' NetBSD ', $Agent)) {
$os = "NetBSD";
}
ElseIf (eregi (' BSD ', $Agent)) {
$os = "BSD";
}
ElseIf (Ereg (' OSF1 ', $Agent)) {
$os = "OSF1";
}
ElseIf (Ereg (' IRIX ', $Agent)) {
$os = "IRIX";
}
ElseIf (eregi (' FreeBSD ', $Agent)) {
$os = "FreeBSD";
}
if ($os = = ") $os =" Unknown ";
return $os;
}
Call Method $os=os_infor ();
?>

File Format class
Copy Code code as follows:

?
$mime _types = Array (
' gif ' => ' image/gif ',
' jpg ' => ' image/jpeg ',
' JPEG ' => ' image/jpeg ',
' Jpe ' => ' image/jpeg ',
' BMP ' => ' image/bmp ',
' PNG ' => ' image/png ',
' tif ' => ' Image/tiff ',
' TIFF ' => ' Image/tiff ',
' Pict ' => ' image/x-pict ',
' Pic ' => ' image/x-pict ',
' pct ' => ' image/x-pict ',
' tif ' => ' Image/tiff ',
' TIFF ' => ' Image/tiff ',
' PSD ' => ' image/x-photoshop ',
' swf ' => ' Application/x-shockwave-flash ',
' JS ' => ' application/x-javascrīpt ',
' PDF ' => ' application/pdf ',
' ps ' => ' application/postscrīpt ',
' EPS ' => ' application/postscrīpt ',
' AI ' => ' application/postscrīpt ',
' WMF ' => ' application/x-msmetafile ',
' CSS ' => ' text/css ',
' htm ' => ' text/html ',
' HTML ' => ' text/html ',
' txt ' => ' text/plain ',
' xml ' => ' Text/xml ',
' WML ' => ' text/wml ',
' Wbmp ' => ' image/vnd.wap.wbmp ',
' Mid ' => ' Audio/midi ',
' WAV ' => ' audio/wav ',
' mp3 ' => ' audio/mpeg ',
' MP2 ' => ' audio/mpeg ',
' avi ' => ' Video/x-msvideo ',
' MPEG ' => ' video/mpeg ',
' mpg ' => ' video/mpeg ',
' Qt ' => ' video/quicktime ',
' mov ' => ' video/quicktime ',
' Lha ' => ' Application/x-lha ',
' Lzh ' => ' Application/x-lha ',
' Z ' => ' application/x-compress ',
' Gtar ' => ' Application/x-gtar ',
' GZ ' => ' application/x-gzip ',
' gzip ' => ' application/x-gzip ',
' tgz ' => ' application/x-gzip ',
' Tar ' => ' Application/x-tar ',
' bz2 ' => ' application/bzip2 ',
' Zip ' => ' application/zip ',
' Arj ' => ' Application/x-arj ',
' rar ' => ' application/x-rar-compressed ',
' hqx ' => ' application/mac-binhex40 ',
' Sit ' => ' application/x-stuffit ',
' Bin ' => ' application/x-macbinary ',
' UU ' => ' Text/x-uuencode ',
' Uue ' => ' Text/x-uuencode ',
' Latex ' => ' Application/x-latex ',
' Ltx ' => ' Application/x-latex ',
' Tcl ' => ' application/x-tcl ',
' PGP ' => ' APPLICATION/PGP ',
' ASC ' => ' APPLICATION/PGP ',
' exe ' => ' application/x-msdownload ',
' Doc ' => ' Application/msword ',
' RTF ' => ' application/rtf ',
' xls ' => ' application/vnd.ms-excel ',
' ppt ' => ' application/vnd.ms-powerpoint ',
' MDB ' => ' application/x-msaccess ',
' WRI ' => ' application/x-mswrite ',
);
?>

PHP generates Excel documents
Copy Code code as follows:

?
Header ("Content-type:application/vnd.ms-excel");
Header ("Content-disposition:filename=test.xls");
echo "TEST1T";
echo "Test2tn";
echo "TEST1T";
echo "Test2tn";
echo "TEST1T";
echo "Test2tn";
echo "TEST1T";
echo "Test2tn";
echo "TEST1T";
echo "Test2tn";
echo "TEST1T";
echo "Test2tn";
Change the file header to output the. doc. xls file format
?>

Time comparison issues
Give a simple example to illustrate: for example, a forum on the day posted posts with new pictures marked.
Method One:
Copy Code code as follows:

?
$db->rows[$i][date] is the value of the DateTime field in the database.
$today =time ();
$theDay =date ("y-m-d h:i:s", $today -24*3600);
$newTag = $db->rows[$i][date]>= $theDay? " ': "";
Method Two:
$newTag = $db->rows[$i][date]>=date ("y-m-d 00:00:00")? ': "";
?>

Copy Code code as follows:

Extracts pages and browser-submitted variables that are equivalent to making php.ini open global variables
?
@extract ($_server, Extr_skip);
@extract ($_session, Extr_skip);
@extract ($_post, Extr_skip);
@extract ($_files, Extr_skip);
@extract ($_get, Extr_skip);
@extract ($_env, Extr_skip);
?>

Reading file functions
Copy Code code as follows:

?
function ReadFromFile ($file _name) {
if (file_exists ($file _name)) {
$filenum =fopen ($file _name, "R");
Flock ($filenum, LOCK_EX);
$file _data=fread ($filenum, FileSize ($file _name));
Rewind ($filenum);
Fclose ($filenum);
return $file _data;
}
}
?>

Write to a file function
Copy Code code as follows:

?
function WriteToFile ($file _name, $data, $method = "W") {
$filenum =fopen ($file _name, $method);
Flock ($filenum, LOCK_EX);
$file _data=fwrite ($filenum, $data);
Fclose ($filenum);
return $file _data;
}
?>

Current 1/2 page 12 Next read the full text

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.