-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
111 lines (99 loc) · 3.79 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
// Require the configuration file
require "config.php";
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);
try {
// Database connection
$host = "localhost";
$dbname = "short-urls";
$user = "root";
$pass = "";
// Create a new PDO instance
$conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Check if the form is submitted
if (isset($_POST['submit'])) {
if ($_POST['url'] == '') {
echo "The input is empty";
} else {
$url = $_POST['url'];
// Check if the URL already exists
$check = $conn->prepare("SELECT COUNT(*) FROM urls WHERE url = :url");
$check->execute(["url" => $url]);
$urlExists = $check->fetchColumn();
if ($urlExists) {
echo "The URL already exists";
} else {
// Prepare and execute the insert query
$insert = $conn->prepare("INSERT INTO urls (url) VALUES (:url)");
$insert->execute(["url" => $url]);
}
}
}
// Fetch URLs from the database
$stmt = $conn->query("SELECT * FROM urls");
$rows = $stmt->fetchAll(PDO::FETCH_OBJ);
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
body { overflow: hidden; }
.margin { margin-top: 200px }
</style>
</head>
<body>
<div class="container">
<div class="row gx-4 gx-lg-5 justify-content-center">
<div class="col-md-10 col-lg-8 col-xl-7">
<form class="card p-2 margin" method="POST" action="index.php">
<div class="input-group">
<input type="text" name="url" class="form-control" placeholder="Your URL">
<div class="input-group-append">
<button type="submit" name="submit" class="btn btn-success">Shorten</button>
</div>
</div>
</form>
</div>
</div>
</div>
<div class="container mt-4">
<div class="row gx-4 gx-lg-5 justify-content-center">
<div class="col-md-10 col-lg-8 col-xl-7">
<table class="table">
<thead>
<tr>
<th scope="col">Long URL</th>
<th scope="col">Short URL</th>
<th scope="col">Clicks</th>
</tr>
</thead>
<tbody>
<?php if (!empty($rows)) : ?>
<?php foreach ($rows as $row) : ?>
<tr>
<td><?php echo $row->url; ?></td>
<td><a href="http://localhost/short-urls/u?id=<?php echo $row->id;?>" target="_blank">http://localhost/short-urls/<?php echo $row->id;?></a></td>
<td><?php echo $row->clicks; ?></td>
</tr>
<?php endforeach; ?>
<?php else : ?>
<tr><td colspan="3">No URLs found.</td></tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
</body>
</html>