-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
49 lines (39 loc) · 1.12 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/*
Created by Prayogo
Descr:
This page was the index file from the simple shorten url program.
*/
//include UrlData class
require_once('UrlData.php');
$msg = "";
if (isset($_GET['key'])){
//GET Method
$key = $_GET['key'];
$data = new UrlData();
$url = $data->get($key);
header('Location: ' . $url);
} else if (isset($_POST["txtUrl"])){
//POST Method
if ($_POST["txtUrl"] == null || $_POST["txtUrl"] == ""){
$msg = "Url must be filled.";
}else{
$data = new UrlData();
//function generateKey return the new key, increment from the last key that store in database.
$data->key = $data->generateKey();
$data->url = $_POST["txtUrl"];
//function validateUrl return the url if the url is valid, and return empty string if url is not a valid url.
if ($data->validateUrl() == ""){
$msg = "Url that your input is not a valid url.";
}else {
$data->save();
$msg = "Your url has been shorten to " . "URLShort/index.php?key=" . $data->key;
}
}
}
?>
<form method="POST" action="index.php">
<input type="text" name="txtUrl">
<input type="submit" value="Shorten Url">
<label><?=$msg?></label>
</form>