10段PHP常用功能代码
1、使用PHP Mail函数发送Email
$to = "viralpatel.net@gmail.com";
$subject = "VIRALPATEL.net";
$body = "Body of your message here you can use HTML too. e.g. ﹤br﹥ ﹤b﹥ Bold ﹤/b﹥";
$headers = "from: Peter\r\n";
$headers .= "Reply-To: info@yoursite.com\r\n";
$headers .= "Return-Path: info@yoursite.com\r\n";
$headers .= "X-Mailer: PHP5\n"; $headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to,$subject,$body,$headers);
2、PHP中的64位编码和解码
function base64url_encode($plainText) {
$base64 = base64_encode($plainText);
$base64url = strtr($base64, '+/=', '-_,');
return $base64url;
}
function base64url_decode($plainText) {
$base64url = strtr($plainText, '-_,', '+/=');
$base64 = base64_decode($base64url);
return $base64;
}
3、获取远程IP地址
function getRealIPAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
4、 日期格式化
function checkDateFormat($date)
{
//match the format of the date
if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts))
{//check weather the date is valid of not
if(checkdate($parts[2],$parts[3],$parts[1]))
return true;
else
return false;
}
else
return false;
}
5、验证Email
$email = $_POST['email'];
if(preg_match("~([a-zA-Z0-9!#$%&'*+-/=?^_`{|}~])@([a-zA-Z0-9-]).
([a-zA-Z0-9]{2,4})~",$email)) {
echo 'This is a valid email.';
} else{
echo 'This is an invalid email.';
}
6、在PHP中轻松解析XML
//this is a sample xml string
$xml_string="﹤?x
相关文档:
1、模板的由来
在没有模板技术之前,使用PHP开发程序,通常都是php代码和html混编在一起。比如说新闻列表,很可能就是一个newslist.php页面,结构如下:
<?
//从数据库中读取出要显示的新闻记录
?>
<html>
<head>……..
</head>
<body>
<?
While ($news ......
装了PHP-5.3.0, 启动的时候总是会有
"[06-Aug-2009 13:27:31] PHP Warning: PHP Startup: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are stil ......
可以使用的命令:
popen
fpassthru
shell_exec
exec
system
1.popen
resource popen
( string command, string mode )
打开一个指向进程的管道,该进程由派生给定的 command
命令执行而产生。
返回一个和 fopen()
所返回的相同的文件指针,只不过它是单向的(只能用于读或写)并且必须用 pclose()
来关闭 ......
在PHP中修补XSS漏洞,我们可以使用三个PHP函数。
这些函数主要用于清除HTML标志,这样就没办法注入代码了。使用更多的函数是htmlspecialchars() ,它可以将所有的"<"与">"符号转换成"<" 与">;"。其它可供选择的函数还有htmlentities(), 它可以用相应的字符实体(entities)替换掉所有想要替换掉的特征码(cha ......
UTF-8匹配: 在javascript中,要判断字符串是中文是很简单的。比如:
var str = "php编程";
if (/^[\u4e00-\u9fa5]+$/.test(str)) {
alert("该字符串全部是中文");
}
else{
alert("该字符串不全部是中文");
} php中,是用\x表示十六进制数据的。于是,变换成如下的代码:
$str = "php编程";
if (preg_match("/^[\x4 ......