PHP short URL super simple code

巴扎黑
Release: 2023-03-03 12:04:02
Original
1803 people have browsed it

Super simple code for php short URL
System environment:
php, apache2, linux

Operation to generate php short URL:
Copy the code to index.php and put it in a folder name with only 1 character (such as u) in the secondary directory.
Add write permissions to this directory. To save trouble, just chmod 777 u (the root directory is also OK, you may need to change the code to avoid affecting other files)

The URL generation result:
Put http://www.heimaolianmeng.com/ heimaoseojishu/ becomes http://127.0.0.1/u/1
The principle of php generating short URL:
1. Get the url to be shortened through form post
2. Put the url in a javascript and write it to the file. Names grow by numbers. The function of javascript is to jump to the specified url

Optimization:
If you can set a second-level domain name, just point the second-level domain name to that directory, and there is no need to enter an extra u/.

Code:


Code example:



Shorten URL

URL to be 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 is NOT valid.";
}
}
?>


Generate a file with a number as the file name:


Code example:

source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!