added used counter
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('shortlinks', function (Blueprint $table) {
|
||||
$table->bigInteger('used')->default(0);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('shortlinks', function (Blueprint $table) {
|
||||
//
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user