Home > Backend Development > PHP Tutorial > Examples of methods to prevent collection of PHP encrypted URLs

Examples of methods to prevent collection of PHP encrypted URLs

小云云
Release: 2023-03-21 21:40:01
Original
2767 people have browsed it

This article mainly shares with you examples of methods to prevent collection of PHP encrypted URLs. I hope it can help everyone.

There are many collection tools on the Internet, how do they collect them.

These URLs are very regular and are composed of (blog-) + numbers. The collection uses this URL pattern to automatically collect web pages to a large extent.

How to better prevent the website from being collected? We can encrypt the (key number) part of the URL.

Provides encryption method, the generated encrypted URL is short, stable, non-random number encryption method, friendly to SEO. The algorithm does its own research.

/**
 * 加密数字方法
 *   echo idEncode(222);
 * @author uuleaf[<uuleaf#163.com>] 小叶
 * @param int $int 要加密的数字
 * @return string 加密后的字符串
 */
function idEncode($int)  
{  
    $str = md5($int);  
    $sarr = str_split($str);  
    $stai = (ord($str) + 8) % 10;  
    if ($stai == 0) $stai = 8;  
    $idstr = base_convert($int * $stai, 10, 32);  
    $str1 = substr($str, 10, 2);  
    $str2 = substr($str, 14, 2);  
    $str3 = substr($str, 18, 2);  
    return $str1 . $idstr . $str2 . $stai . $str3;  
}  

解密方法

/**
 * 解密数字方法
 *   echo idDncode("");
 * @author uuleaf[<uuleaf#163.com>] 小叶
 * @param string $str 要解密的数字
 * @return int 解密后的数字
 */
function idDecode($str)  
{  
    $idstr = substr(substr($str, 2), 0, -5);  
    $ji = base_convert($idstr, 32, 10);  
    $si = (int)substr($str, -3, -2);  
    return floor($ji / $si);  
}
Copy after login

Test

  1. echo "加密前的网址:http://thinkphp.cn/blog-54.html";  
    echo "\n";  
    $encode_str = idEncode(54);  
    echo "加密后的网址:http://thinkphp.cn/blog-{$encode_str}.html";  
    echo "\n";  
    $decode_str = idDecode($encode_str);  
    echo "还原后的网址:http://thinkphp.cn/blog-{$decode_str}.html";
    Copy after login


    Output result        

    Related recommendations:

    3 ways to implement data collection in PHP

    PHP method of writing WeChat public account article pages

    PHP uses curl to collect

    The above is the detailed content of Examples of methods to prevent collection of PHP encrypted URLs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template