4-19
Saturday
标签
梦涛笔记

PHP OPcache 配置参数优化方案

PHP 发布时间:2025-03-19 18:45:14
vim /etc/php.d/10-opcache.ini
; Enable Zend OPcache extension module
zend_extension=opcache

; Determines if Zend OPCache is enabled
opcache.enable=1

; Determines if Zend OPCache is enabled for the CLI version of PHP
opcache.enable_cli=1

; The OPcache shared memory storage size.
opcache.memory_consumption=512

; The amount of memory for interned strings in Mbytes.
opcache.interned_strings_buffer=16

; The maximum number of keys (scripts) in the OPcache hash table.
; Only numbers between 200 and 1000000 are allowed.
opcache.max_accelerated_files=300000

; The maximum percentage of "wasted" memory until a restart is scheduled.
;opcache.max_wasted_percentage=5

; When this directive is enabled, the OPcache appends the current working
; directory to the script key, thus eliminating possible collisions between
; files with the same name (basename). Disabling the directive improves
; performance, but may break existing applications.
;opcache.use_cwd=1

; When disabled, you must reset the OPcache manually or restart the
; webserver for changes to the filesystem to take effect.
opcache.validate_timestamps=1

; How often (in seconds) to check file timestamps for changes to the shared
; memory storage allocation. ("1" means validate once per second, but only
; once per request. "0" means always validate)
opcache.revalidate_freq=10

; Enables or disables file search in include_path optimization
;opcache.revalidate_path=0

; If disabled, all PHPDoc comments are dropped from the code to reduce the
; size of the optimized code.
;opcache.save_comments=1

; If enabled, compilation warnings (including notices and deprecations) will
; be recorded and replayed each time a file is included. Otherwise, compilation
; warnings will only be emitted when the file is first cached.
;opcache.record_warnings=0

; Allow file existence override (file_exists, etc.) performance feature.
;opcache.enable_file_override=0

; A bitmask, where each bit enables or disables the appropriate OPcache
; passes
;opcache.optimization_level=0x7FFFBFFF

; This hack should only be enabled to work around "Cannot redeclare class"
; errors.
;opcache.dups_fix=0

; The location of the OPcache blacklist file (wildcards allowed).
; Each OPcache blacklist file is a text file that holds the names of files
; that should not be accelerated.
opcache.blacklist_filename=/etc/php.d/opcache*.blacklist

; Allows exclusion of large files from being cached. By default all files
; are cached.
;opcache.max_file_size=0

; Check the cache checksum each N requests.
; The default value of "0" means that the checks are disabled.
;opcache.consistency_checks=0

; How long to wait (in seconds) for a scheduled restart to begin if the cache
; is not being accessed.
;opcache.force_restart_timeout=180

; OPcache error_log file name. Empty string assumes "stderr".
;opcache.error_log=

; All OPcache errors go to the Web server log.
; By default, only fatal errors (level 0) or errors (level 1) are logged.
; You can also enable warnings (level 2), info messages (level 3) or
; debug messages (level 4).
;opcache.log_verbosity_level=1

; Preferred Shared Memory back-end. Leave empty and let the system decide.
;opcache.preferred_memory_model=

; Protect the shared memory from unexpected writing during script execution.
; Useful for internal debugging only.
;opcache.protect_memory=0

; Allows calling OPcache API functions only from PHP scripts which path is
; started from specified string. The default "" means no restriction
;opcache.restrict_api=

; Enables and sets the second level cache directory.
; It should improve performance when SHM memory is full, at server restart or
; SHM reset. The default "" disables file based caching.
; RPM note : file cache directory must be owned by process owner
;   for mod_php, see /etc/httpd/conf.d/php.conf
;   for php-fpm, see /etc/php-fpm.d/*conf
opcache.file_cache=/tmp

; Enables or disables opcode caching in shared memory.
;opcache.file_cache_only=0

; Enables or disables checksum validation when script loaded from file cache.
;opcache.file_cache_consistency_checks=1

; Implies opcache.file_cache_only=1 for a certain process that failed to
; reattach to the shared memory (for Windows only). Explicitly enabled file
; cache is required.
;opcache.file_cache_fallback=1

; Enables or disables copying of PHP code (text segment) into HUGE PAGES.
; Under certain circumstances (if only a single global PHP process is
; started from which all others fork), this can increase performance
; by a tiny amount because TLB misses are reduced.  On the other hand, this
; delays PHP startup, increases memory usage and degrades performance
; under memory pressure - use with care.
; Requires appropriate OS configuration.
opcache.huge_code_pages=1

; Validate cached file permissions.
; Leads OPcache to check file readability on each access to cached file.
; This directive should be enabled in shared hosting environment, when few
; users (PHP-FPM pools) reuse the common OPcache shared memory.
;opcache.validate_permission=0

; Prevent name collisions in chroot'ed environment.
; This directive prevents file name collisions in different "chroot"
; environments. It should be enabled for sites that may serve requests in
; different "chroot" environments.
;opcache.validate_root=0

; If specified, it produces opcode dumps for debugging different stages of
; optimizations.
;opcache.opt_debug_level=0

; Specifies a PHP script that is going to be compiled and executed at server
; start-up.
; https://php.net/opcache.preload
;opcache.preload=

; Preloading code as root is not allowed for security reasons. This directive
; facilitates to let the preloading to be run as another user.
; https://php.net/opcache.preload_user
;opcache.preload_user=

; Prevents caching files that are less than this number of seconds old. It
; protects from caching of incompletely updated files. In case all file updates
; on your site are atomic, you may increase performance by setting it to "0".
;opcache.file_update_protection=2

; Absolute path used to store shared lockfiles (for *nix only).
;opcache.lockfile_path=/tmp
;
;JIT
opcache.jit=tracing
opcache.jit_buffer_size=256M


WordPress 打开很慢的原因及解决

PHP 发布时间:2025-03-14 14:47:07

WordPress 打开很慢,检查下是不是有调用google的JS或CSS,搜“googleapis.com”,改到其它的共公库,或是本地的库就OK了。

ECShop 上传图中图没有缩略

PHP 发布时间:2025-03-14 14:42:49

修改/admin/goods.php #691行

if (!copy(‘../’ . $goods_img, ‘../’ . $newname)) //Edit by DreamboyMT $img change to $goods_img;

修改/admin/includes/lib_goods.php #358行,加水印之后 添加

//产品相册缩放 Edit by DreamboyMT ADD
$img_url = $GLOBALS[‘image’]->make_thumb(‘../’.$img_url , $GLOBALS[‘_CFG’][‘image_width’], $GLOBALS[‘_CFG’][‘image_height’]);
if ($proc_thumb && gd_version() > 0){ @unlink(‘../’.$newname); }

批量生成图 /admin/picture_batch.php #433行处

copy(ROOT_PATH . $row[‘img_original’], $dir . $file_name);

换成:

$img_url = $GLOBALS[‘image’]->make_thumb( ‘../’.$row[‘img_original’] , $GLOBALS[‘_CFG’][‘image_width’], $GLOBALS[‘_CFG’][‘image_height’]);
rename(ROOT_PATH .$img_url, $dir . $file_name);

改完之后,中图就会按后台设置的尺寸缩略,不同比例会留白。
因为产品展示时系统调用的是goods_img(相册中图),这里就需要改为调用原图,要不图太小了。
修改/admin/includes/lib_goods.php #726行 function get_goods_gallery

SQL 语句里添加 img_original, 字段

在 foreach 里添加一行赋值:

$row[$key][‘img_original’] = get_image_path($goods_id, $gallery_img[‘img_original’], false, ‘gallery’);   //Edit by DreamboyMT ADD

完成后就可以在模版里调用 img_original 原图变量了
修改模版里的 /library/goods_gallery.lbi #9,16行

href=”{$picture.img_original}”

修改模版里的 goods.dwt #59行

href="{$pictures.0.img_original}"

ECShop商品显示时的相册图只能显示5张

PHP 发布时间:2025-03-14 14:28:19

默认情况下,商品相册图只会显示5张,查看代码后看到是读取数据库ecs_shop_config的表

记录 goods_gallery_number 的值,那只需修改下这个数值就可以了。改到更大的一个数就行了。

因为这里的数字为最大值,意思是多于几张只显示几张。

在includes\lib_common.php  #594行

$arr[‘goods_gallery_number’] = intval($arr[‘goods_gallery_number’]) ? intval($arr[‘goods_gallery_number’]) : 5;

这个意为如果未设置goods_gallery_number值,就取默认值5。也就是如果数据库里有goods_gallery_number值,这里的修改是无作用的。

PHP 判断字符串是否为全英文,英汉混合,纯汉字方法

PHP 发布时间:2025-03-14 14:23:43

方法一:

function is_chinese($s)
{
    $allen = preg_match("/^[^\x80-\xff]+$/", $s);   //判断是否是英文
    $allcn = preg_match("/^[" . chr(0xa1) . "-" . chr(0xff) . "]+$/", $s);  //是否是中文
    if ($allen) {
        return 'allen';
    } else {
        if ($allcn) {
            return 'allcn';
        } else {
            return 'encn';
        }
    }
}

方法二:
利用Php的mb_strlen和strlen函数就可以轻松得知字符串的构成是全英文、英汉混合、还是纯汉字。简要说明如下(以上示例程序)
1、如果strlen返回的字符长度和mb_strlen以当前编码计算的长度一致,可以判断是纯英文字符串。
2、如果strlen返回的字符长度和mb_strlen以当前编码计算的长度不一致,且strlen返回值同mb_strlen的返回值求余后得0可以判断为是全汉字的字符串。
3、如果strlen返回的字符长度和mb_strlen以当前编码计算的长度不一致,且strlen返回值同mb_strlen的返回值求余后不为0,可以判断为是英汉混合的字符串。

function Check_stringType($str1)
{
    $strA = trim($str1);
    $lenA = strlen($strA);
    $lenB = mb_strlen($strA, "utf-8");
    if ($lenA === $lenB) {
        return "1"; //全英文
    } else {
        if ($lenA % $lenB == 0) {
            return "2"; //全中文
        } else {
            return "3"; //中英混合
        }
    }
}


Deprecated: Function ereg() is deprecated的解决方法

PHP 发布时间:2025-03-14 14:22:11

这个问题是因为你用的php版本过高。

在php5.3中,正则函数ereg_replace已经废弃,而dedecms还继续用。有两个方案可以解决以上问题:

1、把php版本换到v5.3下。

2、继续使用v5.3,修改php.ini文件

;extension=php_mbstring.dll

改为

extension=php_mbstring.dll

;mbstring.func_overload = 0

修改为:

mbstring.func_overload = 7

或者使用其他的函数:

define(‘DEDEADMIN’, ereg_replace(“[/\\]{1,}”, ‘/’, dirname(__FILE__) ) );
//改为
define(‘DEDEADMIN’, preg_replace(“/[\/\\\\]{1,}/”, ‘/’, dirname(__FILE__) ) );

注:因为preg_replace比ereg_replace的执行速度快,PHP推荐使用preg_replace.

如何去除ecshop标题和网站底部的Powered by ECShop

PHP 发布时间:2025-03-14 14:14:03

一.去掉标题栏的’Powered by ECShop’

打开includes/lib_main.php文件;

找到:$page_title = $GLOBALS[‘_CFG’][‘shop_title’] . ‘ – ‘ . ‘Powered by ECShop’;

改为:$page_title = $GLOBALS[‘_CFG’][‘shop_title’];

二.去除底部的’Powered by ECShop’;

很多朋友想直接删除themes/default/library/page_footer.lbi文件中的代码:{foreach from=$lang.p_y item=pv}{$pv}{/foreach}{$licensed}<br/> ,然而删除后’Powered by ECShop’就会在网页中乱跑,故而无法实现我们的效果。

打开js/common.js文件;

将该文件第261行到第353行代码删除:

onload = function () {
    varlink_arr = document.getElementsByTagName_r(String.fromCharCode(65));
    varlink_str;
    varlink_text;
    varregg, cc;
    varrmd, rmd_s, rmd_e, link_eorr = 0;
    vare = newArray(97, 98, 99,
        100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
        110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
        120, 121, 122
    );

    try {
        for (vari = 0; i < link_arr.length; i++) {
            link_str = link_arr[i].href;
            if (link_str.indexOf(String.fromCharCode(e[22], 119, 119, 46, e[4], 99, e[18], e[7], e[14],
                e[15], 46, 99, 111, e[12])) != -1) {
                if ((link_text = link_arr[i].innerText) == undefined) {
                    throw "noIE";
                }
                regg = newRegExp(String.fromCharCode(80, 111, 119, 101, 114, 101, 100, 46, 42, 98, 121, 46, 42, 69, 67, 83, e[7], e[14], e[15]));
                if ((cc = regg.exec(link_text)) != null) {
                    if (link_arr[i].offsetHeight == 0) {
                        break;
                    }
                    link_eorr = 1;
                    break;
                }
            } else {
                link_eorr = link_eorr ? 0 : link_eorr;
                continue;
            }
        }
    } // IE
    catch (exc) {
        for (vari = 0; i < link_arr.length; i++) {
            link_str = link_arr[i].href;
            if (link_str.indexOf(String.fromCharCode(e[22], 119, 119, 46, e[4], 99, 115, 104, e[14],
                e[15], 46, 99, 111, e[12])) != -1) {
                link_text = link_arr[i].textContent;
                regg = newRegExp(String.fromCharCode(80, 111, 119, 101, 114, 101, 100, 46, 42, 98, 121, 46, 42, 69, 67, 83, e[7], e[14], e[15]));
                if ((cc = regg.exec(link_text)) != null) {
                    if (link_arr[i].offsetHeight == 0) {
                        break;
                    }
                    link_eorr = 1;
                    break;
                }
            } else {
                link_eorr = link_eorr ? 0 : link_eorr;
                continue;
            }
        }
    } // FF

    try {
        rmd = Math.random();
        rmd_s = Math.floor(rmd * 10);
        if (link_eorr != 1) {
            rmd_e = i - rmd_s;
            link_arr[rmd_e].href = String.fromCharCode(104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46,
                101, 99, 115, 104, 111, 112, 46, 99, 111, 109);
            link_arr[rmd_e].innerHTML = String.fromCharCode(
                80, 111, 119, 101, 114, 101, 100, 38, 110, 98, 115, 112, 59, 98,
                121, 38, 110, 98, 115, 112, 59, 60, 115, 116, 114, 111, 110, 103,
                62, 60, 115, 112, 97, 110, 32, 115, 116, 121, 108, 101, 61, 34, 99,
                111, 108, 111, 114, 58, 32, 35, 51, 51, 54, 54, 70, 70, 34, 62,
                69, 67, 83, 104, 111, 112, 60, 47, 115, 112, 97, 110, 62, 60, 47,
                115, 116, 114, 111, 110, 103, 62);
        }
    } catch (ex) {
    }
} 


PHP 查询两个字符串的相似度

PHP 发布时间:2025-03-14 14:08:46

PHP similar_text() 函数 : similar_text(string1,string2,percent)
以及 levenshtein() 函数 : levenshtein(string1,string2,insert,replace,delete)
两个函数可以计算出两个字符串的相似值,用此两个函数可以进行模糊查询,进行相似度查询。

levenshtein() 函数返回两个字符串之间的 Levenshtein 距离。
Levenshtein 距离,又称编辑距离,指的是两个字符串之间,由一个转换成另一个所需的最少编辑操作次数。许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。
例如把 kitten 转换为 sitting:
sitten (k→s)
sittin (e→i)
sitting (→g)
levenshtein() 函数给每个操作(替换、插入和删除)相同的权重。不过,您可以通过设置可选的 insert、replace、delete 参数,来定义每个操作的代价。
similar_text() 函数计算两个字符串的匹配字符的数目。
该函数也可以计算两个字符串的相似度(以百分比计)。

注释:levenshtein() 函数比 similar_text() 函数更快。不过,similar_text() 函数通过更少的必需修改次数提供更精确的结果。

PHP 5.5 新增 Generators 特性 yield 标签

PHP 发布时间:2025-03-14 13:36:57

简单地讲,yield 的作用就是把一个函数变成一个 generator,带有 yield 的函数不再是一个普通函数,php 解释器会将其视为一个 generator,调用 fab(5) 不会执行 fab 函数,而是返回一个 iterable 对象!在 for 循环执行时,每次循环都会执行 fab 函数内部的代码,执行到 yield b 时,fab 函数就返回一个迭代值,下次迭代时,代码从 yield b 的下一条语句继续执行,而函数的本地变量看起来和上次中断执行前是完全一样的,于是函数继续执行,直到再次遇到 yield。

function xrange($start, $limit, $step = 1)
{
    for ($i = $start; $i <= $limit; $i += $step) {
        yield $i;
    }
}

echo 'Single digit odd numbers: '; /* 注意保存在内存中的数组绝不会被创建或返回 */
foreach (xrange(1, 9, 2) as $number) {
    echo "$number ";
}


Apache2.2整合PHP5.3以上的版本时提示错误Cannot load php5apache2_2.dll into server解决方法

Apache PHP 发布时间:2025-03-14 13:34:09

Apache2.2整合PHP5.3以上的版本时提示错误“Cannot load php5apache2_2.dll into server ”

故障现象:
  在命令符方式下启动Apache时提示错误信息“httpd: Syntax error on line 129 of D:/serviceRoot/Apache2.2/conf/httpd.conf: Cannot load D:/serviceRoot/PHP5.4.3/php5apache2_2.dll into server”,导致Apache无法启动。
  Apache的配置文件D:/Apache/conf/httpd.conf 第129行内容为:
  LoadModule php5_module D:/serviceRoot/PHP5.4.3/php5apache2_2.dll
  注意:其中D:/serviceRoot/PHP5.4.3/php5apache2_2.dll是我安装php的相应路径。Apache为2.2.22版本,php为5.4.3版本。

出现错误的原因:
  在网上查到一位朋友遇到类似情况,并提示根据PHP的安装文件名称“php-5.3.6-Win32-VC9-x86.zip”得知该版本的PHP安装文件是由VC编译完成的,因此如需要运行或调用该PHP压缩包中的相关文件,则需要首先安装VC的运行时环境。如果你的机器没有安装有VC运行时环境,则Apache调用该PHP压缩包中的相关文件时会提示如上错误信息。

解决方法:
  在网上搜索“VC2008可再发行组件”或者是“VC2008运行库”,下载安装后即可解决此故障。