[Release] URL Shortner in OOP PHP

Results 1 to 15 of 15
  1. #1
    :joy: Jonteh is offline
    MemberRank
    Apr 2007 Join Date
    New York, USALocation
    3,375Posts

    [Release] URL Shortner in OOP PHP

    Hi.. I had a semi popular URL shortening website but as it's now offline I have no use for the script.

    Features:
    - Shortening URLs and providing the short url
    - htaccess for dynamic short urls
    - OOP PHP
    - Simple yet elegant design
    - Template system

    Here is the shortening class for a taste of what is inside the download;

    Code:
    <?php
    
    
    	class SoShort
    	{
    		public function ParseConfig()
    		{
    			require_once "inc/config.php";
    			$this->config = new Config;
    		}
    		public function AddURL($short_code, $full_url)
    		{
    			global $db;
    			$db->real_query("INSERT INTO `urls` (`original_url`, `short_code`, `active`) VALUES ('" . $full_url . "', '" . $short_code . "', '1')") or die($db->error);
    		}
    		public function IsAlreadyShortened($full_url)
    		{
    			global $db;
    			if($query = $db->query("SELECT `full_url` FROM `urls` WHERE `full_url` = '" . $full_url . "'"))
    			{
    				$num = $query->num_rows;
    				if($num > 0)
    				{
    					return true;
    				}
    			}
    			return false;
    		}
    		public function GetURLInfo($url, $info)
    		{
    			global $db;
    			
    			if($query = $db->query("SELECT `" . $info . "` FROM `urls` WHERE `original_url` = '" . $url . "'"))
    			{
    				while($dRow = $query->fetch_row())
    				{
    					$data = $dRow[0];
    				}
    				return $data;
    			}
    		}
    		public function GenerateShortURL()
    		{
    			$strs = "1234567890asdfghjklqwertyuiopzxcvbnm";
    			$random = '';
    			
    			for($i = 0; $i < 5; $i++)
    			{
    				$random .= $strs[mt_rand(0, strlen($strs) - 1)];
    			}
    			return $random;
    		}
    		public function IsAURL($url)
    		{
    			if(strpos($url, 'http://') !== false)
    			{
    				return true;
    			}
    			else if(strpos($url, 'https://') !== false)
    			{
    				return true;
    			}
    			return false;
    		}
    		public function GetFullFromShort($url)
    		{
    			global $db;
    			
    			if($query = $db->query("SELECT `original_url` FROM `urls` WHERE `short_code` = '" . $url . "'"))
    			{
    				while($data = $query->fetch_assoc())
    				{
    					$return = $data['original_url'];
    				}
    			}
    			return $return;
    		}
    		public function DoesShortURLExist($url)
    		{
    			global $db;
    			
    			if($query = $db->query("SELECT null FROM `urls` WHERE `short_code` = '" . $url . "'"))
    			{
    				$num = $query->num_rows;
    				
    				if($num >= 1)
    				{
    					return true;
    				}
    			}
    			return false;
    		}
    		public function GetNumberOfURLs()
    		{
    			global $db;
    			
    			if($query = $db->query("SELECT null FROM urls"))
    			{
    				return number_format($query->num_rows);
    			}
    			return 0;
    		}
    	}
    	
    ?>
    Download; Download SOSHORT.rar @ UppIT

    Have fun & good luck with php


  2. #2
    Coder/Programmer Toxic998 is offline
    MemberRank
    Nov 2012 Join Date
    USALocation
    215Posts

    Re: [Release] URL Shortner in OOP PHP

    Nice... I'm looking to make a shortening site, and have been looking for a code.
    Do i need a db with this?

  3. #3
    :joy: Jonteh is offline
    MemberRank
    Apr 2007 Join Date
    New York, USALocation
    3,375Posts

    Re: [Release] URL Shortner in OOP PHP

    Quote Originally Posted by Toxic998 View Post
    Nice... I'm looking to make a shortening site, and have been looking for a code.
    Do i need a db with this?
    Ya, here you go:

    Code:
    /*
    Navicat MySQL Data Transfer
    
    
    Source Server         : new
    Source Server Version : 50528
    Source Host           : localhost:12345
    Source Database       : soshort
    
    
    Target Server Type    : MYSQL
    Target Server Version : 50528
    File Encoding         : 65001
    
    
    Date: 2013-01-25 06:07:59
    */
    
    
    SET FOREIGN_KEY_CHECKS=0;
    
    
    -- ----------------------------
    -- Table structure for `urls`
    -- ----------------------------
    DROP TABLE IF EXISTS `urls`;
    CREATE TABLE `urls` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `original_url` text NOT NULL,
      `short_code` varchar(7) NOT NULL,
      `active` enum('0','1') NOT NULL DEFAULT '1',
      `clicks` int(11) NOT NULL DEFAULT '0',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
    
    -- ----------------------------
    -- Records of urls
    -- ----------------------------

  4. #4
    Coder/Programmer Toxic998 is offline
    MemberRank
    Nov 2012 Join Date
    USALocation
    215Posts

    Re: [Release] URL Shortner in OOP PHP

    Thanks again :)
    really appreciate it. You;ve really helped me out

  5. #5
    Pee Aitch Pee Dave is offline
    MemberRank
    Mar 2011 Join Date
    The NetherlandsLocation
    722Posts

    Re: [Release] URL Shortner in OOP PHP

    I have a few questions:
    - Why are you using global variables? Isn't the whole point of classes to -not- use globals?
    - Where is the protection against SQL injection? Or do you expect the user to check the variable before using your class?
    - Why is there a while loop in the function GetURLInfo and GetFullFromShort?
    - Don't use "or die($db->error)" (in the AddURL function) but add nice error handling.

    Looks fine next to that. :)

  6. #6

    Re: [Release] URL Shortner in OOP PHP

    nice :D

  7. #7
    :joy: Jonteh is offline
    MemberRank
    Apr 2007 Join Date
    New York, USALocation
    3,375Posts

    Re: [Release] URL Shortner in OOP PHP

    Quote Originally Posted by SuperWaffle View Post
    I have a few questions:
    - Why are you using global variables? Isn't the whole point of classes to -not- use globals?
    - Where is the protection against SQL injection? Or do you expect the user to check the variable before using your class?
    - Why is there a while loop in the function GetURLInfo and GetFullFromShort?
    - Don't use "or die($db->error)" (in the AddURL function) but add nice error handling.

    Looks fine next to that. :)
    - Using globals to make work easier. The point of the project was not for perfect PHP but to get it done so I could put the site online.
    - $db->real_escape_string.
    - MySQLi likes to use loops to fetch results
    - Meh, error handling isn't needed, there shouldn't be an SQL error, it was for debugging purposes

  8. #8
    Coder/Programmer Toxic998 is offline
    MemberRank
    Nov 2012 Join Date
    USALocation
    215Posts

    Re: [Release] URL Shortner in OOP PHP

    I just started using this. It looks great, better then most I've seen. I made a few small tweaks as to the CSS, and the page. But it's really fantastic overall, and am glad you shared with us.

  9. #9
    Banned bugme is offline
    BannedRank
    Feb 2007 Join Date
    1,380Posts

    Re: [Release] URL Shortner in OOP PHP

    Thanks for this I'm going to use it for my upcoming website so much thanks to you!

  10. #10
    Apprentice Crypt0nite is offline
    MemberRank
    Sep 2013 Join Date
    The NetherlandsLocation
    24Posts

    Re: [Release] URL Shortner in OOP PHP

    Quote Originally Posted by Jonteh View Post
    - Using globals to make work easier. The point of the project was not for perfect PHP but to get it done so I could put the site online.
    - $db->real_escape_string.
    - MySQLi likes to use loops to fetch results
    - Meh, error handling isn't needed, there shouldn't be an SQL error, it was for debugging purposes
    Actually, he's right. The use of globals isn't recommered when you're developing OOP.

    Btw, ever thought about using camelCase? This looks, ugh..

  11. #11
    Valued Member VladHQ is offline
    MemberRank
    Jun 2013 Join Date
    127.0.0.1Location
    133Posts

    Re: [Release] URL Shortner in OOP PHP

    So you check for a valid URL by trying to find if a URL includes http://? What if the URL is ftp://server/? What if the URL is http://' followed by an SQL injection? The least you could do is remove quotes/double-quotes from the URL.

  12. #12
    :joy: Jonteh is offline
    MemberRank
    Apr 2007 Join Date
    New York, USALocation
    3,375Posts

    Re: [Release] URL Shortner in OOP PHP

    Quote Originally Posted by Vlad1k View Post
    So you check for a valid URL by trying to find if a URL includes http://? What if the URL is ftp://server/? What if the URL is http://' followed by an SQL injection? The least you could do is remove quotes/double-quotes from the URL.
    I wrote this a while ago for a basic shortener for users of my website to use exclusively, but thanks :)

  13. #13
    Banned bugme is offline
    BannedRank
    Feb 2007 Join Date
    1,380Posts

    Re: [Release] URL Shortner in OOP PHP

    Is this still working because I've used it once and I'm pretty convinced about this so good work.

  14. #14
    Proficient Member H O R I Z O N is offline
    MemberRank
    Jul 2011 Join Date
    IndonesiaLocation
    176Posts

    Re: [Release] URL Shortner in OOP PHP

    Nice share really appreciate it.

  15. #15
    Novice jung3o is offline
    MemberRank
    Dec 2013 Join Date
    1Posts

    Re: [Release] URL Shortner in OOP PHP

    generating random string is a bad way to make url shortener. In most url shorteners, it uses 62 characters => a-z,A-Z,0-9
    so based on each new id (primary key), you can convert the id into the 62 characters (base 62)

    here's a good stackoverflow answer
    http://stackoverflow.com/questions/7...#answer-742047



Advertisement