Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[Release] URL Shortner in OOP PHP

Joined
Apr 30, 2007
Messages
2,339
Reaction score
1,547
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;

Have fun & good luck with php
 
Junior Spellweaver
Joined
Nov 5, 2012
Messages
191
Reaction score
17
Nice... I'm looking to make a shortening site, and have been looking for a code.
Do i need a db with this?
 
Joined
Apr 30, 2007
Messages
2,339
Reaction score
1,547
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
-- ----------------------------
 
Junior Spellweaver
Joined
Nov 5, 2012
Messages
191
Reaction score
17
Thanks again :)
really appreciate it. You;ve really helped me out
 
Pee Aitch Pee
Joined
Mar 30, 2011
Messages
630
Reaction score
422
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. :)
 
Joined
Apr 30, 2007
Messages
2,339
Reaction score
1,547
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
 
Junior Spellweaver
Joined
Nov 5, 2012
Messages
191
Reaction score
17
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.
 
Banned
Banned
Joined
Feb 9, 2007
Messages
1,313
Reaction score
177
Thanks for this I'm going to use it for my upcoming website so much thanks to you!
 
Newbie Spellweaver
Joined
Sep 16, 2013
Messages
24
Reaction score
4
- 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..
 
Junior Spellweaver
Joined
Jun 8, 2013
Messages
126
Reaction score
22
So you check for a valid URL by trying to find if a URL includes http://? What if the URL is ? What if the URL is followed by an SQL injection? The least you could do is remove quotes/double-quotes from the URL.
 
Joined
Apr 30, 2007
Messages
2,339
Reaction score
1,547
So you check for a valid URL by trying to find if a URL includes http://? What if the URL is ? What if the URL is 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 :)
 
Banned
Banned
Joined
Feb 9, 2007
Messages
1,313
Reaction score
177
Is this still working because I've used it once and I'm pretty convinced about this so good work.
 
Initiate Mage
Joined
Dec 25, 2013
Messages
1
Reaction score
1
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
 
Back
Top