1、PHP中set_magic_quotes_runtime()函数的作用:
修改PHP.ini文件中的 magic_quotes_runtime 变量的状态,如果想获得magic_quotes_runtime 变量的状态用get_magic_quotes_runtime这个函数如果返回0表示本功能被关闭,如果返回1表示本功能已经开启。
magic_quotes_runtime的功能
当它被开启时所有外部引入的 数据库资料 或 文件 等都会自动转为含有反斜线溢出字符的资料。
比如:
用户向数据库提交的数据中含有\" '这些符号时它就会在这些符号的前面自动加上"\"转义符。
PHP4之前,此属性默认关闭,php4以后默认开启,可以用set_magic_quotes_runtime(0)将其关闭。
2、get_magic_quotes_gpc函数作用:
此函数取得 PHP 环境配置的变量 magic_quotes_gpc (GPC, Get/Post/Cookie) 值。返回 0 表示关闭本功能;返回 1 表示本功能打开。当
magic_quotes_gpc 打开时,所有的 ' (单引号), " (双引号), \ (反斜线) and 空字符会自动加上转义符\;
默认情况下,PHP 指令 magic_quotes_gpc 为 on,它主要是对所有的 GET、POST 和 COOKIE 数据自动运行 addslashes()。不要对已经被 magic_quotes_gpc转义过的字符串使用 addslashes(),因为这样会导致双层转义。遇到这种情况时可以使用函数 get_magic_quotes_gpc() 进行检测。
多用于判断有PHP有没有自动调用addslashes 这个函数,
来看具体 的例子吧。
<?php
echo get_magic_quotes_gpc(); // 检测,输出0
echo
tiny_mce_marker
POST['name']; // jason'name
echo addslashes(
tiny_mce_marker
POST['name']); // jason\'name
if (!get_magic_quotes_gpc()) {
$name = addslashes(
tiny_mce_marker
POST['name']);
} else {
$name =
tiny_mce_marker
POST['name'];
}
echo $name; // jason\'name
//安全写入到数据库了
?>
以下例子中,把两个函数都做了处理。
if(version_compare(PHP_VERSION,'6.0.0','<') ) {
@set_magic_quotes_runtime (0);
define('MAGIC_QUOTES_GPC',get_magic_quotes_gpc()?True:False);
}
另外,还可以用ini_get和ini_set读取和设置系统配置:
在php中,使用自带的mail()函数来发送邮件,会存在很多不安全因素。
我们实现了如下的ae_send_mail函数,适用于PHP4.0.2或更高版本。
ae_send_mail调用时只用四个参数:发件人,收件人,主题,邮件内容。
此函数通过加过一些过滤机制,可以去除一切不必要的特殊字符对邮件的影响。
代码:
<?php
/**
* Email邮件发送
* by www.
*/
function ae_send_mail($from, $to, $subject, $text, $headers="")
{
if (strtolower(substr(PHP_OS, 0, 3)) === 'win')
$mail_sep = "\r\n";
else
$mail_sep = "\n";
function _rsc($s)
{
$s = str_replace("\n", '', $s);
$s = str_replace("\r", '', $s);
return $s;
}
$h = '';
if (is_array($headers))
{
foreach($headers as $k=>$v)
$h = _rsc($k).': '._rsc($v).$mail_sep;
if ($h != '') {
$h = substr($h, 0, strlen($h) - strlen($mail_sep));
$h = $mail_sep.$h;
}
}
$from = _rsc($from);
$to = _rsc($to);
$subject = _rsc($subject);
mail($to, $subject, $text, 'From: '.$from.$h);
}
?>
以上函数有必选参数:$from, $to, $subject, $text,另外有一个可选参数$headers,用于传输一些邮件头信息,它可以接受数组的形式,例如:(“头信息1”=>“值”,“头信息2”=>“值”)。
以下是在具体页面中的例子,大家可以参考下。
代码:
<?php
/**
* as_send_mail函数示例
* 接收联系人信息
* by www.
*/
$site_admin = 'your@email.adress';
// function ae_send_mail (see code above) is pasted here
if (($_SERVER['REQUEST_METHOD'] == 'POST') &&
isset($_POST['subject']) && isset($_POST['text']) &&
isset($_POST['from1']) && isset($_POST['from2']))
{
$from = $_POST['from1'].' <'.$_POST['from2'].'>';
// nice RFC 2822 From field
ae_send_mail($from, $site_admin, $_POST['subject'], $_POST['text'],
array('X-Mailer'=>'PHP script at '.$_SERVER['HTTP_HOST']));
$mail_send = true;
}
?>
<html><head>
<title>发送邮件的例子</title>
</head>
<body>
<?php
if (isset($mail_send)) {
echo '<h1>邮件已发送!谢谢!</h1>';
}
else {
?>
<form action="/blog_article/</php echo $_SERVER[.html'REQUEST_URI']; ?>" method="post">
姓名: <input type="text" name="from1" size="30" /><br />
邮箱: <input type="text" name="from2" size="30" /><br />
主题: <input type="text" name="subject" size="30" /><br />
内容: <br />
<textarea rows="5" cols="40" name="text"></textarea>
<input type="submit" value="send" />
</form>
<?php } ?>
</body>
</html>
注意,由于ae_send_mail函数使用php内置的mail()函数来发送邮件,与此函数相关的问题,ae_send_mail也会存在。
大家在使用时,遇到问题时,请参考mail函数的用法及相关解释。
一个php cookie操作类,实现了基本的操作功能:
创建cookie、设置cookie的过期时间、注销cookie等。
代码:
<?php
namespace com\net {
/**
* php cookie操作类
* by www.
*/
class Cookie {
/**
* cookie名称
* @var string
*/
public $name;
/**
* cookie值
* @var string
*/
public $value;
/**
* cookie过期时间
* @var int
*/
public $expire;
/**
* cookie 保存路径
* @var string
*/
public $path;
/**
* cookie域
* @var string
*/
public $domain;
/**
* Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client.
* When set to TRUE, the cookie will only be set if a secure connection exists.
* On the server-side, it's on the programmer to send this kind of cookie only on secure connection
* (e.g. with respect to $_SERVER["HTTPS"]).
* @var boolean
*/
public $secure;
/**
* When TRUE the cookie will be made accessible only through the HTTP protocol.
* This means that the cookie won't be accessible by scripting languages, such as JavaScript.
* This setting can effectively help to reduce identity theft through XSS attacks
* (although it is not supported by all browsers).
* Added in PHP 5.2.0. TRUE or FALSE
* @var boolean
*/
public $httponly;
/**
* 创建cookie
*
* @param string $name The name of the cookie.
* @param string $value [optional] The value of the cookie.
* @param int $expire [optional] The time the cookie expires.
* @param string $path [optional] The path on the server in which the cookie will be available on.
* @param string $domain [optional] The domain that the cookie is available to.
* @param boolean $secure [optional] Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client.
* @param boolean $httponly [optional] Indicates that the cookie will be made accessible only through the HTTP protocol
*/
public function __construct($name, $value = null, $expire = null, $path = null, $domain = null, $secure = null, $httponly = null){
if(($this->name = (string) $name)){
if(!is_null($value)){
$this->value = (string) $value;
$this->expire = $expire;
$this->path = $path;
$this->domain = $domain;
$this->secure = $secure;
$this->httponly = $httponly;
} else {
$this->value = $this->exists() ? $_COOKIE[$this->name] : '';
}
} else {
throw new Exception("invalid cookie name");
}
}
/**
* 检测cookie是否存在
* @return boolean
*/
public function exists(){
return isset($_COOKIE[$this->name]);
}
/**
* 通过setcookie设置cookie信息
*/
public function save(){
return setcookie($this->name, $this->value, $this->expire, $this->path, $this->domain, $this->secure, $this->httponly);
}
/**
* 注销cookie
*/
public function delete(){
return setcookie($this->name, "", time() - 3600);
}
}
}
?>