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.