From 2240bf44545b35730a97667e8effbfa7858321ef Mon Sep 17 00:00:00 2001 From: Tutur33 Date: Fri, 1 Dec 2023 20:42:23 +0100 Subject: [PATCH] create link_reducer project --- Activity_PHP/link_reducer/index.php | 47 ++++++++++++++++++++++++++ Activity_PHP/link_reducer/links.json | 1 + Activity_PHP/link_reducer/redirect.php | 18 ++++++++++ Activity_PHP/link_reducer/style.css | 38 +++++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 Activity_PHP/link_reducer/index.php create mode 100644 Activity_PHP/link_reducer/links.json create mode 100644 Activity_PHP/link_reducer/redirect.php create mode 100644 Activity_PHP/link_reducer/style.css diff --git a/Activity_PHP/link_reducer/index.php b/Activity_PHP/link_reducer/index.php new file mode 100644 index 0000000..9827ead --- /dev/null +++ b/Activity_PHP/link_reducer/index.php @@ -0,0 +1,47 @@ + + + + + + URL Shortener + + + +

URL Shortener

+
+ + +
+ + Shortened URL: $shortened_url

"; + } + + 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); + } + ?> + + \ No newline at end of file diff --git a/Activity_PHP/link_reducer/links.json b/Activity_PHP/link_reducer/links.json new file mode 100644 index 0000000..7e8ae11 --- /dev/null +++ b/Activity_PHP/link_reducer/links.json @@ -0,0 +1 @@ +{"0e6841":"https:\/\/github.com\/Tutur33"} \ No newline at end of file diff --git a/Activity_PHP/link_reducer/redirect.php b/Activity_PHP/link_reducer/redirect.php new file mode 100644 index 0000000..d14c1b2 --- /dev/null +++ b/Activity_PHP/link_reducer/redirect.php @@ -0,0 +1,18 @@ + diff --git a/Activity_PHP/link_reducer/style.css b/Activity_PHP/link_reducer/style.css new file mode 100644 index 0000000..9952188 --- /dev/null +++ b/Activity_PHP/link_reducer/style.css @@ -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; +}