There are questions remain, We'll search for the answers together. But one thing we known for sure,the future is not set!

【原创文章】修正升级到PHP5.6后ecshop系统前后台出现的几个错误

ecshop 百蔬君 10269℃ 已收录 1评论

基于整个系统的优化和性能和对各种网站程序的支持程度方面来考虑,最近更换了操作系统和网站运行环境,把php从原来的5.3升级到了现在的5.6,当初没有升级php环境一个重要原因就是不想去修改各个程序对php5.6的兼容代码,引起这个话题的主要原因是php5.6丢弃了5.3版本的很多参数和书写规范,导致原来的代码运营在5.6版本上出错。今天就细细给大家说说我在搬迁百蔬网时遇到的几个问题,和大家共勉。

preg_replace函数是蛮多地方需要修改的一个函数,改动最多的是它,特别是后台很多地方都是提示这个函数的/e表达符弃用了,需要规范。大多数是在includes/cls_template.php文件。

includes/cls_template.php

错误提示:

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in includes/cls_template.php on line 300

解决办法:
300行

return preg_replace("/{([^\}\{\n]*)}/e", "\$this->select('\\1');", $source);

替换为

return preg_replace_callback("/{([^\}\{\n]*)}/", function($r) { return $this->select($r[1]); }, $source);

497行

 $out = "<?php \n" . '$k = ' . preg_replace("/(\'\\$[^,]+)/e" , "stripslashes(trim('\\1','\''));", var_export($t, true)) . ";\n";

修改为

$out = "<?php \n" . '$k = ' . preg_replace_callback("/(\'\\$[^,]+)/" , function($match){return stripslashes(trim($match[1],'\''));}, var_export($t, true)) . ";\n";

556行

$val = preg_replace("/\[([^\[\]]*)\]/eis", "'.'.str_replace('$','\$','\\1')", $val);

替换为

$val = preg_replace_callback('/\[([^\[\]]*)\]/is',function ($matches) {return '.'.str_replace('$','\$',$matches[1]);},$val);

1074行,这一行的修改是下一个preg_replace替换成功的关键,必须去掉pattern中的表达符/e

$pattern = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/se';

修改为

$pattern = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/s';

1076行

$source = preg_replace($pattern, $replacement, $source);

替换为

$source =preg_replace_callback($pattern, function($matcheb){ return '{include file='.strtolower($matcheb[1]). '}';}, $source);

mobile\include\vendor\phpmailer\class.phpmailer.php

如果有使用ectouch作为ecshop的手机移动客服端,那么还要修改这个文件mobile\include\vendor\phpmailer\class.phpmailer.php,这个文件有多个preg_replace函数使用了/e参数,具体如下:
1703行

$encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);

修改为

$encoded =preg_replace_callback("/([^A-Za-z0-9!*+\/ -])/", function($ra) { return '='.sprintf('%02X', ord($ra[1]));}, $encoded);

1707行

$encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);

修改为

$encoded = preg_replace_callback("/([\(\)\"])/", function($rb) { return '='.sprintf('%02X', ord($rb[1]));}, $encoded);

1713行

$encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e', "'='.sprintf('%02X', ord('\\1'))", $encoded);

修改为

$encoded = preg_replace_callback('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/', function($rc) { return '='.sprintf('%02X',ord($rc[1]));}, $encoded);

 

这个文件里面还存在一个书写规范的问题

错误提示

 Strict Standards: Only variables should be passed by reference in  \includes\cls_template.php on line 418

解决办法:
423行:
$tag_sel = array_shift(explode(' ', $tag));
php5.6已经不允许这样相套使用了,更换为
$tag_arr = explode(' ', $tag); $tag_sel = array_shift($tag_arr);

\mobile\include\library\EcsTemplate.class.php

248行

return preg_replace("/{([^\}\{\n]*)}/e", "\$this->select('\\1');", $source);

修改为

return preg_replace_callback("/{([^\}\{\n]*)}/", function($r) { return $this->select($r[1]);}, $source);

417行

$out = "<?php \n" . '$k = ' . preg_replace("/(\'\\$[^,]+)/e", "stripslashes(trim('\\1','\''));", var_export($t, true)) . ";\n";

修改为

$out = "<?php \n" . '$k = ' . preg_replace_callback("/(\'\\$[^,]+)/", function($r) { return stripslashes(trim($r[1],'\''));}, var_export($t, true)) . ";\n";

475行

$val = preg_replace("/\[([^\[\]]*)\]/eis", "'.'.str_replace('$','\$','\\1')", $val);

修改为

$val = preg_replace_callback("/\[([^\[\]]*)\]/is", function($r) { return '.'.str_replace('$','\$',$r[1]);}, $val);

912行

$pattern = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/se';

修改为

$pattern = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/s';

914行

$source = preg_replace($pattern, $replacement, $source);

修改为

$source =preg_replace_callback($pattern, function($matcheb){ return '{include file='.strtolower($matcheb[1]). '}';}, $source);

\mobile\include\common.php

662行

return ucfirst(preg_replace("/_([a-zA-Z])/e", "strtoupper('\\1')", $name));

修改为

return ucfirst(preg_replace_callback("/_([a-zA-Z])/", function($r) { return strtoupper($r[1]);}, $name));

另外一个函数相套使用需要修改的文件是 includes/lib_main.php
1330行
$ext = end(explode('.', $tmp));
修改为
$exta = explode('.', $tmp);$ext = end($exta);

还有php5.6版本弃用了split函数,需要修改,具体文件为goods.php

错误提示:

Function split() is deprecated Error File: \goods.php on line 362 

解决办法:
362行将
$cat_ar=split('/',$cat_name);
修改为
$cat_ar=explode('/',$cat_name);
需要正则表达式的就需要更换为preg_split函数,速度慢些。

 

修改了上面的这些问题后,前台的一般性警告或者错误提示就没有了,但是后台出现了蛮多问题。如下图

第一个,订单列表的订单状态为空,这个订单状态没有了很麻烦,不知道这个订单现在是什么状况。
错误状况:

在网上找了很多资料也没有找到解决办法,在CSDN上面有人提出了相同的问题,但是没有人回答,没办法,只能自己一个一个文件的定位。

最后找出问题是php升级之后,书写规范变了,所以取空值了。

解决办法是:
admin\templates\order_list.htm
修改

<td align="center" valign="top" nowrap="nowrap">{$lang.os[$order.order_status]},{$lang.ps[$order.pay_status]},{$lang.ss[$order.shipping_status]}</td>

<td align="center" valign="top" nowrap="nowrap">{$lang.os.$order.order_status},{$lang.ps.$order.pay_status},{$lang.ss.$order.shipping_status}</td>

引用变量的方式变了,修正之后就显示正常了。

第二个问题,单选项旁边的文字都没有了,没有文字说明。
错误状况:

解决办法是:
修改admin\templates\shop_config_form.htm文件
34行
{$var.display_options[$k]}</label>
修改为
{$var.display_options.$k}</label>

修改好之后就能正常显示了。

第三个问题,在后台还经常弹出来这个严格标准的提示,

错误提示:

Strict Standards: mktime(): You should be using the time() function instead in ......\admin\shop_config.php on line 32
Strict Standards: mktime(): You should be using the time() function instead in ......\admin\sms_url.php on line 31

解决办法:提示已经说的很清楚了

admin/sms_url.php on line 31
admin/shop_config.php on line 32

$auth = mktime();
修改为
$auth = time();

 

总结归纳一下,php环境升级到5.6以后,主要变化就是书写规则的改变和函数或者其修饰符的改变,比如preg_replace的/e修饰符在5.6就弃用了,split函数和mktime函数在5.6不被支持了。修正preg_replace这个问题时有一个诀窍,就是一对引号对应一个function,大括号收之前必须要加php语句结束符号英文状态的引号;,那么基本上就不会出错了。欢迎分享您发现的升级php5.6后ecshop存在的一些问题,一起进步,have a enjoy!

转载请注明:百蔬君 » 【原创文章】修正升级到PHP5.6后ecshop系统前后台出现的几个错误

喜欢 (38)or分享 (0)
发表我的评论
取消评论

请证明您不是机器人(^v^):

表情
(1)个小伙伴在吐槽
  1. 非常感谢!!
    匿名2022-06-30 22:43 回复