1706199973

[Tutorial] create short url with php


There are many times when we need to shorten the url of our sites or even sites that we need to share the url of. I've searched for several solutions and almost all of them haven't worked for me. So I decided to create my own solution and I want to share it with all of you. If you like this kind of content, sign up and leave your comment here. Thank you. ## <br>The function ```php function CreateShortURL($url, $redirect, $len $file) { $contents = @file_getcontents($file); $lines = explode(";", $content); $shorts = array(); $longs = array() ; if(strlen($contents)) foreach($lines as $line) if(strlen($line)) list($shorts[], $longs[]) = explode('|', $line); if(in_array($url, $longs)) for($j = 0; $j < count($longs); ++$j) if($longs[$j] == $url) return $redirect . "?u=" . $short[$j]; do $str = substr(md5(rand(0, 1000000)), 0, $len); while(in_array($str, $shorts)); file_put_contents($file, "$contents$str|$url;"); return $redirect . "?u=$str"; } ``` ## How to Use It To create a short URL, you need to pass four arguments to the function, like this: ```php $long = "https://playstationcouch.com/anime.php"; $redirect = "play.com"; $len = 7; $file = "shorturls.txt"; $result = CreateShortURL($long, $redirect, $len, $file); ``` So stop registering on websites just to shorten your urls. Get unlimited short urls quickly. In the next post, I'll show you how to use the urls we've shortened with this function. Leave your comments

(0) Comments

Welcome to Chat-to.dev, a space for both novice and experienced programmers to chat about programming and share code in their posts.

About | Privacy | Terms | Donate
[2024 © Chat-to.dev]