emlog 无插件实现网站源码压缩 二 教程
之前分享过《emlog 无插件实现网站源码压缩》,此方法需要修改模板中的相关代码,当切换模板后失效,每次都要修改模板,有些麻烦,所以提供以下代码来解决此问题。
在 include\lib\function.base.php 新增代码
function emHTMLCompression($html) { $chunks = preg_split('/(<!--<nocompress>-->.*?<!--<\/nocompress>-->|<nocompress>.*?<\/nocompress>|<pre.*?\/pre>|<textarea.*?\/textarea>|<script.*?\/script>)/msi', $html, -1, PREG_SPLIT_DELIM_CAPTURE); $compress = ''; foreach ($chunks as $c) { if (strtolower(substr($c, 0, 19)) == '<!--<nocompress>-->') { $c = substr($c, 19, strlen($c) - 19 - 20); $compress .= $c; continue; } else if (strtolower(substr($c, 0, 12)) == '<nocompress>') { $c = substr($c, 12, strlen($c) - 12 - 13); $compress .= $c; continue; } else if (strtolower(substr($c, 0, 4)) == '<pre' || strtolower(substr($c, 0, 9)) == '<textarea') { $compress .= $c; continue; } else if (strtolower(substr($c, 0, 7)) == '<script' && strpos($c, '//') != false && (strpos($c, "\r") !== false || strpos($c, "\n") !== false)) { // JS代码,包含“//”注释的,单行代码不处理 $tmps = preg_split('/(\r|\n)/ms', $c, -1, PREG_SPLIT_NO_EMPTY); $c = ''; foreach ($tmps as $tmp) { if (strpos($tmp, '//') !== false) { // 对含有“//”的行做处理 if (substr(trim($tmp), 0, 2) == '//') { // 开头是“//”的就是注释 continue; } $chars = preg_split('//', $tmp, -1, PREG_SPLIT_NO_EMPTY); $is_quot = $is_apos = false; foreach ($chars as $key => $char) { if ($char == '"' && $chars[$key - 1] != '\\' && !$is_apos) { $is_quot = !$is_quot; } else if ($char == '\'' && $chars[$key - 1] != '\\' && !$is_quot) { $is_apos = !$is_apos; } else if ($char == '/' && $chars[$key + 1] == '/' && !$is_quot && !$is_apos) { $tmp = substr($tmp, 0, $key); // 不是字符串内的就是注释 break; } } } $c .= $tmp; } } $c = preg_replace('/[\\n\\r\\t]+/', ' ', $c); // 清除换行符,清除制表符 $c = preg_replace('/\\s{2,}/', ' ', $c); // 清除额外的空格 $c = preg_replace('/>\\s</', '> <', $c); // 清除标签间的空格 $c = preg_replace('/\\/\\*.*?\\*\\//i', '', $c); // 清除 CSS & JS 的注释 $c = preg_replace('/<!--[^!]*-->/', '', $c); // 清除 HTML 的注释 $compress .= $c; } return $compress; }
修改 include\lib\view.php 代码
public static function output() { $content = ob_get_clean(); if (Option::get('isgzipenable') == 'y' && function_exists('ob_gzhandler')) { ob_start('ob_gzhandler'); } else { ob_start(); } echo $content; ob_end_flush(); exit; }
修改为
public static function output() { $content = ob_get_clean(); $initial = strlen($content); if (Option::get('isgzipenable') == 'y' && function_exists('ob_gzhandler')) { ob_start('ob_gzhandler'); } else { ob_start(); } $content_out = $content; $final = strlen($content_out); $savings = ($initial - $final) / $initial * 100; $savings = round($savings, 2); $content_out .= PHP_EOL . "<!--压缩前的大小: $initial bytes; 压缩后的大小: $final bytes; 节约:$savings% -->"; echo $content_out; ob_end_flush(); exit; }
以上部分代码源自菜鸟建站,感谢。
emlog 无插件实现网站源码压缩 教程
在以往的 emlog 优化教程中,相信都是使用的代码压缩插件,今天主要是分享插件的代码版本,也就是不使用插件,直接将代码写在 module.php 中就可以,好吧,又消灭一个插件!
以下代码是写在 module.php 里面
<?php function em_compress_html($buffer) { $initial = strlen($buffer); $buffer = explode("<!--em-compress-html-->", $buffer); $count = count($buffer); for ($i = 0; $i <= $count; $i++) { if (stristr($buffer[$i], '<!--em-compress-html no compression-->')) { $buffer[$i] = (str_replace("<!--em-compress-html no compression-->", " ", $buffer[$i])); } else { $buffer[$i] = (str_replace("\t", " ", $buffer[$i])); $buffer[$i] = (str_replace("\n\n", "\n", $buffer[$i])); $buffer[$i] = (str_replace("\n", "", $buffer[$i])); $buffer[$i] = (str_replace("\r", "", $buffer[$i])); while (stristr($buffer[$i], ' ')) { $buffer[$i] = (str_replace(" ", " ", $buffer[$i])); } } $buffer_out .= $buffer[$i]; } $final = strlen($buffer_out); $savings = ($initial - $final) / $initial * 100; $savings = round($savings, 2); $buffer_out .= PHP_EOL . "<!--压缩前的大小: $initial bytes; 压缩后的大小: $final bytes; 节约:$savings% -->"; return $buffer_out; } ?>
以下代码放在 footer.php 最末尾(即</html>结尾处)
<?php if (_g('compress_html') == 'open') { $html = ob_get_contents(); ob_get_clean(); echo em_compress_html($html); } ?>
以上的代码有一个模板设置判断语句,其代码为以下(放在 options.php 里面)
'compress_html' => array( 'type' => 'radio', 'name' => '网站源码压缩', 'description' => '将HTML的空格和空行删除,保留pre里面的格式,压缩输出的HTML~', 'values' => array('open' => '开启', 'close' => '关闭'), 'default' => 'open' ),
如果你想要实现不压缩 pre(就是网页中插入的代码,这样就能显示代码的排版)中的代码,要在 module.php 里面的加入下面代码。
<?php function unCompress($content) { if (preg_match_all('/(<pre|<\/pre>)/i', $content, $matches)) { $content = '<!--em-compress-html--><!--em-compress-html no compression-->' . $content; $content .= '<!--em-compress-html no compression--><!--em-compress-html-->'; } return $content; } ?>
然后找到模版文件夹下的 echo_log.php(文章内容页面)、page.php(评论页面)文件中的 $log_content 替换掉。
<?php echo $log_content; ?> // 替换为 <?php echo unCompress($log_content); ?>
不压缩 pre 的解决方法二,直接把第一步的 module.php 里面的 em_compress_html 修改一下
<?php function em_compress_html($buffer) { $initial = strlen($buffer); $buffer = preg_replace('/<pre/', '<!--em-compress-html--><!--em-compress-html no compression--><pre', $buffer); $buffer = preg_replace('/<\/pre/', '<!--em-compress-html no compression--><!--em-compress-html--></pre', $buffer); $buffer = explode("<!--em-compress-html-->", $buffer); $count = count($buffer); for ($i = 0; $i <= $count; $i++) { if (stristr($buffer[$i], '<!--em-compress-html no compression-->')) { $buffer[$i] = (str_replace("<!--em-compress-html no compression-->", " ", $buffer[$i])); } else { $buffer[$i] = (str_replace("\t", " ", $buffer[$i])); $buffer[$i] = (str_replace("\n\n", "\n", $buffer[$i])); $buffer[$i] = (str_replace("\n", "", $buffer[$i])); $buffer[$i] = (str_replace("\r", "", $buffer[$i])); while (stristr($buffer[$i], ' ')) { $buffer[$i] = (str_replace(" ", " ", $buffer[$i])); } } $buffer_out .= $buffer[$i]; } $final = strlen($buffer_out); $savings = ($initial - $final) / $initial * 100; $savings = round($savings, 2); $buffer_out .= PHP_EOL . "<!--压缩前的大小: $initial bytes; 压缩后的大小: $final bytes; 节约:$savings% -->"; return $buffer_out; } ?>
让 emlog 5.3.1 兼容 PHP7 环境一些处理方法分享 教程
直接在 php7 安装 emlog5.3.1 各种报错。emlog5.3.1 虽然已经出了使用 mysqli 连接类,但是为了兼容性还是默认是使用了 mysql。因为 PHP7 已经不支持 mysql 扩展了,但是支持 mysqli 和 pdo_mysql。所以这里还是介绍如何使用 mysqli 来安装 emlog。
以下是修改 emlog 安装程序,无报错安装。如果是实际环境请在本地环境模拟后成功后再更换。
1、修改 include\lib\option.php
const DEFAULT_MYSQLCONN = 'mysql'; 改为 const DEFAULT_MYSQLCONN = 'mysqli'; //默认链接方式改为mysqli
2、在 /include/lib/cache.php
$$row['option_name'] = $row['option_value']; 改为 ${$row['option_name']} = $row['option_value'];
3、在 admim/seo.php
$$t 改为 ${$t}
4、在 admim/views/admin_log.php
$$a $$b $$a 改为 ${$a} ${$b} ${$a}
5、在 admim/views/comment.php
$$a = "class=\"filter\""; 改为 ${$a} = "class=\"filter\"";
另外有些插件和主题是固定了使用mysql连接类,这样还需要修改插件和主题中的数据库连接方式,不然直接报数据库错误。
比如: $DB = MySql::getInstance(); 都要改为 $DB = Database::getInstance();
此教程参考网上很多资料,未做测试,仅供学习参考之用!!!
emlog SEO优化之URL统一 教程
在emlog模板制作初期就必须要考虑到路径规范化的问题,如果不注意就会造成危害:
1、搜索引擎在抓取网页的时候遇到多种路径的时候,他会自动选择其中一种路径作为标准,这个路径的选择也许不是你想让搜索引擎抓取的,平时做的外链用的都不是这个路径,这时候搜索引擎在给网页做排名的时候就会有一部分外链权重不计算在内。
2、多种路径会让本来就不熟悉网站的用户产生记忆模糊的问题,在一定程度上影响了用户体验。
所以,我们要力求同一个网页要有唯一的简单、短的路径,这样可以方便搜索引擎抓取和排名权重的集中,避免不必要的麻烦。同时这样的路径可以方便用户去记忆,从连接上满足用户体验。
emlog是一个php动态语言程序,用来做网站生成的数据网址是动态地址,如果使用了伪静态功能,一个页面地址会变为很多种,例如:
https://www.emlog.pro/code/2.html #开启伪静态后的url可以访问 https://www.emlog.pro/?post=2 #动态地址url也可以访问 https://www.emlog.pro/2 #这个url也可以访问
如上面的例子,这三个url网址访问的都是同一个页面,如果不做下url统一,百度收录这3个网址后,会导致重复内容,容易被百度降权甚至K了这个页面。
如何通过修改代码来实现,如标题所说的emlog SEO优化之URL统一,其实很简单,就是在页面头部增加
<link rel="canonical" href="网页权威链接" />
这样做的效果就是让百度知道这个url才是权威的网址;针对emlog的修改代码如下,请自己复制粘贴到模版文件header.php里之前就可以了。
#说明:编辑器打开模版文件夹下的header.php文件 #把以下代码粘贴到</head>之前 <?php if($logid): ?> <link href="<?php echo Url::log($logid);?>" rel="canonical" /> <?php elseif($sortid): ?> <link href="<?php echo Url::sort($sortid);?>" rel="canonical" /> <?php elseif($tag): ?> <link href="<?php echo Url::tag($params[2]);?>" rel="canonical" /> <?php elseif($record): ?> <link href="<?php echo Url::record($params[2]);?>" rel="canonical" /> <?php endif; ?>
emlog 去除分类目录前的sort,请先开启伪静态 教程
EMLOG使用伪静态后,分类URL网址中有个sort字样,这个让很多人着实不爽,都想把这个sort去掉。把sort去掉,直接显示网址/分类别名 的形式,这样有利于搜索引擎收录,也使网址更加简明。例如:
原网址:https://www.emlog.pro/sort/code 更改后:https://www.emlog.pro/code
修改教程
1.首先确认你的站点支持Rewrite,确认支持Rewrite后开启伪静态
2.修改include\lib\url.php文件,删除 sort/
$sortUrl = BLOG_URL . 'sort/' . $sort_index; 改成 $sortUrl = BLOG_URL . $sort_index; $sortUrl = BLOG_URL . 'sort/' . $sort_index . '/page/'; 改成 $sortUrl = BLOG_URL . $sort_index . '/page/';
3.修改include\lib\dispatcher.php文件中
return $path; 替换成 if($path!="/"&&substr($path,0,6)!="/sort/"&&substr($path,0,2)!="/?") { return "/sort".$path; } else { return $path; }
4.修改include\lib\dispatcher.php文件中
$path = str_ireplace('index.php', '', $path); 替换成 $path = str_ireplace('/index.php', '', $path);