php5.3提示Function ereg() is deprecated Error问题

 更新时间:2016年11月25日 16:46  点击:1626
PHP 5.3 ereg() 无法正常使用,提示“Function ereg() is deprecated Error”是因为它长ereg 函数进行了升级处理哦,需要像preg_match使用/ /来规则了,当然也是php5.3把ereg给费掉的节奏了。

PHP 5.3 ereg() 无法正常使用,提示“Function ereg() is deprecated Error”。

问题根源是php中有两种正则表示方法,一个是posix,一个是perl,php6打算废除posix的正则表示方法所以后来就加了个preg_match。此问题解决办法很简单,在ereg前加个过滤提示信息符号即可:把ereg()变成@ereg()。这样屏蔽了提示信息,但根本问题还是没有解决,php在5.2版本以前ereg都使用正常,在5.3以后,就要用preg_match来代替ereg。所以就需要变成这样,

原来:ereg("^[0-9]*$",$page)变成:preg_match("/^[0-9]*$/",$page)

特别提醒:posix与perl的很明显的表达区别就是是否加斜杠,所以与ereg相比,后者在正则的前后分别增加了两个"/"符号,不能缺少。


改前:function inject_check($sql_str) {

 $sql_str = strtolower($sql_str);
 return eregi('fopen|post|eval|select|insert|and|or|update|delete|'|/*|*|../|./|union|into|load_file|outfile', $sql_str); // 进行过滤
}

解决方法:

找到代码所在的文件 位置

 

function inject_check($sql_str) {
 $sql_str = strtolower($sql_str);
 return preg_match('/fopen|post|eval|select|insert|and|or|update|delete|'|/*|*|../|./|union|into|load_file|outfile/', $sql_str); // 进行过滤
}
 

注意:一定要加'/'开头与结束哦。此段参考:http://www.111cn.net/phper/31/47360.htm


Tips:此问题在php5.2之前版本不会出现。

自动补足空白位数在php中str_pad函数可以帮我们实现哦,str_pad() 函数把字符串填充为指定的长度。

str_pad() 函数把字符串填充为指定的长度。

语法

str_pad(string,length,pad_string,pad_type)

参数 描述
string 必需。规定要填充的字符串。
length 必需。规定新字符串的长度。如果该值小于原始字符串的长度,则不进行任何操作。
pad_string 可选。规定供填充使用的字符串。默认是空白。
pad_type

可选。规定填充字符串的那边。

可能的值:

  • STR_PAD_BOTH - 填充到字符串的两头。如果不是偶数,则右侧获得额外的填充。
  • STR_PAD_LEFT - 填充到字符串的左侧。
  • STR_PAD_RIGHT - 填充到字符串的右侧。这是默认的。

 代码如下 复制代码

$cardCount = 10;
$arr = array();
for ($i = 1; $i <= $cardCount; $i++) {
$strCard = str_pad($i, 10, '0', STR_PAD_LEFT);
$arr[] = $strCard;
}

print_r($arr);

Array ( [0] => 0000000001 [1] => 0000000002 [2] => 0000000003 [3] => 0000000004 [4] => 0000000005

[5] =>0000000006 [6] => 0000000007 [7] => 0000000008 [8] => 0000000009 [9] => 0000000010 )

 

下面我们一看一篇关于php中file_get_contents()导致nginx出现504问题的解决办法,希望此方法对各位朋友有所帮助。

Nginx+PHP-CGI(php-fpm) 的Web环境
突然发现系统负载上升,top 查看后发现很多 php-cgi 进程 CPU 使用率接近100%
找其中一个 CPU 100% 的 php-cgi 进程的 PID,用strace -p 10747跟踪,结果发现以下结果:
select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout)
select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout)
select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout)
select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout)
select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout)
select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout)
select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout)
select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout)
select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout)
select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout)

几乎可以肯定是file_get_contents()导致的问题,
原因是:file_get_contents的目标网站如果反应过慢,file_get_contents就会一直卡在那里不会超时,
我们知道php.ini 里面max_execution_time 可以设置 PHP 脚本的最大执行时间,但是,在 php-cgi(php-fpm) 中,该参数不会起效。真正能够控制 PHP 脚本最大执行时间的是 php-fpm.conf 配置文件中的以下参数:

The timeout (in seconds) for serving a single request after which the worker process will be terminated 
Should be used when 'max_execution_time' ini option does not stop script execution for some reason 
'0s' means 'off' 
<value name="request_terminate_timeout">0s</value>  默认值为 0 秒,也就是说,PHP 脚本会一直执行下去。这样,当所有的 php-cgi 进程都卡在 file_get_contents() 函数时,这台 Nginx+PHP 的 WebServer 已经无法再处理新的 PHP 请求了,Nginx 将给用户返回“502 Bad Gateway”。修改该参数,设置一个 PHP 脚本最大执行时间是必要的,但是,治标不治本。例如改成 30s,如果发生 file_get_contents() 获取网页内容较慢的情况,这就意味着 150 个 php-cgi 进程,每秒钟只能处理 5 个请求,WebServer 同样很难避免“502 Bad Gateway”。

要做到彻底解决,不妨重新封装一下file_get_contents函数:

 代码如下 复制代码

function _file_get_content($str) {
$ctx = stream_context_create(array(  
   'http' => array(  
       'timeout' => 10 //设置一个超时时间,单位为秒  
       )  
   )  
);  
return file_get_contents($str, 0, $ctx);  
}

如此 用_file_get_content代替直接使用file_get_contents 问题解决。

header() 函数向客户端发送原始的 HTTP 报头,主要包括有HTTP协议的版本、状态代码、原因短语等我们常用于跳转页面,状态发送与文件下载,下面我们一起来看看。

header分为三部分:

第一部分为HTTP协议的版本(HTTP-Version);
第二部分为状态代码(Status);
第三部分为原因短语(Reason-Phrase)。

header()函数使用说明:  

一、作用:  
~~~~~~~~~  
       PHP只是以HTTP协议将HTML文档的标头送到浏览器,告诉浏览器具体怎么处理这个页面,至于传送的内容则需要熟悉一下HTTP协议了,与PHP无关了,可参照http://www.w3.org/Protocols/rfc2616/rfc2616。  
       传统的标头一定包含下面三种标头之一,并只能出现一次。  
       Location:  xxxx:yyyy/zzzz  
       Content-Type:  xxxx/yyyy  
       Status:  nnn  xxxxxx  

二、先来了解一下HTTP协议的运作方式  
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
       HTTP协议是基于请求/响应范式的。一个客户机与服务器建立连接后,发送一个请求给服务器,请求方式的格式为,统一资源标识符、协议版本号,后边是MIME信息包括请求修饰符、客户机信息和可能的内容。服务器接到请求后,给予相应的响应信息,其格式为一个状态行包括信息的协议版本号、一个成功或错误的代码,后边是MIME信息包括服务器信息、实体信息和可能的内容。  
       它分四个过程,在HTTP协议中,服务端是指提供HTTP服务的部分,客户端是指你使用的浏览器或者下载工具等等。在通讯时,由客户端发出请求连接,服务端建立连接;然后,客户端发出HTTP请求(Request),服务端返回响应信息(Respond),由此完成一个HTTP操作。  

三、HTTP协议状态码表示的意思  
~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
  1××  保留  
  2××  表示请求成功地接收  
  3××  为完成请求客户需进一步细化请求  
  4××  客户错误  
  5××  服务器错误  

 代码如下 复制代码

// fix 404 pages: 用这个header指令来解决URL重写产生的404 header
header(‘HTTP/1.1 200 OK’);

// set 404 header: 页面没找到
header(‘HTTP/1.1 404 Not Found’);

// 页面被永久删除,可以告诉seo/seo.html" target="_blank">搜索引擎更新它们的urls
// set Moved Permanently header (good for redrictions)
// use with location header
header(‘HTTP/1.1 301 Moved Permanently’);
// 访问受限
header(‘HTTP/1.1 403 Forbidden’);
// 服务器错误
header(‘HTTP/1.1 500 Internal Server Error’);

// 重定向到一个新的位置
// redirect to a new location:
header(‘Location: http://www.m-bang.com);

延迟一段时间后重定向
// redrict with delay:
header(‘Refresh: 10; url=http://www.sina.com.cn’);
print ‘You will be redirected in 10 seconds’;

// 覆盖 X-Powered-By value
// override X-Powered-By: PHP:
header(‘X-Powered-By: PHP/4.4.0′);
header(‘X-Powered-By: Brain/0.6b’);

// 内容语言 (en = English)
// content language (en = English)
header(‘Content-language: en’);

//最后修改时间 (在缓存的时候可以用到)
// last modified (good for caching)
$time = time() – 60; // or filemtime($fn), etc
header(‘Last-Modified: ‘.gmdate(‘D, d M Y H:i:s’, $time).’ GMT’);

// 告诉浏览器要获取的内容还没有更新
// header for telling the browser that the content
// did not get changed
header(‘HTTP/1.1 304 Not Modified’);

// 设置内容的长度 (缓存的时候可以用到):
// set content length (good for caching):
header(‘Content-Length: 1234′);

// 用来下载文件:
// Headers for an download:
header(‘Content-Type: application/octet-stream’);
header(‘Content-Disposition: attachment; filename=”example.zip”‘);
header(‘Content-Transfer-Encoding: binary’);

// 禁止缓存当前文档:
// load the file to send:readfile(‘example.zip’);
// Disable caching of the current document:
header(‘Cache-Control: no-cache, no-store, max-age=0, must-revalidate’);
header(‘Expires: Mon, 26 Jul 1997 05:00:00 GMT’);
// 设置内容类型:
// Date in the pastheader(‘Pragma: no-cache’);
// set content type:
header(‘Content-Type: text/html; charset=iso-8859-1′);
header(‘Content-Type: text/html; charset=utf-8′);
header(‘Content-Type: text/plain’);

// plain text file
header(‘Content-Type: image/jpeg’);

// JPG picture
header(‘Content-Type: application/zip’);

// ZIP file
header(‘Content-Type: application/pdf’);

// PDF file
header(‘Content-Type: audio/mpeg’);

// Audio MPEG (MP3,…) file
header(‘Content-Type: application/x-shockwave-flash’);

// 显示登录对话框,可以用来进行HTTP认证
// Flash animation// show sign in box
header(‘HTTP/1.1 401 Unauthorized’);
header(‘WWW-Authenticate: Basic realm=”Top Secret”‘);
print ‘Text that will be displayed if the user hits cancel or ‘;
print ‘enters wrong login da
ta’;
?>

现在表单的填写,我们可以用AJAX对用户随时进行验证,进行友好的提示,但是在用户没有留意AJAX友好提示,提交了错误的表单,跳回原页,而填写的信息却全部丢失了。要支持页面回跳,有以下的办法:
1. 使用session_cache_limiter方法: session_cache_limiter(‘private,must-revalidate’);但是要值得注意的是 session_cache_limiter()方法要写在session_start()方法之前才有用;
2.用header来设置控制缓存的方法: header(‘Cache-control:private,must-revalidate’);


页面跳转要注意的几个问题总结

1、location和“:”号间不能有空格,否则会出错。
2、在用header前不能有任何的输出。
3、header后的PHP代码还会被执行。
下面是和asp中重定向response.redirect的比较:
例1:

 代码如下 复制代码
response.redirect "../test.asp"
header("location:../test.php");

两者区别:
asp的redirect函数可以在向客户发送头文件后起作用.

 代码如下 复制代码
<html><head></head><body>
<%response.redirect "../test.asp"%>
</body></html>
查是php中下例代码会报错:
<html><head></head><body>
<?
header("location:../test.php");
?>
</body></html>
只能这样:
<?
header("location:../test.php");
?>
<html><head></head><body>...</body></html>

即header函数之前不能向客户发送任何数据.
例2:
asp中

 代码如下 复制代码
<html><head></head><body>
<%
response.redirect "../a.asp"
response.redirect "../b.asp"
%>
</body></html>
结果是重定向a.asp文件.
php呢?
<?
header("location:../a.php");
header("location:../b.php");
?>
<html><head></head><body></body></html>

我们发现它重定向b.php.
原来在asp中执行redirect后不会再执行后面的代码.
而php在执行header后,继续执行下面的代码.
在这方面上php中的header重定向不如asp中的重定向.有时我们要重定向后,不能执行后面的代码:
一般地我们用

 代码如下 复制代码
if(...)
header("...");
else
{
...
}

但是我们可以简单的用下面的方法:

 代码如下 复制代码
if(...)
{ header("...");exit();}

还要注意的是,如果是用Unicode(UTF-8)编码时也会出现问题,需要调整缓存设置.

 代码如下 复制代码
<[email=%@]%@LANGUAGE="VBSCRIPT[/email]" CODEPAGE="936"%>
<%if Request.ServerVariables("SERVER_NAME")="s.111cn.net" then
response.redirect "news/index.htm"
else%>
<%end if%>
<script>
var url = location.href;
if(url.indexOf('http://www.111cn.net/')!=-1)location.href='/index/index.htm';
if(url.indexOf('http://www.zhutiy.com/')!=-1)location.href='/index1/index.htm';
if(url.indexOf('http://www.111cn.net/')!=-1)location.href='/cn/index.asp';
if(url.indexOf('http://www.baidu.com/')!=-1)location.href='/cn/index.asp';
</script>
闭包函数是在PHP5.3版本才引入的了,闭包函数也就是匿名函数函数了,这个与js中的匿名函数很像了,下面我们来看看php匿名函数吧。

php闭包函数比如你现在就可以这样使用:

 代码如下 复制代码

$closure = function($param) { echo $param; };

感觉和js是不是一样的用法了.

一些闭包函数实例

 代码如下 复制代码


function test(){
$test='';
$test=function ($str){
echo 'test';
return $str;
};
timeout('Y-m-d H:i:s',function ($time){
//$this->date=time();
return $time-24*60*60;
});

var_dump($test(‘hello word!’));

}
function timeout($format,$time){
echo date($format,$time(time()));
}
test();
?>

上例输出

2013-11-19 16:24:56teststring(11) “hello word!”

这样子参数便可以用函数了。
条件是,php3.0以后php 4.0以后闭包函数支持$this用法

闭包函数通常被用在preg_match等有callback的函数

 代码如下 复制代码

class A {
private static $sfoo = 1;
private $ifoo = 2;
}
$cl1 = static function() {
return A::$sfoo;
};
$cl2 = function() {
return $this->ifoo;
};

$bcl1 = Closure::bind($cl1, null, ‘A’);
$bcl2 = Closure::bind($cl2, new A(), ‘A’);
echo $bcl1(), “n”;
echo $bcl2(), “n”;
?>

输出
1
2
bind将类可以在闭包函数中使用

 代码如下 复制代码


class A1 {
function __construct($val) {
$this->val = $val;
}
function getClosure() {
//returns closure bound to this object and scope
return function() { return $this->val; };
}
}

$ob1 = new A1(1);
$ob2 = new A1(2);

$cl = $ob1->getClosure();
echo $cl(), “n”;
$cl = $cl->bindTo($ob2);
echo $cl(), “n”;
?>

以上例程的输出类似于:
1
2
bindto在类里可以再次绑定类

 代码如下 复制代码

$fn = function(){
return ++$this->foo; // increase the value
};

class Bar{
private $foo = 1; // initial value
}

$bar = new Bar();

$fn1 = $fn->bindTo($bar, ‘Bar’); // specify class name
$fn2 = $fn->bindTo($bar, $bar); // or object
$fn3 = $fn2->bindTo($bar); // or object

echo $fn1(); // 2
echo $fn2(); // 3
echo $fn3(); // 4

?>

在类之外需要绑定类才能用,绑定可以是类名,也可以是对象,绑定过之后可以再次绑定不需要提拱类名或对象

[!--infotagslink--]

相关文章