From 3d28e73369d4728ccc1e3fb03444ab99aca102a5 Mon Sep 17 00:00:00 2001 From: James Collins Date: Sat, 6 May 2023 18:29:03 +1000 Subject: [PATCH] added used counter --- ...082705_add_counter_to_shortlinks_table.php | 32 +++++++++++++++++++ public/shortlink.php | 9 ++++-- 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 database/migrations/2023_05_06_082705_add_counter_to_shortlinks_table.php diff --git a/database/migrations/2023_05_06_082705_add_counter_to_shortlinks_table.php b/database/migrations/2023_05_06_082705_add_counter_to_shortlinks_table.php new file mode 100644 index 0000000..d0a2cd1 --- /dev/null +++ b/database/migrations/2023_05_06_082705_add_counter_to_shortlinks_table.php @@ -0,0 +1,32 @@ +bigInteger('used')->default(0); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('shortlinks', function (Blueprint $table) { + // + }); + } +}; diff --git a/public/shortlink.php b/public/shortlink.php index cdb28f7..8212219 100644 --- a/public/shortlink.php +++ b/public/shortlink.php @@ -24,17 +24,20 @@ $code = $_SERVER['REQUEST_URI']; $code = trim($code, '/'); // lookup code in database -$sql = "SELECT url FROM shortlinks WHERE code = '$code'"; +$sql = "SELECT url, used FROM shortlinks WHERE code = '$code'"; $result = $conn->query($sql); -// if code is found, redirect to URL +// if code is found, redirect to URL and update 'used' column if ($result->num_rows > 0) { $row = $result->fetch_assoc(); $url = $row["url"]; + $used = $row["used"] + 1; + $updateSql = "UPDATE shortlinks SET used = $used WHERE code = '$code'"; + $conn->query($updateSql); header("Location: " . $url); exit(); } else { // if code is not found, redirect to default URL header("Location: https://www.stemmechanics.com.au/"); exit(); -} \ No newline at end of file +}