Skip to content

Commit d336b7f

Browse files
committed
initial Commit
1 parent 1ca1a6c commit d336b7f

File tree

9 files changed

+254
-0
lines changed

9 files changed

+254
-0
lines changed

.editorconfig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
end_of_line = crlf
7+
charset = utf-8
8+
trim_trailing_whitespace = false
9+
insert_final_newline = false

.htaccess

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
RewriteEngine on
2+
rewriteRule ^([a-z0-9]+)$ redirect.php?code=$1

DB/shorten.sql

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
-- phpMyAdmin SQL Dump
2+
-- version 4.7.9
3+
-- https://www.phpmyadmin.net/
4+
--
5+
-- Host: 127.0.0.1
6+
-- Generation Time: Nov 11, 2018 at 10:23 PM
7+
-- Server version: 10.1.31-MariaDB
8+
-- PHP Version: 7.2.3
9+
10+
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
11+
SET AUTOCOMMIT = 0;
12+
START TRANSACTION;
13+
SET time_zone = "+00:00";
14+
15+
16+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
17+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
18+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
19+
/*!40101 SET NAMES utf8mb4 */;
20+
21+
--
22+
-- Database: `shorten`
23+
--
24+
25+
-- --------------------------------------------------------
26+
27+
--
28+
-- Table structure for table `links`
29+
--
30+
31+
CREATE TABLE `links` (
32+
`id` int(11) NOT NULL,
33+
`url` varchar(255) DEFAULT NULL,
34+
`code` varchar(12) DEFAULT NULL,
35+
`created` datetime DEFAULT NULL
36+
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
37+
38+
--
39+
-- Dumping data for table `links`
40+
--
41+
42+
INSERT INTO `links` (`id`, `url`, `code`, `created`) VALUES
43+
(10000000, 'http://google.com', 'google123', '2018-11-12 01:19:02'),
44+
(10000001, 'http://misbah.co', '0', '2018-11-12 01:25:02'),
45+
(10000002, 'http://vaiya.co', '5yc1u', '2018-11-12 01:25:46'),
46+
(10000003, 'http://gmail.com', '5yc1v', '2018-11-12 01:35:48'),
47+
(10000004, 'http://googlee.com', '5yc1w', '2018-11-12 02:19:46'),
48+
(10000005, 'http://twitter.com', '5yc1x', '2018-11-12 02:21:16'),
49+
(10000006, 'http://t.co', '5yc1y', '2018-11-12 02:27:40'),
50+
(10000007, 'http://vaia.co', '5yc1z', '2018-11-12 02:37:09'),
51+
(10000008, 'https://gmail.com', '5yc20', '2018-11-12 03:13:21');
52+
53+
--
54+
-- Indexes for dumped tables
55+
--
56+
57+
--
58+
-- Indexes for table `links`
59+
--
60+
ALTER TABLE `links`
61+
ADD PRIMARY KEY (`id`);
62+
63+
--
64+
-- AUTO_INCREMENT for dumped tables
65+
--
66+
67+
--
68+
-- AUTO_INCREMENT for table `links`
69+
--
70+
ALTER TABLE `links`
71+
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10000009;
72+
COMMIT;
73+
74+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
75+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
76+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

app/config.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
4+
define('DB_HOST', 'localhost');
5+
define('DB_USER', 'root');
6+
define('DB_PASS', '');
7+
define('DB_NAME', 'shorten');

classes/Shortener.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
require dirname(__FILE__).'../../app/config.php';
3+
4+
class Shortener {
5+
6+
protected $DB;
7+
8+
public function __construct(){
9+
$this->DB = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
10+
}
11+
12+
//generate a unique code for shortening url
13+
protected function generateCode($num){
14+
return base_convert($num, 10, 36);
15+
}
16+
17+
// the actual URL Shortening Function
18+
public function makeCode($url){
19+
20+
$url = trim($url);
21+
if(!filter_var($url, FILTER_VALIDATE_URL)){
22+
return '';
23+
}
24+
25+
$url = $this->DB->escape_string($url);
26+
$exist = $this->DB->query("SELECT code FROM links WHERE url = '{$url}'");
27+
28+
if($exist->num_rows){
29+
30+
return $exist->fetch_object()->code;
31+
32+
}else{
33+
34+
// insert record Without a code
35+
$this->DB->query("INSERT INTO links (url, created) VALUES('{$url}', now())");
36+
37+
//generate code Based on inserted ID
38+
$code = $this->generateCode($this->DB->insert_id);
39+
40+
$this->DB->query("UPDATE links SET code = '{$code}' WHERE url='{$url}'");
41+
42+
return $code;
43+
}
44+
}
45+
46+
public function getURL($code){
47+
$code = $this->DB->escape_string($code);
48+
$code = $this->DB->query("SELECT url FROM links WHERE code = '{$code}'");
49+
50+
if($code->num_rows){
51+
return $code->fetch_object()->url;
52+
}
53+
return '';
54+
}
55+
56+
57+
}

index.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
session_start();
3+
?>
4+
5+
<!DOCTYPE html>
6+
<html lang="en">
7+
<head>
8+
<meta charset="UTF-8">
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
10+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
11+
<title>URL Shortener</title>
12+
<link rel="stylesheet" href="./stylesheet/style.css">
13+
</head>
14+
<body>
15+
<div class="container">
16+
<div id="app">
17+
<h1>Shorten A URL</h1>
18+
<?php
19+
if(isset($_SESSION['feedback'])){
20+
echo "<p>{$_SESSION['feedback']}</p>";
21+
unset($_SESSION['feedback']);
22+
}
23+
?>
24+
<form action="shorten.php" method="POST">
25+
<input type="url" name="url" placeholder="Type a URL" autocomplete="off" required>
26+
<button type="submit">Shorten</button>
27+
</form>
28+
</div>
29+
</div>
30+
</body>
31+
</html>

redirect.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
require_once './classes/Shortener.php';
3+
4+
if(isset($_GET['code'])){
5+
6+
$s = new Shortener;
7+
8+
$code = $_GET['code'];
9+
10+
if($url = $s->getURL($code)){
11+
12+
header("location: {$url}");
13+
14+
die();
15+
}
16+
}
17+
header('location: index.php');

shorten.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
session_start();
3+
require_once './classes/Shortener.php';
4+
5+
$s = new Shortener;
6+
7+
if(isset($_POST['url'])){
8+
9+
$url = $_POST['url'];
10+
if($code = $s->makeCode($url)){
11+
$_SESSION['feedback'] = "Generated! your url is <a href=\"http://localhost/php-url-shortener/{$code}\">http://localhost/php-url-shortener/{$code}</a>";
12+
}else{
13+
$_SESSION['feedback'] = "There was a problem to Shorten your URL. invalid URL, Perhaps?";
14+
}
15+
}
16+
header('Location: ./index.php');

stylesheet/style.css

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
body{
2+
background: #f5f5f5;
3+
font-family: tahoma, sans-serif;
4+
font-size: 0.9em;
5+
}
6+
h1{
7+
font-weight: normal;
8+
}
9+
.container{
10+
width: 100%;
11+
max-width: 600px;
12+
text-align: center;
13+
margin: 20px auto;
14+
padding: 10px;
15+
}
16+
#app{
17+
padding: 15px 15px 40px 15px;
18+
border-radius: 8px;
19+
box-shadow: 0 0 30px rgba(0,0,0, .1);
20+
background-color: #fff;
21+
}
22+
input{
23+
width: 300px;
24+
padding: 12px;
25+
}
26+
button{
27+
padding: 12px 20px;
28+
cursor: pointer;
29+
}
30+
input, button{
31+
border: 1px solid #ccc;
32+
margin: 0;
33+
display: inline-block;
34+
background-color: #fff;
35+
transition: all .3s;
36+
}
37+
button:hover{
38+
background-color: #ccc;
39+
}

0 commit comments

Comments
 (0)