Practical analysis of php development (9): Use solutions to realize short address sharing (third-party short link service, database auto-increment ID conversion, custom short address generation algorithm, auto-increment digital short code)

Php development combat analysis series catalog

  1. Practical analysis of php development (1): mysql operation field (add, delete, modify, add multiple fields in multi-data table)
  2. Practical analysis of php development (2): dynamic use of cookies (setting, acquisition, deletion, guessing you like the principle, shopping cart call)
  3. Practical analysis of php development (3): the method of judging that the variable is empty (0, false, null) in php
  4. Practical analysis of php development (4): php call encapsulation function contains file path adaptive solution for different directories ($_SERVER['DOCUMENT_ROOT'] and __DIR__ magic constant)
  5. Practical analysis of php development (5): file and directory operations
  6. Practical Analysis of PHP Development (6): Generation and Calling of Configuration Files or Cache Files
  7. Practical analysis of php development (7): advanced use of mysql library operations
  8. Practical analysis of php development (8): advanced use of API development for the operation of mysql class library (field operation, permission assignment, date format, cross-table query, data sub-table)

1. Why use short addresses

The main purpose of using short URLs is to make long URLs more concise, easy to remember and easy to share. Here are some common causes:

  1. Beautiful and concise: Long URLs usually contain multiple characters and parameters, while short URLs can simplify it to a few characters, making the URL shorter and neater. This can improve the user experience and make the website more beautiful.

  2. Easy to remember: Long and complex URLs are often difficult to remember, especially for users on mobile devices. The short address usually uses a concise combination of characters, which is easier to remember.

  3. Easy to share: Short addresses are more suitable for sharing in a limited space, such as social media, SMS, chat apps, etc. Long addresses may be truncated or line-wrapped, resulting in failure to open the target page correctly, while short addresses can avoid this problem.

  4. Hiding the real address: For some sensitive information or content that needs to be kept secret, using a short address can hide the real target URL. This can add a certain amount of security and prevent users from obtaining sensitive information by directly accessing long addresses.

  5. URL statistics and monitoring: Using the statistics function provided by the short address service provider, you can track the click volume, conversion rate and other data of the short address, which is convenient for data analysis and monitoring. This has important implications for marketing campaigns, promotional links, etc.

In short, using short addresses can improve user experience, simplify URLs, and facilitate sharing and statistics, so it can be considered in many scenarios.

2. The way to realize the short address

  1. Self-incrementing digital short code: Based on the hash algorithm, generate a short string from the long link, and save the short string and long link in the database. To obtain the short link, query the database through the short string to obtain the corresponding long link, and then redirect the user to the long link.

  2. Database auto-increment ID conversion: convert the ID of the long link into 62, and then combine it with the preset domain name to form a short link. When obtaining the short link, obtain the long link through the 62-bit ID in the short link, and then redirect the user to the long link.

  3. Use third-party short link services, such as bit.ly, goo.gl, etc. Submit the long link to the service provider, get the short link, and store it in the database. To obtain the short link, redirect the user to the service provider's interface through the short link, and let it redirect the user to the long link.

  4. Based on your own short link service, use a distributed ID generator (such as Snowflake) to generate a unique ID and convert it into a short link. When obtaining the short link, obtain the long link according to the unique ID in the short link, and redirect the user to the long link.

3. Practical development example

1. Short address usage instructions

Format: https://test.com/t.php?t=21_1

/*
 * 短地址系统
 * 为方便微信内传播,直接使用ID进行短域名缩写
 * 参数方式:$Id_0
 * $Id为加密方式密文
 * _分割符号
 * 0,项目链接,1,培训链接,2单位邀请注册
 * By Woodcutter 2023-05-17
 */

2. Get parameters

$t = get_param('t');
$pid = explode("_", $t)[0];
$fromUrl = explode("_", $t)[1];
if (stripos($t, '_') == false) {
    
    
    die("参数错误,无法正常访问");
} 

3. Jump condition judgment

 //判断跳转;
    switch ($fromUrl) {
    
    
        case "0";//项目分享
            $row = $db->fetch('project', 'project_id', array('project_id' => $pid), ' project_id DESC');
            if ($row['project_id']) {
    
    
                $url = 'app.php?m=Course&a=courseList&act=list&project_id=' . $row['project_id'];
            } else {
    
    
                die("项目信息不存在,无法访问");
            }
            break;
            
        case "1";//培训分享
            $row = $db->fetch('course', 'project_id,course_id', array('course_id' => $pid), ' course_id DESC');
            if ($row['course_id']) {
    
    
                $url = 'app.php?m=Course&a=courseDetails&act=show&project_id=' . $row['project_id'] . '&course_id=' . $row['course_id'];
            } else {
    
    
                die("培训信息不存在,无法访问");
            }
            break;
            
        case "2";//PC端单位分享
            $row = $db->fetch('depart', 'depart_id,depart_name,depart_no,province,city,area', array('depart_id' => $pid), ' depart_id DESC');
            if ($row['depart_id']) {
    
    
                $url = 'index.php?m=Index&a=register&depart_id=' . $row['depart_id'] . '&user_depart=' . UrlEncode($row['depart_name']) . '&depart_no=' . UrlEncode($row['depart_no']) . '&province=' . UrlEncode($row['province']) . '&city=' . UrlEncode($row['city']) . '&area=' . UrlEncode($row['area']);
            } else {
    
    
                die("单位信息不存在,无法访问");
            }
            break;
            
        case "3";//手机端单位分享
            $row = $db->fetch('depart', 'depart_id,depart_name,depart_no,province,city,area', array('depart_id' => $pid), ' depart_id DESC');
            if ($row['depart_id']) {
    
    
                $url = 'app.php?m=Index&a=register&depart_id=' . $row['depart_id'] . '&user_depart=' . UrlEncode($row['depart_name']) . '&depart_no=' . UrlEncode($row['depart_no']) . '&province=' . UrlEncode($row['province']) . '&city=' . UrlEncode($row['city']) . '&area=' . UrlEncode($row['area']);
            } else {
    
    
                die("单位信息不存在,无法访问");
            }
            break;
            
        case "4";//签到二维码分享
            $row = $db->fetch('course', 'project_id,course_id', array('course_id' => $pid), ' course_id DESC');
            if ($row['course_id']) {
    
    
                $url = 'app.php?m=Course&a=courseCheck&act=check&project_id=' . $row['project_id'] . '&course_id=' . $row['course_id'];
            } else {
    
    
                die("培训信息不存在,无法访问");
            }
            break;
            
        default:
            die("短域名参数错误,无法正常访问。");

@ Leak sometimes

Guess you like

Origin blog.csdn.net/weixin_41290949/article/details/131743960