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:
<meta charset= "Utf-8"/>
<title>shorten url</title>
<body>
URL to IS shortened: (must include protocol like HTTP//or https://etc.) <br/>
<form method= "POST" >
<textarea rows= ' 3 ' name= "url" ></textarea><br/>
<input type= "Submit" value= "Submit"/>
<form><br/>
<?php
if (isset ($_post[' url ')) {
$origin = $_post[' url '];
if (strlen ($origin) > 10) {
$filename = count (Scandir ('. '))-3; Strip PHP self. ..
File_put_contents ($filename,
' <script type= ' text/javascript ">location.href=" '. $origin. ' " </script> ');
$shortened = "http://". $_server[' Http_host '].dirname ($_server[' php_self ']). ' /'. $filename;
Echo ' Original URL is<br/><a href= "'. $origin. '" > '. $origin. ' </a><br/> '
.' Shortened URL is<br/><a href= "'. $shortened. '" > '. $shortened. ' </a> ';
} else {
echo "The URL you entered was not valid.";
}
}
?>
</body>
To generate a file with a number as a filename:
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.