create link_reducer project

This commit is contained in:
Tutur33
2023-12-01 20:42:23 +01:00
parent 86af53f206
commit 2240bf4454
4 changed files with 104 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>URL Shortener</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h2>URL Shortener</h2>
<form method="post">
<input type="url" name="original_url" placeholder="Enter URL" required>
<button type="submit" name="shorten">Shorten</button>
</form>
<?php
if (isset($_POST['shorten'])) {
$original_url = $_POST['original_url'];
$shortened_url = shortenURL($original_url);
echo "<p>Shortened URL: <a href='$shortened_url'>$shortened_url</a></p>";
}
function shortenURL($url)
{
$links = file_get_contents('links.json');
$links = json_decode($links, true);
if (!$links) {
$links = array();
}
$key = generateKey();
$links[$key] = $url;
file_put_contents('links.json', json_encode($links));
return "http://" . $_SERVER['HTTP_HOST'] . "/redirect.php?key=$key";
}
function generateKey()
{
return substr(md5(uniqid(rand(), true)), 0, 6);
}
?>
</body>
</html>
+1
View File
@@ -0,0 +1 @@
{"0e6841":"https:\/\/github.com\/Tutur33"}
+18
View File
@@ -0,0 +1,18 @@
<?php
if (isset($_GET['key'])) {
$key = $_GET['key'];
$links = file_get_contents('links.json');
$links = json_decode($links, true);
if (array_key_exists($key, $links)) {
$original_url = $links[$key];
header("Location: $original_url");
exit();
} else {
echo "Invalid URL key.";
}
} else {
echo "Key parameter is missing.";
}
?>
+38
View File
@@ -0,0 +1,38 @@
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h2 {
text-align: center;
}
form {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
margin-bottom: 20px;
}
form input[type="url"],
form button {
margin: 5px;
padding: 8px;
border-radius: 5px;
border: 1px solid #ccc;
}
p {
text-align: center;
margin-bottom: 20px;
}
a {
color: blue;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}