{"id":3684,"date":"2018-12-20T18:25:52","date_gmt":"2018-12-20T18:25:52","guid":{"rendered":"https:\/\/www.codexworld.com\/?p=3684"},"modified":"2020-05-29T07:51:10","modified_gmt":"2020-05-29T07:51:10","slug":"php-url-shortener-library-create-short-url","status":"publish","type":"post","link":"https:\/\/www.codexworld.com\/php-url-shortener-library-create-short-url\/","title":{"rendered":"Create Short URL using PHP URL Shortener"},"content":{"rendered":"<p>A short URL always a recommended way to share the web page URL. It easy to remember and can be shared easily on the web. There are many URL Shortener services are available that allows you to convert long URL to short URL online. But the main disadvantages of these services are you won&#8217;t be able to use your own domain in the short URL. If you want to create short URL with your own domain name, you need to use <b>custom URL Shortener<\/b>.<\/p>\n<p><b>URL Shortener<\/b> service takes a long URL and compresses it in a short link which is easier to share. You can create short URLs programmatically using PHP without any third party <b>URL Shortener API<\/b>. In this tutorial, we will show you how to build a URL Shortener library and <b>create short URL using PHP and MySQL<\/b>. With PHP URL Shortener library you can shorten the long URLs and use your own domain in the short URLs.<\/p>\n<p>In the example script shows the process to create a clean, short, and tiny link that can be shared easily via email or social media. The database is used to store the information about long and short URL. Also, you can track the number of hits the short URL gets by the visitors.<\/p>\n<h2>Create Database Table<\/h2>\n<p>We will use the database to handle the redirection based on the shortcode. The following SQL creates a <code>short_urls<\/code> table in the MySQL database to store URL info (long URL, short code, hits, and create time).<\/p>\n<pre style=\"color: rgb(0, 0, 0);\"><span class=\"hljs-keyword\" style=\"color: rgb(149, 65, 33);\">CREATE<\/span> <span class=\"hljs-keyword\" style=\"color: rgb(149, 65, 33);\">TABLE<\/span> <span class=\"hljs-string\" style=\"color: rgb(33, 145, 97);\">`short_urls`<\/span> (\r\n <span class=\"hljs-string\" style=\"color: rgb(33, 145, 97);\">`id`<\/span> <span class=\"hljs-built_in\" style=\"color: rgb(0, 134, 179);\">int<\/span>(<span class=\"hljs-number\" style=\"color: rgb(64, 160, 112);\">11<\/span>) <span class=\"hljs-keyword\" style=\"color: rgb(149, 65, 33);\">NOT<\/span> <span class=\"hljs-literal\" style=\"color: rgb(149, 65, 33);\">NULL<\/span> AUTO_INCREMENT,\r\n <span class=\"hljs-string\" style=\"color: rgb(33, 145, 97);\">`long_url`<\/span> <span class=\"hljs-built_in\" style=\"color: rgb(0, 134, 179);\">varchar<\/span>(<span class=\"hljs-number\" style=\"color: rgb(64, 160, 112);\">255<\/span>) <span class=\"hljs-keyword\" style=\"color: rgb(149, 65, 33);\">COLLATE<\/span> utf8_unicode_ci <span class=\"hljs-keyword\" style=\"color: rgb(149, 65, 33);\">NOT<\/span> <span class=\"hljs-literal\" style=\"color: rgb(149, 65, 33);\">NULL<\/span>,\r\n <span class=\"hljs-string\" style=\"color: rgb(33, 145, 97);\">`short_code`<\/span> <span class=\"hljs-built_in\" style=\"color: rgb(0, 134, 179);\">varchar<\/span>(<span class=\"hljs-number\" style=\"color: rgb(64, 160, 112);\">25<\/span>) <span class=\"hljs-keyword\" style=\"color: rgb(149, 65, 33);\">COLLATE<\/span> utf8_unicode_ci <span class=\"hljs-keyword\" style=\"color: rgb(149, 65, 33);\">NOT<\/span> <span class=\"hljs-literal\" style=\"color: rgb(149, 65, 33);\">NULL<\/span>,\r\n <span class=\"hljs-string\" style=\"color: rgb(33, 145, 97);\">`hits`<\/span> <span class=\"hljs-built_in\" style=\"color: rgb(0, 134, 179);\">int<\/span>(<span class=\"hljs-number\" style=\"color: rgb(64, 160, 112);\">11<\/span>) <span class=\"hljs-keyword\" style=\"color: rgb(149, 65, 33);\">NOT<\/span> <span class=\"hljs-literal\" style=\"color: rgb(149, 65, 33);\">NULL<\/span>,\r\n <span class=\"hljs-string\" style=\"color: rgb(33, 145, 97);\">`created`<\/span> datetime <span class=\"hljs-keyword\" style=\"color: rgb(149, 65, 33);\">NOT<\/span> <span class=\"hljs-literal\" style=\"color: rgb(149, 65, 33);\">NULL<\/span>,\r\n PRIMARY <span class=\"hljs-keyword\" style=\"color: rgb(149, 65, 33);\">KEY<\/span> (<span class=\"hljs-string\" style=\"color: rgb(33, 145, 97);\">`id`<\/span>)\r\n) <span class=\"hljs-keyword\" style=\"color: rgb(149, 65, 33);\">ENGINE<\/span>=<span class=\"hljs-keyword\" style=\"color: rgb(149, 65, 33);\">InnoDB<\/span> <span class=\"hljs-keyword\" style=\"color: rgb(149, 65, 33);\">DEFAULT<\/span> <span class=\"hljs-keyword\" style=\"color: rgb(149, 65, 33);\">CHARSET<\/span>=utf8 <span class=\"hljs-keyword\" style=\"color: rgb(149, 65, 33);\">COLLATE<\/span>=utf8_unicode_ci;<\/pre>\n<h2>URL Shortener Library (Shortener.class.php)<\/h2>\n<p>The <b>URL Shortener class<\/b> allow to programmatically create short URL using PHP and MySQL. This class uses <b>PDO Extension<\/b> to work with the MySQL database, so, PDO object instance is required on initialization of Shortener class.<\/p>\n<p><b>Static Variables:<\/b><\/p>\n<ul class=\"bullet_disk_list\">\n<li><code>$chars<\/code> &#8211; Allowed characters for the short code. (Characters group is separated by <code>|<\/code>)<\/li>\n<li><code>$table<\/code> &#8211; Database table name to store the URL and short code info.<\/li>\n<li><code>$checkUrlExists<\/code> &#8211; Set to TRUE, to check whether the long URL exists.<\/li>\n<li><code>$codeLength<\/code> &#8211; The length of the short code characters.<\/li>\n<\/ul>\n<p><b>Functions:<\/b><\/p>\n<ul class=\"bullet_disk_list\">\n<li><b>__construct()<\/b> &#8211; Set PDO object reference and timestamp.<\/li>\n<li><b>urlToShortCode()<\/b> &#8211; Validate URL and create short code.<\/li>\n<li><b>validateUrlFormat()<\/b> &#8211; Validate the format of the URL.<\/li>\n<li><b>verifyUrlExists()<\/b> &#8211; Verify the URL whether it exist or not using cURL in PHP.<\/li>\n<li><b>urlExistsInDB()<\/b> &#8211; Check whether the long URL is exist in the database. If exist, return the shot code, otherwise, return FALSE.<\/li>\n<li><b>createShortCode()<\/b> &#8211; Create short code for the long URL and insert the long URL &amp; short code in the database.<\/li>\n<li><b>generateRandomString()<\/b> &#8211; Generate random string (short code) with the specified characters in the $chars variable.<\/li>\n<li><b>insertUrlInDB()<\/b> &#8211; Insert URL info in the database using PDO Extension and MySQL and return the row ID.<\/li>\n<li><b>shortCodeToUrl()<\/b> &#8211; Convert the short code to long URL and insert the hits count in the database.<\/li>\n<li><b>validateShortCode()<\/b> &#8211; Validate the short code based on the allowed characters.<\/li>\n<li><b>getUrlFromDB()<\/b> &#8211; Fetch the long URL from the database based on the short code.<\/li>\n<li><b>incrementCounter()<\/b> &#8211; Increment the URL visits counter in the database for a particular record.<\/li>\n<\/ul>\n<pre><span style=\"color: #0000BB\">&lt;?php\r\n<\/span><span style=\"color: #FF8000\">\/**&nbsp;\r\n&nbsp;*&nbsp;Class&nbsp;to&nbsp;create&nbsp;short&nbsp;URLs&nbsp;and&nbsp;decode&nbsp;shortened&nbsp;URLs\r\n&nbsp;*&nbsp;\r\n&nbsp;*&nbsp;@author&nbsp;CodexWorld.com&nbsp;&lt;contact@codexworld.com&gt;&nbsp;\r\n&nbsp;*&nbsp;@copyright&nbsp;Copyright&nbsp;(c)&nbsp;2018,&nbsp;CodexWorld.com\r\n&nbsp;*&nbsp;@url&nbsp;https:\/\/www.codexworld.com\r\n&nbsp;*\/&nbsp;\r\n<\/span><span style=\"color: #007700\">class&nbsp;<\/span><span style=\"color: #0000BB\">Shortener\r\n<\/span><span style=\"color: #007700\">{\r\n&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;static&nbsp;<\/span><span style=\"color: #0000BB\">$chars&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">\"abcdfghjkmnpqrstvwxyz|ABCDFGHJKLMNPQRSTVWXYZ|0123456789\"<\/span><span style=\"color: #007700\">;\r\n&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;static&nbsp;<\/span><span style=\"color: #0000BB\">$table&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">\"short_urls\"<\/span><span style=\"color: #007700\">;\r\n&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;static&nbsp;<\/span><span style=\"color: #0000BB\">$checkUrlExists&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">;\r\n&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;static&nbsp;<\/span><span style=\"color: #0000BB\">$codeLength&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">7<\/span><span style=\"color: #007700\">;\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;<\/span><span style=\"color: #0000BB\">$pdo<\/span><span style=\"color: #007700\">;\r\n&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;<\/span><span style=\"color: #0000BB\">$timestamp<\/span><span style=\"color: #007700\">;\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;<\/span><span style=\"color: #0000BB\">__construct<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">PDO&nbsp;$pdo<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">pdo&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$pdo<\/span><span style=\"color: #007700\">;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">timestamp&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">date<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"Y-m-d&nbsp;H:i:s\"<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;<\/span><span style=\"color: #0000BB\">urlToShortCode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$url<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(empty(<\/span><span style=\"color: #0000BB\">$url<\/span><span style=\"color: #007700\">)){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;<\/span><span style=\"color: #0000BB\">Exception<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"No&nbsp;URL&nbsp;was&nbsp;supplied.\"<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">validateUrlFormat<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$url<\/span><span style=\"color: #007700\">)&nbsp;==&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;<\/span><span style=\"color: #0000BB\">Exception<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"URL&nbsp;does&nbsp;not&nbsp;have&nbsp;a&nbsp;valid&nbsp;format.\"<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(<\/span><span style=\"color: #0000BB\">self<\/span><span style=\"color: #007700\">::<\/span><span style=\"color: #0000BB\">$checkUrlExists<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">verifyUrlExists<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$url<\/span><span style=\"color: #007700\">)){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;<\/span><span style=\"color: #0000BB\">Exception<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"URL&nbsp;does&nbsp;not&nbsp;appear&nbsp;to&nbsp;exist.\"<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$shortCode&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">urlExistsInDB<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$url<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(<\/span><span style=\"color: #0000BB\">$shortCode&nbsp;<\/span><span style=\"color: #007700\">==&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$shortCode&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">createShortCode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$url<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<\/span><span style=\"color: #0000BB\">$shortCode<\/span><span style=\"color: #007700\">;\r\n&nbsp;&nbsp;&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;function&nbsp;<\/span><span style=\"color: #0000BB\">validateUrlFormat<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$url<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<\/span><span style=\"color: #0000BB\">filter_var<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$url<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">FILTER_VALIDATE_URL<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">FILTER_FLAG_HOST_REQUIRED<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;function&nbsp;<\/span><span style=\"color: #0000BB\">verifyUrlExists<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$url<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$ch&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">curl_init<\/span><span style=\"color: #007700\">();\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">curl_setopt<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">CURLOPT_URL<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$url<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">curl_setopt<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">CURLOPT_NOBODY<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">curl_setopt<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">,&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">CURLOPT_RETURNTRANSFER<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">curl_exec<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$response&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">curl_getinfo<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">CURLINFO_HTTP_CODE<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">curl_close<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">);\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(!empty(<\/span><span style=\"color: #0000BB\">$response<\/span><span style=\"color: #007700\">)&nbsp;&amp;&amp;&nbsp;<\/span><span style=\"color: #0000BB\">$response&nbsp;<\/span><span style=\"color: #007700\">!=&nbsp;<\/span><span style=\"color: #0000BB\">404<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;function&nbsp;<\/span><span style=\"color: #0000BB\">urlExistsInDB<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$url<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$query&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">\"SELECT&nbsp;short_code&nbsp;FROM&nbsp;\"<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">self<\/span><span style=\"color: #007700\">::<\/span><span style=\"color: #0000BB\">$table<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #DD0000\">\"&nbsp;WHERE&nbsp;long_url&nbsp;=&nbsp;:long_url&nbsp;LIMIT&nbsp;1\"<\/span><span style=\"color: #007700\">;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$stmt&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">pdo<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">prepare<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$query<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$params&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;array(\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">\"long_url\"&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$url\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$stmt<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">execute<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$params<\/span><span style=\"color: #007700\">);\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$result&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$stmt<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">fetch<\/span><span style=\"color: #007700\">();\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(empty(<\/span><span style=\"color: #0000BB\">$result<\/span><span style=\"color: #007700\">))&nbsp;?&nbsp;<\/span><span style=\"color: #0000BB\">false&nbsp;<\/span><span style=\"color: #007700\">:&nbsp;<\/span><span style=\"color: #0000BB\">$result<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">\"short_code\"<\/span><span style=\"color: #007700\">];\r\n&nbsp;&nbsp;&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;function&nbsp;<\/span><span style=\"color: #0000BB\">createShortCode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$url<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$shortCode&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">generateRandomString<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">self<\/span><span style=\"color: #007700\">::<\/span><span style=\"color: #0000BB\">$codeLength<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$id&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">insertUrlInDB<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$url<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$shortCode<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<\/span><span style=\"color: #0000BB\">$shortCode<\/span><span style=\"color: #007700\">;\r\n&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;function&nbsp;<\/span><span style=\"color: #0000BB\">generateRandomString<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$length&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">6<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$sets&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">explode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'|'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">self<\/span><span style=\"color: #007700\">::<\/span><span style=\"color: #0000BB\">$chars<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$all&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">''<\/span><span style=\"color: #007700\">;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$randString&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">''<\/span><span style=\"color: #007700\">;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach(<\/span><span style=\"color: #0000BB\">$sets&nbsp;<\/span><span style=\"color: #007700\">as&nbsp;<\/span><span style=\"color: #0000BB\">$set<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$randString&nbsp;<\/span><span style=\"color: #007700\">.=&nbsp;<\/span><span style=\"color: #0000BB\">$set<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #0000BB\">array_rand<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">str_split<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$set<\/span><span style=\"color: #007700\">))];\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$all&nbsp;<\/span><span style=\"color: #007700\">.=&nbsp;<\/span><span style=\"color: #0000BB\">$set<\/span><span style=\"color: #007700\">;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$all&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">str_split<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$all<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(<\/span><span style=\"color: #0000BB\">$i&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">;&nbsp;<\/span><span style=\"color: #0000BB\">$i&nbsp;<\/span><span style=\"color: #007700\">&lt;&nbsp;<\/span><span style=\"color: #0000BB\">$length&nbsp;<\/span><span style=\"color: #007700\">-&nbsp;<\/span><span style=\"color: #0000BB\">count<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$sets<\/span><span style=\"color: #007700\">);&nbsp;<\/span><span style=\"color: #0000BB\">$i<\/span><span style=\"color: #007700\">++){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$randString&nbsp;<\/span><span style=\"color: #007700\">.=&nbsp;<\/span><span style=\"color: #0000BB\">$all<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #0000BB\">array_rand<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$all<\/span><span style=\"color: #007700\">)];\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$randString&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">str_shuffle<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$randString<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<\/span><span style=\"color: #0000BB\">$randString<\/span><span style=\"color: #007700\">;\r\n&nbsp;&nbsp;&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;function&nbsp;<\/span><span style=\"color: #0000BB\">insertUrlInDB<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$url<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$code<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$query&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">\"INSERT&nbsp;INTO&nbsp;\"<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">self<\/span><span style=\"color: #007700\">::<\/span><span style=\"color: #0000BB\">$table<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #DD0000\">\"&nbsp;(long_url,&nbsp;short_code,&nbsp;created)&nbsp;VALUES&nbsp;(:long_url,&nbsp;:short_code,&nbsp;:timestamp)\"<\/span><span style=\"color: #007700\">;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$stmnt&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">pdo<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">prepare<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$query<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$params&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;array(\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">\"long_url\"&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$url<\/span><span style=\"color: #007700\">,\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">\"short_code\"&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$code<\/span><span style=\"color: #007700\">,\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">\"timestamp\"&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">timestamp\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$stmnt<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">execute<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$params<\/span><span style=\"color: #007700\">);\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">pdo<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">lastInsertId<\/span><span style=\"color: #007700\">();\r\n&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;<\/span><span style=\"color: #0000BB\">shortCodeToUrl<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$code<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$increment&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(empty(<\/span><span style=\"color: #0000BB\">$code<\/span><span style=\"color: #007700\">))&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;<\/span><span style=\"color: #0000BB\">Exception<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"No&nbsp;short&nbsp;code&nbsp;was&nbsp;supplied.\"<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">validateShortCode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$code<\/span><span style=\"color: #007700\">)&nbsp;==&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;<\/span><span style=\"color: #0000BB\">Exception<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"Short&nbsp;code&nbsp;does&nbsp;not&nbsp;have&nbsp;a&nbsp;valid&nbsp;format.\"<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$urlRow&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">getUrlFromDB<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$code<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(empty(<\/span><span style=\"color: #0000BB\">$urlRow<\/span><span style=\"color: #007700\">)){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;<\/span><span style=\"color: #0000BB\">Exception<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"Short&nbsp;code&nbsp;does&nbsp;not&nbsp;appear&nbsp;to&nbsp;exist.\"<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(<\/span><span style=\"color: #0000BB\">$increment&nbsp;<\/span><span style=\"color: #007700\">==&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">incrementCounter<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$urlRow<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">\"id\"<\/span><span style=\"color: #007700\">]);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<\/span><span style=\"color: #0000BB\">$urlRow<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">\"long_url\"<\/span><span style=\"color: #007700\">];\r\n&nbsp;&nbsp;&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;function&nbsp;<\/span><span style=\"color: #0000BB\">validateShortCode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$code<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$rawChars&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">str_replace<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'|'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">''<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">self<\/span><span style=\"color: #007700\">::<\/span><span style=\"color: #0000BB\">$chars<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<\/span><span style=\"color: #0000BB\">preg_match<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"|[\"<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$rawChars<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #DD0000\">\"]+|\"<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$code<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;function&nbsp;<\/span><span style=\"color: #0000BB\">getUrlFromDB<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$code<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$query&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">\"SELECT&nbsp;id,&nbsp;long_url&nbsp;FROM&nbsp;\"<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">self<\/span><span style=\"color: #007700\">::<\/span><span style=\"color: #0000BB\">$table<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #DD0000\">\"&nbsp;WHERE&nbsp;short_code&nbsp;=&nbsp;:short_code&nbsp;LIMIT&nbsp;1\"<\/span><span style=\"color: #007700\">;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$stmt&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">pdo<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">prepare<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$query<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$params<\/span><span style=\"color: #007700\">=array(\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">\"short_code\"&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$code\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$stmt<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">execute<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$params<\/span><span style=\"color: #007700\">);\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$result&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$stmt<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">fetch<\/span><span style=\"color: #007700\">();\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(empty(<\/span><span style=\"color: #0000BB\">$result<\/span><span style=\"color: #007700\">))&nbsp;?&nbsp;<\/span><span style=\"color: #0000BB\">false&nbsp;<\/span><span style=\"color: #007700\">:&nbsp;<\/span><span style=\"color: #0000BB\">$result<\/span><span style=\"color: #007700\">;\r\n&nbsp;&nbsp;&nbsp;&nbsp;}\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;function&nbsp;<\/span><span style=\"color: #0000BB\">incrementCounter<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$id<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$query&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">\"UPDATE&nbsp;\"<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">self<\/span><span style=\"color: #007700\">::<\/span><span style=\"color: #0000BB\">$table<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #DD0000\">\"&nbsp;SET&nbsp;hits&nbsp;=&nbsp;hits&nbsp;+&nbsp;1&nbsp;WHERE&nbsp;id&nbsp;=&nbsp;:id\"<\/span><span style=\"color: #007700\">;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$stmt&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">pdo<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">prepare<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$query<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$params&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;array(\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">\"id\"&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$id\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$stmt<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">execute<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$params<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;}\r\n}<\/span><\/pre>\n<h2>Database Configuration (dbConfig.php)<\/h2>\n<p>In the <code>dbConfig.php<\/code> file, PDO (PHP Data Objects) is used to connect and select the database. Specify the database host (<code>$dbHost<\/code>), username (<code>$dbUsername<\/code>), password (<code>$dbPassword<\/code>), and name (<code>$dbName<\/code>) as per your MySQL database server credentials.<\/p>\n<pre><span style=\"color: #0000BB\">&lt;?php\r\n<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Database&nbsp;configuration\r\n<\/span><span style=\"color: #0000BB\">$dbHost&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">\"localhost\"<\/span><span style=\"color: #007700\">;\r\n<\/span><span style=\"color: #0000BB\">$dbUsername&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">\"root\"<\/span><span style=\"color: #007700\">;\r\n<\/span><span style=\"color: #0000BB\">$dbPassword&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">\"root\"<\/span><span style=\"color: #007700\">;\r\n<\/span><span style=\"color: #0000BB\">$dbName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">\"codexworld\"<\/span><span style=\"color: #007700\">;\r\n\r\n<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Create&nbsp;database&nbsp;connection\r\n<\/span><span style=\"color: #007700\">try{\r\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$db&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;new&nbsp;<\/span><span style=\"color: #0000BB\">PDO<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"mysql:host=<\/span><span style=\"color: #0000BB\">$dbHost<\/span><span style=\"color: #DD0000\">;dbname=<\/span><span style=\"color: #0000BB\">$dbName<\/span><span style=\"color: #DD0000\">\"<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$dbUsername<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$dbPassword<\/span><span style=\"color: #007700\">);\r\n}catch(<\/span><span style=\"color: #0000BB\">PDOException&nbsp;$e<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"Connection&nbsp;failed:&nbsp;\"&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$e<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">getMessage<\/span><span style=\"color: #007700\">();\r\n}<\/span><\/pre>\n<h2>Create Short URL with PHP<\/h2>\n<p>The following code creates short code and generates short URL with custom URL Shortener class using PHP and MySQL.<\/p>\n<ul class=\"bullet_disk_list\">\n<li>Initialize Shortener class and pass the PDO object.<\/li>\n<li>Specify the long URL which you want to convert in a short link.<\/li>\n<li>Specify the short URL prefix. If you want to use RewriteEngine to Rewrite the URL, specify only the base URI. Otherwise, specify the base URI with a query string to pass the short code.<\/li>\n<li>Call the <code>urlToShortCode()<\/code> function to get the short code of the long URL.<\/li>\n<li>Create short URL with URI prefix and short code.<\/li>\n<\/ul>\n<pre><span style=\"color: #FF8000\">\/\/&nbsp;Include&nbsp;database&nbsp;configuration&nbsp;file\r\n<\/span><span style=\"color: #007700\">require_once&nbsp;<\/span><span style=\"color: #DD0000\">'dbConfig.php'<\/span><span style=\"color: #007700\">;\r\n\r\n<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Include&nbsp;URL&nbsp;Shortener&nbsp;library&nbsp;file\r\n<\/span><span style=\"color: #007700\">require_once&nbsp;<\/span><span style=\"color: #DD0000\">'Shortener.class.php'<\/span><span style=\"color: #007700\">;\r\n\r\n<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Initialize&nbsp;Shortener&nbsp;class&nbsp;and&nbsp;pass&nbsp;PDO&nbsp;object\r\n<\/span><span style=\"color: #0000BB\">$shortener&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;new&nbsp;<\/span><span style=\"color: #0000BB\">Shortener<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$db<\/span><span style=\"color: #007700\">);\r\n\r\n<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Long&nbsp;URL\r\n<\/span><span style=\"color: #0000BB\">$longURL&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'https:\/\/www.codexworld.com\/tutorials\/php\/'<\/span><span style=\"color: #007700\">;\r\n\r\n<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Prefix&nbsp;of&nbsp;the&nbsp;short&nbsp;URL&nbsp;\r\n<\/span><span style=\"color: #0000BB\">$shortURL_Prefix&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'https:\/\/xyz.com\/'<\/span><span style=\"color: #007700\">;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;with&nbsp;URL&nbsp;rewrite\r\n<\/span><span style=\"color: #0000BB\">$shortURL_Prefix&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'https:\/\/xyz.com\/?c='<\/span><span style=\"color: #007700\">;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;without&nbsp;URL&nbsp;rewrite\r\n\r\n<\/span><span style=\"color: #007700\">try{\r\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Get&nbsp;short&nbsp;code&nbsp;of&nbsp;the&nbsp;URL\r\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$shortCode&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$shortener<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">urlToShortCode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$longURL<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Create&nbsp;short&nbsp;URL\r\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$shortURL&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$shortURL_Prefix<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$shortCode<\/span><span style=\"color: #007700\">;\r\n&nbsp;&nbsp;&nbsp;&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Display&nbsp;short&nbsp;URL\r\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">echo&nbsp;<\/span><span style=\"color: #DD0000\">'Short&nbsp;URL:&nbsp;'<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$shortURL<\/span><span style=\"color: #007700\">;\r\n}catch(<\/span><span style=\"color: #0000BB\">Exception&nbsp;$e<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Display&nbsp;error\r\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">echo&nbsp;<\/span><span style=\"color: #0000BB\">$e<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">getMessage<\/span><span style=\"color: #007700\">();\r\n}<\/span><\/pre>\n<h2>Redirect to Long URL<\/h2>\n<p>The following code handles the redirection from short URL to the original URL.<\/p>\n<ul class=\"bullet_disk_list\">\n<li>Retrieve the short code from the query string of the URL, or from the URI segment.<\/li>\n<li>Call <code>shortCodeToUrl()<\/code> function to get the long URL by the short code.<\/li>\n<li>Redirect user to the original URL.<\/li>\n<\/ul>\n<pre><span style=\"color: #FF8000\">\/\/&nbsp;Include&nbsp;database&nbsp;configuration&nbsp;file\r\n<\/span><span style=\"color: #007700\">require_once&nbsp;<\/span><span style=\"color: #DD0000\">'dbConfig.php'<\/span><span style=\"color: #007700\">;\r\n\r\n<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Include&nbsp;URL&nbsp;Shortener&nbsp;library&nbsp;file\r\n<\/span><span style=\"color: #007700\">require_once&nbsp;<\/span><span style=\"color: #DD0000\">'Shortener.class.php'<\/span><span style=\"color: #007700\">;\r\n\r\n<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Initialize&nbsp;Shortener&nbsp;class&nbsp;and&nbsp;pass&nbsp;PDO&nbsp;object\r\n<\/span><span style=\"color: #0000BB\">$shortener&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;new&nbsp;<\/span><span style=\"color: #0000BB\">Shortener<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$db<\/span><span style=\"color: #007700\">);\r\n\r\n<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Retrieve&nbsp;short&nbsp;code&nbsp;from&nbsp;URL\r\n<\/span><span style=\"color: #0000BB\">$shortCode&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$_GET<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">\"c\"<\/span><span style=\"color: #007700\">];\r\n\r\ntry{\r\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Get&nbsp;URL&nbsp;by&nbsp;short&nbsp;code\r\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$url&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$shortener<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">shortCodeToUrl<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$shortCode<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Redirect&nbsp;to&nbsp;the&nbsp;original&nbsp;URL\r\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">header<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"Location:&nbsp;\"<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$url<\/span><span style=\"color: #007700\">);\r\n&nbsp;&nbsp;&nbsp;&nbsp;exit;\r\n}catch(<\/span><span style=\"color: #0000BB\">Exception&nbsp;$e<\/span><span style=\"color: #007700\">){\r\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Display&nbsp;error\r\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">echo&nbsp;<\/span><span style=\"color: #0000BB\">$e<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">getMessage<\/span><span style=\"color: #007700\">();\r\n}<\/span><\/pre>\n<h2>URL Rewrite with HTACCESS<\/h2>\n<p>If you want to make the URL user-friendly, use HTACCESS with RewriteRule. With <b>mod_rewrite<\/b>, you can make the URL length shorter and easy to share.<\/p>\n<p>Create a <code>.htaccess<\/code> file and add the following code.<\/p>\n<pre style=\"color: rgb(68, 68, 68);\">&lt;IfModule mod_rewrite.c&gt;\r\n<span style=\"font-weight: 700;\">RewriteEngine<\/span> On\r\n<span style=\"font-weight: 700;\">RewriteCond<\/span> %{REQUEST_FILENAME} !-f\r\n<span style=\"font-weight: 700;\">RewriteCond<\/span> %{REQUEST_FILENAME} !-d\r\n<span style=\"font-weight: 700;\">RewriteRule<\/span> ^([a-zA-Z0-9]+)\/?$ redirect.php?c=$1 [L] \r\n&lt;\/IfModule&gt;<\/pre>\n<p>The above HTACCESS RewriteRule will send the request to redirect.php file. So, the <code>redirect.php<\/code> file name doesn&#8217;t need to mention in the short URL.<\/p>\n<ul class=\"bullet_disk_list\">\n<li>The short URL without using HTACCESS looks something like this &#8211; <code>https:\/\/example.com\/redirect.php?c=gzYN7BK<\/code><\/li>\n<li>The short URL with using HTACCESS looks something like this &#8211; <code>https:\/\/example.com\/gzYN7BK<\/code><\/li>\n<\/ul>\n<p class=\"seeAlso\"><span><\/span><a href=\"https:\/\/www.codexworld.com\/generate-seo-friendly-url-from-title-string-php\/\">Generate SEO Friendly URL from String in PHP<\/a><\/span><\/p>\n<h2>Conclusion<\/h2>\n<p>Our Shortener class helps you to <b>create short URL<\/b> easily using PHP. You can use this library to build your own URL Shortener with PHP and MySQL. Use the example code to shorten the URL on the fly without using any third-party service. Also, the <b>PHP URL Shortener<\/b> class can easily be extended to customize the URL shorting functionality.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A short URL always a recommended way to share the web page URL. It easy to remember and can be shared easily on the web. There are many URL Shortener services are available that allows <\/p>\n","protected":false},"author":1,"featured_media":3686,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4],"tags":[264,166,14,261],"class_list":["post-3684","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-class","tag-library","tag-php","tag-url","cat-4-id","has_thumb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Create Short URL using PHP URL Shortener - CodexWorld<\/title>\n<meta name=\"description\" content=\"PHP URL Shortener - Convert long URL to short URL in PHP. Use URL Shortener library to create short URL using PHP and MySQL. Example script to generate tiny URL with PHP.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.codexworld.com\/php-url-shortener-library-create-short-url\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create Short URL using PHP URL Shortener - CodexWorld\" \/>\n<meta property=\"og:description\" content=\"PHP URL Shortener - Convert long URL to short URL in PHP. Use URL Shortener library to create short URL using PHP and MySQL. Example script to generate tiny URL with PHP.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.codexworld.com\/php-url-shortener-library-create-short-url\/\" \/>\n<meta property=\"og:site_name\" content=\"CodexWorld\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codexworld\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codexworld\" \/>\n<meta property=\"article:published_time\" content=\"2018-12-20T18:25:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-05-29T07:51:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.codexworld.com\/wp-content\/uploads\/2018\/12\/php-url-shortener-library-create-short-url-codexworld.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1366\" \/>\n\t<meta property=\"og:image:height\" content=\"768\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"CodexWorld\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codexworldblog\" \/>\n<meta name=\"twitter:site\" content=\"@codexworldweb\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"CodexWorld\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"16 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/php-url-shortener-library-create-short-url\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/php-url-shortener-library-create-short-url\\\/\"},\"author\":{\"name\":\"CodexWorld\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/person\\\/9da51d8fa3cdefeb5ec9c69136d4baf0\"},\"headline\":\"Create Short URL using PHP URL Shortener\",\"datePublished\":\"2018-12-20T18:25:52+00:00\",\"dateModified\":\"2020-05-29T07:51:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/php-url-shortener-library-create-short-url\\\/\"},\"wordCount\":852,\"commentCount\":10,\"publisher\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/php-url-shortener-library-create-short-url\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2018\\\/12\\\/php-url-shortener-library-create-short-url-codexworld.png\",\"keywords\":[\"Class\",\"Library\",\"PHP\",\"URL\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.codexworld.com\\\/php-url-shortener-library-create-short-url\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/php-url-shortener-library-create-short-url\\\/\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/php-url-shortener-library-create-short-url\\\/\",\"name\":\"Create Short URL using PHP URL Shortener - CodexWorld\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/php-url-shortener-library-create-short-url\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/php-url-shortener-library-create-short-url\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2018\\\/12\\\/php-url-shortener-library-create-short-url-codexworld.png\",\"datePublished\":\"2018-12-20T18:25:52+00:00\",\"dateModified\":\"2020-05-29T07:51:10+00:00\",\"description\":\"PHP URL Shortener - Convert long URL to short URL in PHP. Use URL Shortener library to create short URL using PHP and MySQL. Example script to generate tiny URL with PHP.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/php-url-shortener-library-create-short-url\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.codexworld.com\\\/php-url-shortener-library-create-short-url\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/php-url-shortener-library-create-short-url\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2018\\\/12\\\/php-url-shortener-library-create-short-url-codexworld.png\",\"contentUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2018\\\/12\\\/php-url-shortener-library-create-short-url-codexworld.png\",\"width\":1366,\"height\":768,\"caption\":\"php-url-shortener-library-create-short-url-codexworld\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/php-url-shortener-library-create-short-url\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.codexworld.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create Short URL using PHP URL Shortener\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#website\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/\",\"name\":\"CodexWorld\",\"description\":\"Web &amp; Mobile App Development Company\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.codexworld.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\",\"name\":\"CodexWorld\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/codexworld-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/codexworld-logo.png\",\"width\":200,\"height\":19,\"caption\":\"CodexWorld\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/codexworld\",\"https:\\\/\\\/x.com\\\/codexworldweb\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/codexworld\",\"https:\\\/\\\/www.youtube.com\\\/codexworld\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/person\\\/9da51d8fa3cdefeb5ec9c69136d4baf0\",\"name\":\"CodexWorld\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"caption\":\"CodexWorld\"},\"description\":\"CodexWorld is a programming blog, one-stop destination for web professionals \u2014 developers, programmers, freelancers, and site owners.\",\"sameAs\":[\"http:\\\/\\\/www.codexworld.com\",\"https:\\\/\\\/www.facebook.com\\\/codexworld\",\"https:\\\/\\\/x.com\\\/codexworldblog\"],\"url\":\"https:\\\/\\\/www.codexworld.com\\\/author\\\/nitya192265\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Create Short URL using PHP URL Shortener - CodexWorld","description":"PHP URL Shortener - Convert long URL to short URL in PHP. Use URL Shortener library to create short URL using PHP and MySQL. Example script to generate tiny URL with PHP.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.codexworld.com\/php-url-shortener-library-create-short-url\/","og_locale":"en_US","og_type":"article","og_title":"Create Short URL using PHP URL Shortener - CodexWorld","og_description":"PHP URL Shortener - Convert long URL to short URL in PHP. Use URL Shortener library to create short URL using PHP and MySQL. Example script to generate tiny URL with PHP.","og_url":"https:\/\/www.codexworld.com\/php-url-shortener-library-create-short-url\/","og_site_name":"CodexWorld","article_publisher":"https:\/\/www.facebook.com\/codexworld","article_author":"https:\/\/www.facebook.com\/codexworld","article_published_time":"2018-12-20T18:25:52+00:00","article_modified_time":"2020-05-29T07:51:10+00:00","og_image":[{"width":1366,"height":768,"url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2018\/12\/php-url-shortener-library-create-short-url-codexworld.png","type":"image\/png"}],"author":"CodexWorld","twitter_card":"summary_large_image","twitter_creator":"@codexworldblog","twitter_site":"@codexworldweb","twitter_misc":{"Written by":"CodexWorld","Est. reading time":"16 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.codexworld.com\/php-url-shortener-library-create-short-url\/#article","isPartOf":{"@id":"https:\/\/www.codexworld.com\/php-url-shortener-library-create-short-url\/"},"author":{"name":"CodexWorld","@id":"https:\/\/www.codexworld.com\/#\/schema\/person\/9da51d8fa3cdefeb5ec9c69136d4baf0"},"headline":"Create Short URL using PHP URL Shortener","datePublished":"2018-12-20T18:25:52+00:00","dateModified":"2020-05-29T07:51:10+00:00","mainEntityOfPage":{"@id":"https:\/\/www.codexworld.com\/php-url-shortener-library-create-short-url\/"},"wordCount":852,"commentCount":10,"publisher":{"@id":"https:\/\/www.codexworld.com\/#organization"},"image":{"@id":"https:\/\/www.codexworld.com\/php-url-shortener-library-create-short-url\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2018\/12\/php-url-shortener-library-create-short-url-codexworld.png","keywords":["Class","Library","PHP","URL"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.codexworld.com\/php-url-shortener-library-create-short-url\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.codexworld.com\/php-url-shortener-library-create-short-url\/","url":"https:\/\/www.codexworld.com\/php-url-shortener-library-create-short-url\/","name":"Create Short URL using PHP URL Shortener - CodexWorld","isPartOf":{"@id":"https:\/\/www.codexworld.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.codexworld.com\/php-url-shortener-library-create-short-url\/#primaryimage"},"image":{"@id":"https:\/\/www.codexworld.com\/php-url-shortener-library-create-short-url\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2018\/12\/php-url-shortener-library-create-short-url-codexworld.png","datePublished":"2018-12-20T18:25:52+00:00","dateModified":"2020-05-29T07:51:10+00:00","description":"PHP URL Shortener - Convert long URL to short URL in PHP. Use URL Shortener library to create short URL using PHP and MySQL. Example script to generate tiny URL with PHP.","breadcrumb":{"@id":"https:\/\/www.codexworld.com\/php-url-shortener-library-create-short-url\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.codexworld.com\/php-url-shortener-library-create-short-url\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codexworld.com\/php-url-shortener-library-create-short-url\/#primaryimage","url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2018\/12\/php-url-shortener-library-create-short-url-codexworld.png","contentUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2018\/12\/php-url-shortener-library-create-short-url-codexworld.png","width":1366,"height":768,"caption":"php-url-shortener-library-create-short-url-codexworld"},{"@type":"BreadcrumbList","@id":"https:\/\/www.codexworld.com\/php-url-shortener-library-create-short-url\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.codexworld.com\/"},{"@type":"ListItem","position":2,"name":"Create Short URL using PHP URL Shortener"}]},{"@type":"WebSite","@id":"https:\/\/www.codexworld.com\/#website","url":"https:\/\/www.codexworld.com\/","name":"CodexWorld","description":"Web &amp; Mobile App Development Company","publisher":{"@id":"https:\/\/www.codexworld.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.codexworld.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.codexworld.com\/#organization","name":"CodexWorld","url":"https:\/\/www.codexworld.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codexworld.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2014\/09\/codexworld-logo.png","contentUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2014\/09\/codexworld-logo.png","width":200,"height":19,"caption":"CodexWorld"},"image":{"@id":"https:\/\/www.codexworld.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codexworld","https:\/\/x.com\/codexworldweb","https:\/\/www.linkedin.com\/company\/codexworld","https:\/\/www.youtube.com\/codexworld"]},{"@type":"Person","@id":"https:\/\/www.codexworld.com\/#\/schema\/person\/9da51d8fa3cdefeb5ec9c69136d4baf0","name":"CodexWorld","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","caption":"CodexWorld"},"description":"CodexWorld is a programming blog, one-stop destination for web professionals \u2014 developers, programmers, freelancers, and site owners.","sameAs":["http:\/\/www.codexworld.com","https:\/\/www.facebook.com\/codexworld","https:\/\/x.com\/codexworldblog"],"url":"https:\/\/www.codexworld.com\/author\/nitya192265\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2018\/12\/php-url-shortener-library-create-short-url-codexworld.png","jetpack_shortlink":"https:\/\/wp.me\/p6bxIh-Xq","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/3684","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/comments?post=3684"}],"version-history":[{"count":6,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/3684\/revisions"}],"predecessor-version":[{"id":4419,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/3684\/revisions\/4419"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/media\/3686"}],"wp:attachment":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/media?parent=3684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/categories?post=3684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/tags?post=3684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}