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 +}