[转]10 个有用的 PHP 代码
- 摘要:[代码]获取浏览器IP地址1functiongetRemoteIPAddress(){2$ip=$_SERVER['REMOTE_ADDR'];3return$ip;4}[代码]如果有代理服务器的情况下获取IP01functiongetRealIPAddress(){02if(!empty($_SERVER['HTTP_CLIENT_IP'])){//checkipfromshareinternet03$ip=$_SERVER['HTTP_CLIENT_IP'];04}elseif(
- 标签:PHP 代码
[代码] 获取浏览器IP地址
1
function
getRemoteIPAddress() {
2
????
$ip
=
$_SERVER
[
'REMOTE_ADDR'
];
3
????
return
$ip
;
4
}
[代码] 如果有代理服务器的情况下获取IP
01
function
getRealIPAddress() {
02
????
if
(!
empty
(
$_SERVER
[
'HTTP_CLIENT_IP'
])) {
03
????????
$ip
=
$_SERVER
[
'HTTP_CLIENT_IP'
];
04
????
}
elseif
(!
empty
(
$_SERVER
[
'HTTP_X_FORWARDED_FOR'
])) {
05
????????
$ip
=
$_SERVER
[
'HTTP_X_FORWARDED_FOR'
];
06
????
}
else
{
07
????????
$ip
=
$_SERVER
[
'REMOTE_ADDR'
];
08
????
}
09
????
return
$ip
;
10
}
[代码] 获取 MySQL 时间戳
1
$query
=
"select UNIX_TIMESTAMP(date_field) as mydate from mytable where 1=1"
;
2
$records
= mysql_query(
$query
)
or
die
(mysql_error());
3
while
(
$row
= mysql_fetch_array(
$records
)) {
4
????
echo
$row
;
5
}
[代码] 验证日期格式:YYYY-MM-DD
01
function
checkDateFormat(
$date
) {
02
????
03
????
if
(preg_match(
"/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/"
,
$date
,
$parts
)) {
04
????????
05
????????
if
(
checkdate
(
$parts
[2],
$parts
[3],
$parts
[1])) {
06
????????????
return
true;
07
????????
}
else
{
08
????????????
return
false;
09
????????
}
10
????
}
else
{
11
????????
return
false;
12
????
}
13
}
[代码] 重定向
1
header(
'Location: http://www.oschina.net/project/zh'
);
01
$to
=
"someone@oschina.net"
;
02
$subject
=
"Your Subject here"
;
03
$body
=
"Body of your message here you can use HTML too. e.g. <br><b> Bold </b>"
;
04
$headers
=
"From: You\r\n"
;
05
$headers
.=
"Reply-To: info@yoursite.com\r\n"
;
06
$headers
.=
"Return-Path: info@yoursite.com\r\n"
;
07
$headers
.=
"X-Mailer: PHP\n"
;
08
$headers
.=
'MIME-Version: 1.0'
.
"\n"
;
09
$headers
.=
'Content-type: text/html; charset=iso-8859-1'
.
"\r\n"
;
10
mail(
$to
,
$subject
,
$body
,
$headers
);
[代码] BASE64 编码和解码
01
function
base64url_encode(
$plainText
) {
02
????
$base64
=
base64_encode
(
$plainText
);
03
????
$base64url
=
strtr
(
$base64
,
'+/='
,
'-_,'
);
04
????
return
$base64url
;
05
}
06
?
?
07
function
base64url_decode(
$plainText
) {
08
????
$base64url
=
strtr
(
$plainText
,
'-_,'
,
'+/='
);
09
????
$base64
=
base64_decode
(
$base64url
);
10
????
return
$base64
;
11
}
[代码] JSON 处理
1
$json_data
=
array
(
'id'
=>1,
'name'
=>
"John"
,
'country'
=>
'Canada'
,
"work"
=>
array
(
"Google"
,
"Oracle"
));
2
echo
json_encode(
$json_data
);
3
?
4
$json_string
=
'{"id":1,"name":"John","country":"Canada","work":["Google","Oracle"]} '
;
5
$obj
=json_decode(
$json_string
);
6
?
7
8
echo
$obj
->name;
9
echo
$obj
->work[0];
[代码] 检测用户浏览器类型
1
$useragent
=
$_SERVER
[
'HTTP_USER_AGENT'
];
2
echo
"<b>Your User Agent is</b>: "
.
$useragent
;
[代码] 显示网页源码
1
$lines
= file(
'http://www.oschina.net/home/about'
);
2
foreach
(
$lines
as
$line_num
=>
$line
) {
3
????
4
????
echo
"Line #<b>{$line_num}</b> : "
. htmlspecialchars(
$line
) .
"<br>\n"
;
5
}
[代码] 调整服务器时间
1
$now
=
date
(
'Y-m-d-G'
);
2
$now
=
strftime
(
"%Y-%m-%d-%H"
,
strtotime
(
"$now -8 hours"
));