PHP Classes

File: index.php

Recommend this page to a friend!
  Classes of anas khan   Custom PHP URL Shortener using PDO   index.php   Download  
File: index.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Custom PHP URL Shortener using PDO
Create and expand short URLs in a custom domain
Author: By
Last change:
Date: 2 years ago
Size: 826 bytes
 

Contents

Class file image Download
<?php
// Include database configuration file
require_once 'config.php';

// Include URL Shortener library file
require_once 'class.URLShortner.php';

// Initialize Shortener class and pass PDO object
$shortener = new URLShortner($db);

// Long URL
$longURL = 'https://dev.to/vtrpldn/write-fewer-media-queries-with-the-clamp-css-function-2cl7';

// Prefix of the short URL
$shortURL_Prefix = 'https://YOUR_PREFIX_URL.com/'; // with URL rewrite
$shortURL_Prefix = 'https://YUR_PREFIX_URL.com/?c='; // without URL rewrite

try{
   
// Get short code of the URL
   
$shortCode = $shortener->urlToShortCode($longURL);
   
   
// Create short URL
   
$shortURL = $shortURL_Prefix.$shortCode;
   
   
// Display short URL
   
echo 'Short URL: '.$shortURL;
}catch(
Exception $e){
   
// Display error
   
echo $e->getMessage();
}
?>