PHP short URL Super simple code PHP reference URL code baidu short URL short url generation

Source: Internet
Author: User
Tags form post
PHP short URL Super simple code
System environment:
Php,apache2,linux
To generate the PHP short URL operation:
Copy the code to index.php in a two-level directory with only 1 characters (such as U) as the folder name.
To add write permissions for this directory, the diagram chmod 777 U (root directory also line, in order to avoid affecting other files may have to change the code)
URL Generation results:
Turn http://www.heimaolianmeng.com/heimaoseojishu/into HTTP://127.0.0.1/U/1.
How PHP generates a short URL:
1, get the URL to be shortened by Form Post
2, put the URL in a JavaScript to write to the file, the file name growth by number. The role of JavaScript is to jump to the specified URL
Can be optimized:
If you can set a two-level domain name, then the two-level domain name point to that directory, you do not have to enter a u/.
Code:
code example:



<title>Shorten URL</title>


URL to IS shortened: (must include protocol like HTTP//or https://etc.)







if (isset ($_post[' url ')) {
$origin = $_post[' url '];
if (strlen ($origin) > 10) {
$filename = count (Scandir ('. '))-3; Strip PHP self. ..
File_put_contents ($filename,
'');
$shortened = "http://". $_server[' Http_host '].dirname ($_server[' php_self ']). ' /'. $filename;
Echo ' Original URL is
'. $origin. '
'
.' Shortened URL is
'. $shortened. ';
} else {
echo "The URL you entered was not valid.";
}
}
?>


To generate a file with a number as a filename:
code example:

The above introduces the PHP short URL ultra-simple code, including the short URL, php content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.