6-4
Wednesday
标签
梦涛笔记

如何去除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运行库”,下载安装后即可解决此故障。

Imghash:相似图片搜索的php实现

PHP 发布时间:2025-03-13 20:00:52

前几天看到阮一峰的博客里写到关于相似图片搜索的原理, 里面介绍了Google相似图片搜索的大致原理,主要是从Neal Krawetz博士的LOOKS LIKE IT翻译而来。文章详细的介绍了相似图片搜索处理的相关步骤,并且给出了一个python的实现,由于我对PHP比较熟,所以写了个PHP的版本。
图片相似搜索的简单原理
根据文章里的描述,其实原理比较简单,大致有如下几个步骤:

1、缩小尺寸。将图片缩小到8×8的尺寸,总共64个像素。这一步的作用是去除图片的细节,只保留结构、明暗等基本信息,摒弃不同尺寸、比例带来的图片差异。

2、简化色彩。将缩小后的图片,转为64级灰度。也就是说,所有像素点总共只有64种颜色。

3、计算平均值。计算所有64个像素的灰度平均值。

4、比较像素的灰度。将每个像素的灰度,与平均值进行比较。大于或等于平均值,记为1;小于平均值,记为0。

5、计算哈希值。将上一步的比较结果,组合在一起,就构成了一个64位的整数,这就是这张图片的指纹。组合的次序并不重要,只要保证所有图片都采用同样次序就行了。得到指纹以后,就可以对比不同的图片,看看64位中有多少位是不一样的。

这种算法的优点是简单快速,不受图片大小缩放的影响,缺点是图片的内容不能变更。实际应用中,往往采用更强大的pHash算法和SIFT算法,它们能够识别图片的变形。只要变形程度不超过25%,它们就能匹配原图。

图片相似搜索的PHP实现
原文中给出了一个python版本的实现,代码只有53行。我写了个对应的PHP版本,由于直接是用类写的,所以代码有100行,具体如下:

class Imghash{
	
	private static $_instance = null;
	
	public $rate = 2;
	
	public static function getInstance(){
		if (self::$_instance === null){
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	public function run($file){
		if (!function_exists('imagecreatetruecolor')){
			throw new Exception('must load gd lib', 1);
		}
		$isString = false;
		if (is_string($file)){
			$file = array($file);
			$isString = true;
		}
		$result = array();
		foreach ($file as $f){
			$result[] = $this->hash($f);
		}
		return $isString ? $result[0] : $result;
	}
	public function checkIsSimilarImg($imgHash, $otherImgHash){
		if (file_exists($imgHash) && file_exists($otherImgHash)){
			$imgHash = $this->run($imgHash);
			$otherImgHash = $this->run($otherImgHash);
		}
		if (strlen($imgHash) !== strlen($otherImgHash)) return false;
		$count = 0;
		$len = strlen($imgHash);
		for($i=0;$i<$len;$i++){
			if ($imgHash{$i} !== $otherImgHash{$i}){
				$count++;
			}
		}
		return $count <= (5 * $rate * $rate) ? true : false;
	}
	public function hash($file){
		if (!file_exists($file)){
			return false;
		}
		$height = 8 * $this->rate;
		$width = 8 * $this->rate;
		$img = imagecreatetruecolor($width, $height);
		list($w, $h) = getimagesize($file);
		$source = $this->createImg($file);
		imagecopyresampled($img, $source, 0, 0, 0, 0, $width, $height, $w, $h);
		$value = $this->getHashValue($img);
		imagedestroy($img);
		return $value;
	}
	public function getHashValue($img){
		$width = imagesx($img);
		$height = imagesy($img);
		$total = 0;
		$array = array();
		for ($y=0;$y<$height;$y++){
			for ($x=0;$x<$width;$x++){
				$gray = ( imagecolorat($img, $x, $y) >> 8 ) & 0xFF;
				if (!is_array($array[$y])){
					$array[$y] = array();
				}
				$array[$y][$x] = $gray;
				$total += $gray;
			}
		}
		$average = intval($total / (64 * $this->rate * $this->rate));
		$result = '';
		for ($y=0;$y<$height;$y++){
			for ($x=0;$x<$width;$x++){
				if ($array[$y][$x] >= $average){
					$result .= '1';
				}else{
					$result .= '0';
				}
			}
		}
		return $result;
	}
	public function createImg($file){
		$ext = $this->getFileExt($file);
		if ($ext === 'jpeg') $ext = 'jpg';
		$img = null;
		switch ($ext){
			case 'png' : $img = imagecreatefrompng($file);break;
			case 'jpg' : $img = imagecreatefromjpeg($file);break;
			case 'gif' : $img = imagecreatefromgif($file);
		}
		return $img;
	}
	public function getFileExt($file){
		$infos = explode('.', $file);
		$ext = strtolower($infos[count($infos) - 1]);
		return $ext;
	}
}
require_once “Imghash.class.php”;
$instance = ImgHash::getInstance();
$result = $instance->checkIsSimilarImg(‘chenyin/IMG_3214.png’, ‘chenyin/IMG_3212.JPG’);

如果$result值为true, 则表明2个图片相似,否则不相似。

在实际的相似图片搜索中,算图片的指纹并不是难点,难点而是在怎么从海量的图片指纹里找出与之相似的指纹。


POST调试工具

HTML PHP 发布时间:2025-03-13 19:34:08

实际开发工作中,很多时候需要用到POST提交值,每次都需要针对写出相对的POST环境,

所以写出这个POST调试工具公共库,方便调试使用。

程序运行环境需要php 及 extension=php_curl.dll

调试时填写的内容:

提交的网址:填写需要调试的网址

提交的变量:分别填写变量名及变量值,可以添加多个变量,及多行(TEXT)变量

提交执行提供了正常执行,返回原码,解析JSON,解析XML 多种操作

可以将返回直接显示,也可以解析后直观显示

在线运行: https://www.zhisg.com/dev/post.php

验证URL字符串的正则

PHP JavaScript 发布时间:2025-03-13 19:31:17

验证URL字符串的正则

^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(\w+\.))*(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[\d]+)?(/[\w\&%_\./-~-\W]*)?$

在PHP中的写法为:

if(preg_match("@^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(\w+\.))*(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[\d]+)?(/[\w\&%_\./-~-\W]*)?$@i", $_url))


cURL超时设置

HTML PHP 发布时间:2025-03-13 19:28:34

一般我们访问HTTP方式很多,主要是:curl, socket, file_get_contents() 等方法。

如果碰到对方服务器一直没有响应的时候,我们就悲剧了,很容易把整个服务器搞死,所以在访问http的时候也需要考虑超时的问题。

CURL 是我们常用的一种比较靠谱的访问HTTP协议接口的lib库,性能高,还有一些并发支持的功能等。


curl_setopt($ch, opt) 可以设置一些超时的设置,主要包括:

*(重要) CURLOPT_TIMEOUT 设置cURL允许执行的最长秒数。

*(重要) CURLOPT_TIMEOUT_MS 设置cURL允许执行的最长毫秒数。

(在cURL 7.16.2中被加入。从PHP 5.2.3起可使用)


CURLOPT_CONNECTTIMEOUT 在发起连接前等待的时间,如果设置为0,则无限等待。

CURLOPT_CONNECTTIMEOUT_MS 尝试连接等待的时间,以毫秒为单位。如果设置为0,则无限等待。

(在cURL 7.16.2中被加入。从PHP 5.2.3开始可用)


CURLOPT_DNS_CACHE_TIMEOUT 设置在内存中保存DNS信息的时间,默认为120秒。


curl普通秒级超时:

$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL,$url);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);  
curl_setopt($ch, CURLOPT_TIMEOUT,60);   //只需要设置一个秒的数量就可以  
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);  

curl如果需要进行毫秒超时,需要增加:

curl_easy_setopt(curl, CURLOPT_NOSIGNAL,1L);  
//或者是:  
curl_setopt ( $ch,  CURLOPT_NOSIGNAL,true);//是可以支持毫秒级别超时设置的  

curl一个毫秒级超时的例子:

if(!isset($_GET['foo'])){  
        // Client  
        $ch = curl_init('http://example.com/');  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);  
        curl_setopt($ch, CURLOPT_NOSIGNAL,1);    //注意,毫秒超时一定要设置这个  
        curl_setopt($ch, CURLOPT_TIMEOUT_MS,200);  //超时毫秒,cURL 7.16.2中被加入。从PHP 5.2.3起可使用  
        $data = curl_exec($ch);  
        $curl_errno = curl_errno($ch);  
        $curl_error = curl_error($ch);  
        curl_close($ch);  
   
        if($curl_errno >0){  
                echo "cURL Error ($curl_errno): $curl_error\n";  
        }else{  
                echo "Data received: $data\n";  
        }  
}else{  
        // Server  
        sleep(10);  
        echo "Done.";  
}  

其他一些技巧:

1. 按照经验总结是:cURL 版本 >= libcurl/7.21.0 版本,毫秒级超时是一定生效的,切记。

2. curl_multi的毫秒级超时也有问题。。单次访问是支持ms级超时的,curl_multi并行调多个会不准


XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin

HTML PHP 发布时间:2025-03-13 19:16:28

解决办法:

1.恳求的url是PHP的,须要PHP中

echo header("Access-Control-Allow-Origin: *");

2.html的,须要在head头加

<meta http-equiv="Access-Control-Allow-Origin" content="*">

这样就可以解决Ajax跨域访问问题。

PHP PECL 扩展库安装

PHP CentOS 发布时间:2025-03-13 14:41:46

https://pecl.php.net/

PECL is a repository for PHP Extensions, providing a directory of all known extensions

搜索下载对应的PHP扩展库原码
#如果不安装php-devel就不会有phpize和php-config

/usr/bin/phpize              # php安装后的路径(不知道位置的可以whereis phpize 查看位置)
./configure --with-php-config=/usr/bin/php-config
make && make install

修改php.ini文件 , 增加如下内容:

extension=XXX.so  #增加对应的扩展库名

重启php-fpm: 或httpd

systemctl restart php-fpm
OR
systemctl restart httpd