PHP Classes

File: logic/shortLogic.php

Recommend this page to a friend!
  Classes of Faris AL-Otabi   PHP URL Shortener   logic/shortLogic.php   Download  
File: logic/shortLogic.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP URL Shortener
Application to create and expand short URLs
Author: By
Last change:
Date: 11 months ago
Size: 1,047 bytes
 

Contents

Class file image Download
<?php
$db
= new Framework\Database();

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

// Long URL
$longURL = $utils->sanitize($_POST['long_url']);
// Custom Code
$custom_code = !empty($_POST['short_code']) ? $utils->sanitize($_POST['short_code']) : false;
// Custom Password
$custom_password = !empty($_POST['password']) ? $utils->sanitize($_POST['password']) : false;

$shortener->setCheckUrlExists(true);

// Prefix of the short URL
$shortURL_Prefix = $utils->siteUrl(); // with URL rewrite

try {
    if (
$custom_code != false) {
       
$shortener->use_custom_code = true;
       
$shortener->custom_code = $custom_code;
    }

    if (
$custom_password != false) {
       
$shortener->link_protected = true;
       
$shortener->password = $custom_password;
    }

   
$shortCode = $shortener->urlToShortCode($longURL);
   
// Create short URL
   
$shortURL = $utils->siteUrl("/" . $shortCode);

   
// Display short URL
} catch (Exception $e) {
   
$shortURL = "";
   
$err = $e->getMessage();
}