From 4956afe2b83d4dabd3e63cb0043e40ca6076ece3 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Sat, 4 Oct 2025 10:27:48 +0000 Subject: [PATCH] URL Shortener First Vesion --- .htaccess | 18 +-- db/config.php | 54 ++++++--- index.php | 321 ++++++++++++++++++++++++++++---------------------- 3 files changed, 220 insertions(+), 173 deletions(-) diff --git a/.htaccess b/.htaccess index e2bbc23..5ce7ea5 100644 --- a/.htaccess +++ b/.htaccess @@ -1,18 +1,4 @@ -DirectoryIndex index.php index.html -Options -Indexes -Options -MultiViews - RewriteEngine On - -# 0) Serve existing files/directories as-is -RewriteCond %{REQUEST_FILENAME} -f [OR] -RewriteCond %{REQUEST_FILENAME} -d -RewriteRule ^ - [L] - -# 1) Internal map: /page or /page/ -> /page.php (if such PHP file exists) -RewriteCond %{REQUEST_FILENAME}.php -f -RewriteRule ^(.+?)/?$ $1.php [L] - -# 2) Optional: strip trailing slash for non-directories (keeps .php links working) +RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d -RewriteRule ^(.+)/$ $1 [R=301,L] +RewriteRule ^([^/]+)$ /index.php?code=$1 [L,QSA] \ No newline at end of file diff --git a/db/config.php b/db/config.php index 89075af..1d6b960 100644 --- a/db/config.php +++ b/db/config.php @@ -1,17 +1,41 @@ PDO::ERRMODE_EXCEPTION, - PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, - ]); - } - return $pdo; -} + static $pdoconn = null; + if ($pdoconn === null) { + $host = '127.0.0.1'; + $db = 'shortkenny'; + $user = 'user'; + $pass = 'password'; + $charset = 'utf8mb4'; + + $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; + $options = [ + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + PDO::ATTR_EMULATE_PREPARES => false, + ]; + try { + // Create database if it doesn't exist + $dbh = new PDO("mysql:host=$host", $user, $pass); + $dbh->exec("CREATE DATABASE IF NOT EXISTS `$db`;") + or die(print_r($dbh->errorInfo(), true)); + + $pdoconn = new PDO($dsn, $user, $pass, $options); + + // Create table + $pdoconn->exec(" + CREATE TABLE IF NOT EXISTS links ( + id INT AUTO_INCREMENT PRIMARY KEY, + original_url VARCHAR(2048) NOT NULL, + short_code VARCHAR(10) NOT NULL UNIQUE, + click_count INT DEFAULT 0, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + "); + + } catch ("PDOException" $e) { + throw new "::PDOException($e->getMessage(), (int)$e->getCode()); + } + } + return $pdoconn; +} \ No newline at end of file diff --git a/index.php b/index.php index 7205f3d..bcf97c2 100644 --- a/index.php +++ b/index.php @@ -1,150 +1,187 @@ prepare("SELECT original_url FROM links WHERE short_code = ?"); + $stmt->execute([$_GET['code']]); + $link = $stmt->fetch(); + + if ($link) { + $updateStmt = $pdo->prepare("UPDATE links SET click_count = click_count + 1 WHERE short_code = ?"); + $updateStmt->execute([$_GET['code']]); + header("Location: " . $link['original_url']); + exit(); + } +} + +// Handle form submission +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['url'])) { + $original_url = trim($_POST['url']); + + if (filter_var($original_url, FILTER_VALIDATE_URL)) { + $short_code = substr(md5(uniqid(rand(), true)), 0, 7); + + try { + $stmt = $pdo->prepare("INSERT INTO links (original_url, short_code) VALUES (?, ?)"); + $stmt->execute([$original_url, $short_code]); + + $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http"; + $host = $_SERVER['HTTP_HOST']; + $shortened_url = "$protocol://$host/$short_code"; + + if (!isset($_SESSION['recent_links'])) { + $_SESSION['recent_links'] = []; + } + array_unshift($_SESSION['recent_links'], ['short' => $shortened_url, 'original' => $original_url, 'clicks' => 0]); + $_SESSION['recent_links'] = array_slice($_SESSION['recent_links'], 0, 10); + + } catch (PDOException $e) { + $error_message = "Could not create short link. Please try again."; + } + } else { + $error_message = "Please enter a valid URL."; + } +} + +$recent_links = $_SESSION['recent_links'] ?? []; -$phpVersion = PHP_VERSION; -$now = date('Y-m-d H:i:s'); ?> - + - - - New Style - - - - - - - - - - - - - - - - - - - + + + ShortKenny - Minimal URL Shortener + + + + + + + + + + -
-
-

Analyzing your requirements and generating your website…

-
- Loading… -
-

AI is collecting your requirements and applying the first changes.

-

This page will update automatically as the plan is implemented.

-

Runtime: PHP — UTC

-
-
- + +
+
+

ShortKenny

+

A simple, fast, and reliable URL shortener.

+
+
+ +
+
+
+
+ + +
+
+ +
+
+ + +
+ + + +
+
Your short URL:
+
+ + +
+ +
+ +
+ + +
+

Recently Created Links (This Session)

+
+ + + + + + + + + + + + + + + + + +
Short URLOriginal URLClicks
+
+
+ + +
+ + + + + - + \ No newline at end of file