聯(lián)系官方銷售客服
1835022288
028-61286886
整合Hashids,用于生成類似YouTube的Id,防爬蟲。Hashids是一個(gè)小型的開放源代碼庫,可以將數(shù)字生成很短的、唯一的、非順序的字符ID。 例如可以數(shù)字347轉(zhuǎn)換為 “yr8” 字符串,你還可以將字符串ID進(jìn)行解碼恢復(fù)成數(shù)字。
1、首先到Hashids官網(wǎng)下載PHP版本,開源地址:https://github.com/vinkla/hashids
2、在dayrui目錄下新建目錄ThirdParty
3、將下載好的Hashids上傳至ThirdParty目錄下
4、修改文件HashGenerator.php和Hashids.php
namespace Hashids; 修改為 namespace Phpcmf\ThirdParty\Hashids;
5、添加自定義函數(shù)。config/custom.php
function eos_hashids( $id = '' , $operation = 'DECODE' , $key = 'G2cOwnsHcjnJAayk' , $length = '16'){ $hashids = new \Phpcmf\ThirdParty\Hashids\Hashids($key, $length); if( $operation == 'DECODE' ){ return intval($hashids->decode( $id )[0] ? $hashids->decode( $id )[0] : '0'); }else if( $operation == 'ENCODE' ){ return $hashids->encode($id); }else{ return; } }
具體配置請(qǐng)看官網(wǎng),只需要知道operation和key和length,一個(gè)是密鑰,一個(gè)是加密長(zhǎng)度,DECODE是解密,ENCODE是加密
這個(gè)時(shí)候可以進(jìn)行測(cè)試了,在模板頁面添加
{eos_hashids('100','ENCODE')} <br> {eos_hashids('bQ58jBGvwNEVWp63','DECODE')}
不出意外就可以得到
bQ58jBGvwNEVWp63 100
6、添加加密url的函數(shù),config/custom.php
// 詳情ID function eos_hashids_id($data){ return eos_hashids($data['id'],'ENCODE'); } // 欄目ID function eos_hashids_catid($data){ return eos_hashids($data['id'],'ENCODE'); }
7,修改URL規(guī)則
8、更新緩存,重新生成內(nèi)容地址,這個(gè)時(shí)候就得到了帶有加密地址的url了,這個(gè)時(shí)候訪問url會(huì)出現(xiàn)404,因?yàn)檫€差最后一步解密
9、修改模塊App / Article / Controllers / 目錄下的 Category.php ,15行
(int)\Phpcmf\Service::L('Input')->get('id'), 修改為 (int)eos_hashids(\Phpcmf\Service::L('Input')->get('id'),'DECODE'),
10、修改模塊App / Article / Controllers / 目錄下的 Show.php ,13行
(int)\Phpcmf\Service::L('Input')->get('id'), 修改為 (int)eos_hashids(\Phpcmf\Service::L('Input')->get('id'),'DECODE'),
到此為止,ID加密就算是完成了,返回頁面刷新試試。使用此函數(shù)可以進(jìn)行很多加密,具體玩法可以自行研究。
看看隱藏了啥
偽靜態(tài)有點(diǎn)問題
回復(fù)@自牧
解決404
回復(fù)@Adai
這個(gè)很實(shí)用,感謝分享
可否改為共享模塊的
多模塊的如何操作
能不能參照官方給的
// 判斷url是否是來自自定義函數(shù)
if (CMSURI) {
$myfile = WRITEPATH.'myid/'.md5(urldecode(CMSURI)).'.txt';
if (is_file($myfile)) {
$id = eos_hashids('file_get_contents($myfile)','ENCODE');
if ($id) {
return [
CMSURI => 'index.php?c=show&id='.$id, // 這里寫內(nèi)容的地址
];
}
}
}
變?yōu)楣蚕砟K通用
學(xué)習(xí)學(xué)習(xí),剛好需要