1706358648

Part two [Tutorial] create short url with php


In order to have a more complete code for this post [click here](https://chat-to.dev/post?id=46), I've decided to publish part two of how we can use the short url we created with the passed function. ```php function UseShortURL($token, $file){ $contents = @file_get_contents($file); $lines = explode(";", $contents); $shorts = array(); $longs = array(); if(strlen($contents)) foreach($lines as $line) if(strlen($line)) list($short[], $long[]) = explode('|', $line); if(in_array($token, $shorts)) if($j =0; $j < count($longs); ++$j) if($shorts[$j] == $token) return $longs[$j]; return FALSE; } ``` ## <br>How to Use It In the previous section I discussed a program called go.php. This is what we will write here. It’s very short and simple: ```php $file = "shorturls.txt"; $result = UseShortURL($_GET['u'], $file); if($result) header("Location: $result"); else echo "That short URL is unrecognized"; ``` What this code does is fetch the argument passed to it in the GET variable ’u’ and run it through UseShortURL(), which then looks up the associated URL and returns it to the string variable $result. The header() function is then called to issue a Location: header, informing the browser where the contents it is requesting can be found. If you have any questions, please leave them in the comments and I'll be happy to answer them. thanks

(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]