├── .htaccess ├── LICENSE ├── README.md ├── assets ├── css │ └── main.css └── images │ └── screenshot.png ├── config.php ├── database └── urls.sql ├── functions ├── UrlShortener.php ├── redirect.php └── shorten.php └── index.php /.htaccess: -------------------------------------------------------------------------------- 1 | RewriteEngine on 2 | 3 | RewriteCond %{REQUEST_FILENAME} !-d 4 | RewriteCond %{REQUEST_FILENAME} !-f 5 | RewriteRule ^(.*)$ functions/redirect.php?secret=$1 [L] 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Connect Everything 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Make It Short 2 | 3 | [](http://amarlearning.mit-license.org/) 4 | 5 | > Web application that will help you in shortening your URL by which you can get a more precise version of your URL at ease. 6 | 7 | ### Screenshots 8 | 9 |  10 | 11 | --- 12 | 13 | ### How to install? 14 | 15 | 1. Create a MySQL database 16 | 17 | 2. Connect to your database and execute the SQL statements contained in the file **database/urls.sql** 18 | 19 | 3. Edit the **config.php** file 20 | 21 | 4. Upload files to your server 22 | 23 | 5. That's it! 24 | 25 | ## Contributing 26 | 27 | Pull requests are always welcome so don't hesitate to create one. 28 | 29 | ### Code Style 30 | 31 | The code is formatted according to the [PSR-2](http://www.php-fig.org/psr/psr-2/) standard. Please follow this standard when contributing. 32 | 33 | ## Contributors 34 | 35 | Here is the list of [contributors](https://github.com/urls/url-shortener/graphs/contributors). 36 | 37 | ## Issues 38 | 39 | You can report the bugs at the [issue tracker](https://github.com/urls/url-shortener/issues) 40 | 41 | **OR** 42 | 43 | You can [tweet me](https://twitter.com/iamarpandey) if you can't get it to work. In fact, you should tweet me anyway. 44 | 45 | --- 46 | 47 | ## License 48 | 49 | Built with ♥ by Amar Prakash Pandey ([@amarlearning](http://github.com/amarlearning)) under [MIT License](http://amarlearning.mit-license.org/) 50 | 51 | You can find a copy of the License at [http://amarlearning.mit-license.org/](http://amarlearning.mit-license.org/) 52 | -------------------------------------------------------------------------------- /assets/css/main.css: -------------------------------------------------------------------------------- 1 | @import url(https://fonts.googleapis.com/css?family=Roboto:400,900); 2 | 3 | * { 4 | margin: 0px; 5 | padding: 0px; 6 | } 7 | 8 | body { 9 | font-family: 'Roboto', sans-serif; 10 | /*background-image: url(../img/a566df2b-7ce7-4f83-a490-5df2ef949817.png);*/ 11 | background-color: white; 12 | } 13 | 14 | h1 { 15 | text-align: center; 16 | font-size: 40px; 17 | margin: 20px; 18 | color: #167699; 19 | } 20 | 21 | .input { 22 | width: 100%; 23 | padding: 10px; 24 | height: 20px; 25 | outline-color: #167699; 26 | border: 1px solid #ddd; 27 | } 28 | 29 | .input_custom { 30 | width: 100%; 31 | padding: 10px; 32 | height: 20px; 33 | outline-color: #167699; 34 | border: 1px solid #ddd; 35 | } 36 | 37 | .submit { 38 | width: 100px; 39 | height: 40px; 40 | font-weight: bold; 41 | margin-top: 20px; 42 | font-size: 16px; 43 | border: 1px solid #ddd; 44 | cursor: pointer; 45 | outline-color: #167699; 46 | background-color: rgba(255, 255, 255, 0.5); 47 | } 48 | 49 | .alert { 50 | font-size: 14px; 51 | color: #000; 52 | margin-bottom: 30px; 53 | margin-top: 30px; 54 | padding-top: 5px; 55 | padding-bottom: 5px; 56 | color: #fff; 57 | background-color: rgba(211, 47, 47, 1); 58 | } 59 | 60 | .success { 61 | font-size: 14px; 62 | color: #000; 63 | margin-bottom: 30px; 64 | margin-top: 30px; 65 | padding-top: 5px; 66 | padding-bottom: 5px; 67 | color: #fff; 68 | background-color: lightgreen; 69 | text-decoration: underline; 70 | } 71 | 72 | .success a { 73 | color: #167699; 74 | } 75 | 76 | .onoffswitch { 77 | position: relative; 78 | width: 90px; 79 | -webkit-user-select: none; 80 | -moz-user-select: none; 81 | -ms-user-select: none; 82 | } 83 | 84 | .onoffswitch-checkbox { 85 | display: none; 86 | } 87 | 88 | .onoffswitch-label { 89 | display: block; 90 | overflow: hidden; 91 | cursor: pointer; 92 | height: 30px; 93 | padding: 0; 94 | line-height: 30px; 95 | border: 2px solid #999999; 96 | border-radius: 30px; 97 | background-color: #EEEEEE; 98 | transition: background-color 0.3s ease-in; 99 | } 100 | 101 | .onoffswitch-label:before { 102 | content: ""; 103 | display: block; 104 | width: 30px; 105 | margin: 0px; 106 | background: #FFFFFF; 107 | position: absolute; 108 | top: 0; 109 | bottom: 0; 110 | right: 58px; 111 | border: 2px solid #999999; 112 | border-radius: 30px; 113 | transition: all 0.3s ease-in 0s; 114 | } 115 | 116 | .onoffswitch-checkbox:checked + .onoffswitch-label { 117 | background-color: #167699; 118 | } 119 | 120 | .onoffswitch-checkbox:checked + .onoffswitch-label, .onoffswitch-checkbox:checked + .onoffswitch-label:before { 121 | border-color: #167699; 122 | } 123 | 124 | .onoffswitch-checkbox:checked + .onoffswitch-label:before { 125 | right: 0px; 126 | } 127 | 128 | /* SECTIONS */ 129 | .section { 130 | clear: both; 131 | padding: 0px; 132 | margin: auto; 133 | width: 50%; 134 | } 135 | 136 | /* COLUMN SETUP */ 137 | .col { 138 | display: block; 139 | float: left; 140 | margin: 1% 0 1% 0; 141 | } 142 | 143 | .col:first-child { 144 | margin-left: 0; 145 | } 146 | 147 | /* GROUPING */ 148 | .group:before, 149 | .group:after { 150 | content: ""; 151 | display: table; 152 | } 153 | 154 | .group:after { 155 | clear: both; 156 | } 157 | 158 | .group { 159 | zoom: 1; /* For IE 6/7 */ 160 | } 161 | 162 | /* GRID OF THREE */ 163 | .span_3_of_3 { 164 | width: 100%; 165 | } 166 | 167 | .span_2_of_3 { 168 | width: 20%; 169 | margin-left: 20px; 170 | 171 | } 172 | 173 | .span_1_of_3 { 174 | width: 70%; 175 | } 176 | 177 | /* GO FULL WIDTH AT LESS THAN 480 PIXELS */ 178 | 179 | @media only screen and (max-width: 480px) { 180 | .col { 181 | margin: 1% 0 1% 0%; 182 | } 183 | 184 | .span_3_of_3, .span_2_of_3, .span_1_of_3 { 185 | width: 100%; 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /assets/images/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/urls/url-shortener-php/7bfe116b136e9eaa94e6cc0345df5dbd5743bab1/assets/images/screenshot.png -------------------------------------------------------------------------------- /config.php: -------------------------------------------------------------------------------- 1 | db = new mysqli(HOST_NAME, USER_NAME, USER_PASSWORD, DB_NAME); 10 | 11 | if ($this->db->connect_errno) { 12 | header("Location: ../index.php?error=db"); 13 | die(); 14 | } 15 | } 16 | 17 | /** 18 | * Function to generate random unique code for new urls 19 | * 20 | * @param string $num row number of the link saved in database 21 | * 22 | * @return integer 23 | */ 24 | 25 | public function generateUniqueCode($idOfRow) { 26 | $idOfRow += 10000000; 27 | return base_convert($idOfRow, 10, 36); 28 | } 29 | 30 | /** 31 | * Validates URL, checks if already present in database and finally inserts 32 | * in database 33 | * 34 | * @param string $url Real Url 35 | * 36 | * @return string 37 | */ 38 | 39 | public function validateUrlAndReturnCode($orignalURL) { 40 | $orignalURL = trim($orignalURL); 41 | 42 | if (!filter_var($orignalURL, FILTER_VALIDATE_URL)) { 43 | header("Location: ../index.php?error=inurl"); 44 | die(); 45 | } 46 | 47 | else { 48 | $orignalURL = $this->db->real_escape_string($orignalURL); 49 | $existInDatabase = $this->db->query("SELECT * FROM link WHERE url ='{$orignalURL}'"); 50 | 51 | if ($existInDatabase->num_rows) { 52 | $uniqueCode = $existInDatabase->fetch_object()->code; 53 | 54 | return $uniqueCode; 55 | } 56 | 57 | $insertInDatabase = $this->db->query("INSERT INTO link (url,created) VALUES ('{$orignalURL}',NOW())"); 58 | $fetchFromDatabase = $this->db->query("SELECT * FROM link WHERE url = '{$orignalURL}'"); 59 | $getIdOfRow = $fetchFromDatabase->fetch_object()->id; 60 | $uniqueCode = $this->generateUniqueCode($getIdOfRow); 61 | $updateInDatabase = $this->db->query("UPDATE link SET code = '{$uniqueCode}' WHERE url = '{$orignalURL}'"); 62 | 63 | return $uniqueCode; 64 | } 65 | } 66 | 67 | /** 68 | * Insert url and custom short url on the database 69 | * 70 | * @param string $url Real Url 71 | * @param string $custom Custom short url wanted 72 | * 73 | * @return boolean 74 | */ 75 | 76 | public function returnCustomCode($orignalURL, $customUniqueCode) { 77 | $orignalURL = trim($orignalURL); 78 | $customUniqueCode = trim($customUniqueCode); 79 | 80 | if (filter_var($orignalURL, FILTER_VALIDATE_URL)) { 81 | $insert = $this->db->query("INSERT INTO link (url,code,created) VALUES ('{$orignalURL}','{$customUniqueCode}',NOW())"); 82 | 83 | return true; 84 | } 85 | 86 | return false; 87 | } 88 | 89 | /** 90 | * Returns the orignal URL based on the shorten url 91 | * 92 | * @param string $string Real Url 93 | * 94 | * @return string 95 | */ 96 | 97 | public function getOrignalURL($string) { 98 | $string = $this->db->real_escape_string(strip_tags(addslashes($string))); 99 | $rows = $this->db->query("SELECT url FROM link WHERE code = '{$string}'"); 100 | 101 | if ($rows->num_rows) { 102 | return $rows->fetch_object()->url; 103 | } 104 | 105 | else { 106 | header("Location: index.php?error=dnp"); 107 | die(); 108 | } 109 | } 110 | 111 | /** 112 | * Check if shorten code is already present in database 113 | * 114 | * @param string $short 115 | * 116 | * @return boolean 117 | */ 118 | 119 | public function checkUrlExistInDatabase($customCode) { 120 | $customCode = $this->db->real_escape_string(strip_tags(addslashes($customCode))); 121 | $fetchedRows = $this->db->query("SELECT url FROM link WHERE code = '{$customCode}' LIMIT 1"); 122 | 123 | return $fetchedRows->num_rows > 0; 124 | } 125 | 126 | /** 127 | * Generates link tag for the new shorten url 128 | * 129 | * @param string $uniqueCode 130 | * 131 | * @return string 132 | */ 133 | 134 | public function generateLinkForShortURL($uniqueCode = '') { 135 | return '' . BASE_URL . $uniqueCode . ''; 136 | } 137 | } 138 | 139 | ?> -------------------------------------------------------------------------------- /functions/redirect.php: -------------------------------------------------------------------------------- 1 | getOrignalURL($uniqueCode); 10 | header("Location: {$orignalUrl}"); 11 | die(); 12 | } 13 | 14 | header("Location: index.php"); 15 | die(); 16 | 17 | ?> -------------------------------------------------------------------------------- /functions/shorten.php: -------------------------------------------------------------------------------- 1 | checkUrlExistInDatabase($customCode)) { 17 | $insertCustom = true; 18 | } 19 | 20 | else { 21 | $errors = true; 22 | $_SESSION['error'] = 'The custom URL ' . BASE_URL . $_POST['custom'] . " already exists"; 23 | } 24 | } 25 | 26 | if (isset($_POST['url']) && !$errors) { 27 | $orignalURL = $_POST['url']; 28 | 29 | if (!$insertCustom) { 30 | if ($uniqueCode = $urlShortener->validateUrlAndReturnCode($orignalURL)) { 31 | $_SESSION['success'] = $urlShortener->generateLinkForShortURL($uniqueCode); 32 | } 33 | 34 | else { 35 | $_SESSION['error'] = "There was a problem. Invalid URL, perhaps?"; 36 | } 37 | } 38 | 39 | else { 40 | if ($urlShortener->returnCustomCode($orignalURL, $customCode)) { 41 | $_SESSION['success'] = $urlShortener->generateLinkForShortURL($customCode); 42 | } 43 | 44 | else { 45 | header("Location: ../index.php?error=inurl"); 46 | die(); 47 | } 48 | } 49 | } 50 | 51 | header("Location: ../index.php"); 52 | exit(); 53 | 54 | ?> -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 |" . $_SESSION['error'] . "
"; 27 | unset($_SESSION['error']); 28 | } 29 | if (isset($_GET['error']) && $_GET['error'] == 'db') { 30 | echo "Error in connecting to database!
"; 31 | } 32 | if (isset($_GET['error']) && $_GET['error'] == 'inurl') { 33 | echo "Not a valid URL!
"; 34 | } 35 | if (isset($_GET['error']) && $_GET['error'] == 'dnp') { 36 | echo "Ok! So I got to know you love playing! But don't play here!!!
"; 37 | } 38 | ?> 39 | 58 | 74 | 75 | 76 | --------------------------------------------------------------------------------