Table of Contents
Short URL exploration, URL exploration
Introduction
Principle
Encoding
Results
Home Backend Development PHP Tutorial Short URL exploration, URL exploration_PHP tutorial

Short URL exploration, URL exploration_PHP tutorial

Jul 13, 2016 am 10:04 AM
http under commonplace introduction Explore short URL

Short URL exploration, URL exploration

Introduction

Short URLs are common, such as the following

 http://dwz.cn/CSW6Y => http://www.cnblogs.com/iforever/p/4313704.html,

 http://dwz.cn/CSWuP => http://www.cnblogs.com/iforever/p/4279006.html, etc.

When accessing these URLs, the front and back pairs point to the same page. I am just giving an example. These short URLs are often seen in weibo or similar social networks. Since the original link address is particularly long, this time the short URL is short. The URL will show its power and be short and easy to remember, but it also has its shortcomings. For example, short URLs may not directly see some information in the URL (some information can be seen in long URLs).

I have been exposed to this all day long, so that before I really understand its principle, it has imprinted "nothing" in my mind. This has missed some knowledge. This situation should be called in psychology. I'm thinking, does anyone know that this is the case?

Principle

First of all, I guess this is achieved through redirection. dwz.cn is a server used to receive some short URLs. These short URLs are processed by dwz.cn and have corresponding short URLs in dwz.cn. The record of the long URL, then obtain the long URL corresponding to the short URL from the database, and then redirect with 302. See if this is the case.

Visit http://dwz.cn/CSW6Y through the browser and analyze the request (use Chrome’s debugging tool here, or you can use tools such as wireshark), and you can get the following data:

Short URL exploration, URL exploration_PHP tutorialname: abit version: 1 handle: - rewrite: if (!-d && !-f && path ~ "/(.*)$") goto "do.php?url=$1&act=out"

Handle: The following are the rewrite rules. For the specific detailed rules of Sina Cloud, please see here http://sae.sina.com.cn/doc/php/runtime.html#php-app-config. It is very simple. The configurations of nginx and apache are also similar.

Another thing to note after the redirection is completed is that urlencode must be performed when passing parameters, and urldecode must be used before redirection. When url type parameters are passed without using urlencode, part of the information may be lost when obtained. Therefore, when passing parameters before generating a short link, the URL must be escaped, the special string must be encoded, and the short link must be processed when accessing. It is necessary to urldecode the encoded URL and restore it to a normal link. Otherwise, the link will not be regarded as a normal URL when the header jumps. After the jump, the URL will be appended to the host of the previous page, similar to http://abit.sinaapp.com/www.cnblogs.com, errors may occur, so please pay special attention here.

There will be a problem when sae redirects, double backslashes will be automatically filtered into one, for example from http://abit.sinaapp.com/ to http:/abit.sinaapp.com/, please note , there is a missing backslash here, you should pay special attention to this when processing, otherwise you may encounter unnecessary trouble.

Encoding

Main processing part

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

<?<span>php

</span><span>class</span><span> snapshotUrl{

    </span><span>//</span><span>进行编码的数据库,没6位二进制数对应一个字符,一共需要64位,因此选取

    //52+10+2个特殊字符</span>

    <span>private</span> <span>static</span> <span>$basedb</span> = <span>array</span><span>(

        </span>'(',')','a','b','c','d',

        'e','f','g','h','i','j',

        'k','l','m','n','o','p',

        'q','r','s','t','u','v',

        'w','x','y','z','A','B',

        'C','D','E','F','G','H',

        'I','J','K','L','M','N',

        'O','P','Q','R','S','T',

        'U','V','W','X','Y','Z',

        '0','1','2','3','4','5',

        '6','7','8','9',<span>

    );

 

    </span><span>private</span> <span>function</span> long2short(<span>$url</span><span>){

        </span><span>$hex</span> = <span>md5</span>(<span>$url</span><span>);

        </span><span>$out</span> = ''<span>;

        </span><span>$hex</span> = 0x7FFFFFFF & (1 * ('0x'.<span>substr</span>(<span>$hex</span>, 0, 8<span>)));

        </span><span>for</span>(<span>$i</span>=0; <span>$i</span><5; <span>$i</span>++<span>){

            </span><span>$index</span> = 0x3f & <span>$hex</span><span>;

            </span><span>$out</span> .= self::<span>$basedb</span>[<span>$index</span><span>];

            </span><span>$hex</span> = <span>$hex</span>>>6<span>;

        }

        </span><span>return</span> <span>$out</span><span>;

    }

 

    </span><span>public</span> <span>function</span> retJson(<span>$arr</span><span>){

        </span><span>return</span> json_encode(<span>$arr</span><span>);

    }

 

    </span><span>//</span><span>对url进行映射保存</span>

    <span>public</span> <span>function</span> dispose(<span>$url</span>, <span>$act</span><span>){

        </span><span>$mysql</span> = <span>new</span><span> SaeMysql();

        </span><span>switch</span> (<span>$act</span><span>) {

            </span><span>case</span> 'in':

                <span>$short</span> = <span>$this</span>->long2short(<span>$url</span><span>);

                </span><span>$url</span> = <span>addslashes</span>(<span>$url</span><span>);

                </span><span>$sql</span> = "insert into `tiny_url`(`short`,`long`) values ('{<span>$short</span>}','{<span>$url</span>}')"<span>;

                </span><span>$mysql</span>->runSql(<span>$sql</span><span>);

                </span><span>if</span>(<span>$mysql</span>->errno() != 0<span>){

                    </span><span>echo</span> "生成失败"<span>;

                }</span><span>else</span><span>{

                    </span><span>echo</span> "http://abit.sinaapp.com/{<span>$short</span>}"<span>;

                }

                </span><span>break</span><span>;

            </span><span>case</span> 'out':

                <span>if</span>(<span>strlen</span>(<span>$url</span>) > 5<span>)

                    </span><span>echo</span> <span>$this</span>->retJson(<span>array</span>("code"=>"-1","msg"=>"没有这条记录"<span>));

                </span><span>$sql</span> = "select * from `tiny_url` where `short`='{<span>$url</span>}' limit 1"<span>;

                </span><span>$data</span> = <span>$mysql</span>->getData(<span>$sql</span><span>);

                </span><span>if</span>(!<span>$data</span><span>) {

                    </span><span>echo</span> <span>$this</span>->retJson(<span>array</span>("code"=>"-1","msg"=>"没有这条记录"<span>));

                }</span><span>else</span><span>{

                    </span><span>$location</span> = <span>urldecode</span>(<span>$data</span>[0]['long'<span>]);

                    </span><span>header</span>("Location: {<span>$location</span>}"<span>);

                    </span><span>exit</span><span>();

                }

                </span><span>break</span><span>;

             

            </span><span>default</span>:

                <span>#</span><span> code...</span>

                <span>break</span><span>;

        }

    }   

}

 

</span><span>$url</span> = <span>isset</span>(<span>$_GET</span>['url']) ? <span>$_GET</span>['url'] : <span>null</span><span>;

</span><span>$act</span> = <span>isset</span>(<span>$_GET</span>['act']) ? <span>$_GET</span>['act'] : <span>null</span><span>;

</span><span>$snapshotUrl</span> = <span>new</span><span> snapshotUrl();

</span><span>if</span>(<span>$url</span> === <span>null</span> || <span>$act</span> === <span>null</span><span>)

    </span><span>echo</span> <span>$snapshotUrl</span>->retJson(<span>array</span>("code"=>"-1","msg"=>"参数错误"<span>));

 

</span><span>$snapshotUrl</span>->dispose(<span>$url</span>, <span>$act</span>);

Copy after login

Results

I made a small webpage that can be tested:

 http://abit.sinaapp.com/ If you are interested, you can try it

The copyright of this article belongs to the author iforever (luluyrt@163.com). Any form of reprinting is prohibited without the author’s consent. After reprinting the article, the author and the original text link must be given in an obvious position on the article page. Otherwise, we reserve the right to pursue legal liability.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/965023.htmlTechArticleShort URL Research, URL Research Introduction Short URLs are common, such as the following http://dwz.cn/CSW6Y =http://www.cnblogs.com/iforever/p/4313704.html, http://dwz.cn/CSWuP=http:/...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Python package manager sinkhole pitfalls: how to avoid them Python package manager sinkhole pitfalls: how to avoid them Apr 01, 2024 am 09:21 AM

The python package manager is a powerful and convenient tool for managing and installing Python packages. However, if you are not careful when using it, you may fall into various traps. This article describes these pitfalls and strategies to help developers avoid them. Trap 1: Installation conflict problem: When multiple packages provide functions or classes with the same name but different versions, installation conflicts may occur. Response: Check dependencies before installation to ensure there are no conflicts between packages. Use pip's --no-deps option to avoid automatic installation of dependencies. Pitfall 2: Old version package issues: If a version is not specified, the package manager may install the latest version even if there is an older version that is more stable or suitable for your needs. Response: Explicitly specify the required version when installing, such as p

How to implement HTTP streaming using C++? How to implement HTTP streaming using C++? May 31, 2024 am 11:06 AM

How to implement HTTP streaming in C++? Create an SSL stream socket using Boost.Asio and the asiohttps client library. Connect to the server and send an HTTP request. Receive HTTP response headers and print them. Receives the HTTP response body and prints it.

Getting Started with Java Git: A Beginner's Guide to Version Control Getting Started with Java Git: A Beginner's Guide to Version Control Mar 27, 2024 pm 02:21 PM

A version control system (VCS) is an indispensable tool in software development that allows developers to track and manage code changes. git is a popular and powerful VCS that is widely used in Java development. This guide will introduce the basic concepts and operations of Git, providing Java developers with the basics of version control. The basic concept of Git Repository: where code and version history are stored. Branch: An independent line of development in a code base that allows developers to make changes without affecting the main line of development. Commit: A change to the code in the code base. Rollback: Revert the code base to a previous commit. Merge: Merge changes from two or more branches into a single branch. Getting Started with Git 1. Install Git Download and download from the official website

The philosophy of Java file operations: understanding the nature of files The philosophy of Java file operations: understanding the nature of files Mar 21, 2024 pm 03:20 PM

Files are the basic unit of information storage and management in computer systems, and are also the core focus of Java file operations. Understanding the nature of files is critical to operating and managing them effectively. Abstraction and Hierarchy A file is essentially an abstraction that represents a set of data stored in a persistent medium such as disk or memory. The logical structure of a file is usually defined by the operating system and provides a mechanism for organizing and accessing data. In Java, files are represented by the File class, which provides abstract access to the file system. Data Persistence One of the key characteristics of a file is its data persistence. Unlike data in memory, data in files persists even after the application exits. This persistence makes files useful for long-term storage and sharing of information.

The future of concurrent collections in Java: Exploring new features and trends The future of concurrent collections in Java: Exploring new features and trends Apr 03, 2024 am 09:20 AM

With the rise of distributed systems and multi-core processors, concurrent collections have become crucial in modern software development. Java concurrent collections provide efficient and thread-safe collection implementations while managing the complexity of concurrent access. This article explores the future of concurrent collections in Java, focusing on new features and trends. New feature JSR354: Resilient concurrent collections jsR354 defines a new concurrent collection interface with elastic behavior to ensure performance and reliability even under extreme concurrency conditions. These interfaces provide additional features of atomicity, such as support for mutable invariants and non-blocking iteration. RxJava3.0: Reactive Concurrent Collections RxJava3.0 introduces the concept of reactive programming, enabling concurrent collections to be easily integrated with reactive data flows.

Teach you how to use PHP to remove the suffix of the URL path Teach you how to use PHP to remove the suffix of the URL path Mar 21, 2024 pm 03:39 PM

Teach you how to use PHP to remove the suffix of the URL path. In website development, you often encounter the need to remove the suffix of the URL path to achieve a more beautiful and standardized URL. Today we will learn how to use PHP to remove the suffix of the URL path, let us explore this issue together. First, we need to clarify what effect we want to achieve. Usually, the URL path suffix refers to the file extension in the URL, such as .php, .html, etc. Our goal is to automatically go to the URL when the user accesses the URL with the suffix

Python and Jython: Unlocking the potential of cross-platform development Python and Jython: Unlocking the potential of cross-platform development Mar 18, 2024 pm 10:20 PM

Cross-platform development is crucial to building applications that run seamlessly on multiple operating systems simultaneously. Python and Jython, as leading programming languages, provide powerful solutions for cross-platform development and unleash their potential. Python’s cross-platform compatibility Python is an interpreted language that uses a virtual machine to execute code. This architecture allows Python code to run on multiple platforms, including Windows, Linux, MacOS, and mobile devices. Python’s broad platform support makes it ideal for building cross-platform applications. In addition, Python has a rich ecosystem of third-party libraries that provide a wide range of functions, from data processing and WEB development to machine learning and data

Demystifying the PHP Phar Extension: Empowering Dynamic PHP Applications Demystifying the PHP Phar Extension: Empowering Dynamic PHP Applications Mar 25, 2024 am 09:01 AM

The PHPPhar extension provides PHP developers with a powerful and flexible way to package scripts, libraries and resources. By creating Phar archives, developers can easily deploy and distribute complex applications without worrying about file system permissions or dependencies. Advantages of Phar files Single-file deployment: Phar archives package all components of an application into a single file for easy distribution and deployment. Independence: Phar files are independent of the file system, so the application can run on any machine with a PHP interpreter. Code Protection: Phar files can be encrypted to protect their contents from unauthorized access. Version Control: The Phar extension provides version control support for Phar files, allowing developers to track

See all articles