187 lines
8.4 KiB
PHP
187 lines
8.4 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
$pdo = db();
|
|
$shortened_url = null;
|
|
$error_message = null;
|
|
|
|
// Handle redirection
|
|
if (isset($_GET['code']) && !empty($_GET['code'])) {
|
|
$stmt = $pdo->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'] ?? [];
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>ShortKenny - Minimal URL Shortener</title>
|
|
<meta name="description" content="ShortKenny: Effortlessly shorten URLs for easy sharing and tracking in your web applications.">
|
|
<meta name="keywords" content="url shortener, link shortener, custom url, short url, link management, analytics, tracking, web application, minimal">
|
|
<meta property="og:title" content="ShortKenny">
|
|
<meta property="og:description" content="ShortKenny: Effortlessly shorten URLs for easy sharing and tracking in your web applications.">
|
|
<meta property="og:image" content="https://project-screens.s3.amazonaws.com/screenshots/34646/app-hero-20251004-102617.png">
|
|
<meta name="twitter:card" content="summary_large_image">
|
|
<meta name="twitter:image" content="https://project-screens.s3.amazonaws.com/screenshots/34646/app-hero-20251004-102617.png">
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
|
|
<style>
|
|
body { background-color: #f8f9fa; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; }
|
|
.hero { background: linear-gradient(135deg, #f8f9fa, #e9ecef); padding: 4rem 0; text-align: center; }
|
|
.hero h1 { font-family: 'Georgia', serif; font-weight: bold; }
|
|
.form-container { max-width: 600px; margin: 2rem auto; background: #fff; padding: 2rem; border-radius: 0.5rem; box-shadow: 0 4px 6px rgba(0,0,0,0.1); }
|
|
.result-container { display: none; }
|
|
.copy-btn { cursor: pointer; }
|
|
.table { margin-top: 2rem; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<header class="hero">
|
|
<div class="container">
|
|
<h1>ShortKenny</h1>
|
|
<p class="lead">A simple, fast, and reliable URL shortener.</p>
|
|
</div>
|
|
</header>
|
|
|
|
<main class="container">
|
|
<div class="form-container">
|
|
<form action="/" method="POST" id="shorten-form">
|
|
<div class="mb-3">
|
|
<label for="url" class="form-label">URL to Shorten</label>
|
|
<input type="url" class="form-control form-control-lg" id="url" name="url" placeholder="https://example.com/my-long-url" required pattern="https?://.+">
|
|
</div>
|
|
<div class="d-grid">
|
|
<button type="submit" class="btn btn-primary btn-lg">Shorten URL</button>
|
|
</div>
|
|
</form>
|
|
|
|
<?php if ($error_message): ?>
|
|
<div class="alert alert-danger mt-3"><?php echo htmlspecialchars($error_message); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($shortened_url): ?>
|
|
<div class="mt-4 p-3 bg-light rounded border" id="result-container">
|
|
<h5>Your short URL:</h5>
|
|
<div class="input-group">
|
|
<input type="text" class="form-control" value="<?php echo htmlspecialchars($shortened_url); ?>" readonly id="short-url-input">
|
|
<button class="btn btn-outline-secondary copy-btn" type="button" id="copy-button" title="Copy to clipboard">
|
|
<i class="bi bi-clipboard"></i>
|
|
</button>
|
|
</div>
|
|
<div id="copy-toast" class="toast align-items-center text-white bg-success border-0 position-fixed bottom-0 end-0 p-3" role="alert" aria-live="assertive" aria-atomic="true">
|
|
<div class="d-flex">
|
|
<div class="toast-body">
|
|
Copied to clipboard!
|
|
</div>
|
|
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<?php if (!empty($recent_links)): ?>
|
|
<div class="mt-5">
|
|
<h3 class="text-center mb-4">Recently Created Links (This Session)</h3>
|
|
<div class="table-responsive">
|
|
<table class="table table-striped table-hover">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th>Short URL</th>
|
|
<th>Original URL</th>
|
|
<th>Clicks</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($recent_links as $link): ?>
|
|
<tr>
|
|
<td><a href="<?php echo htmlspecialchars($link['short']); ?>" target="_blank"><?php echo htmlspecialchars($link['short']); ?></a></td>
|
|
<td class="text-truncate" style="max-width: 300px;"><a href="<?php echo htmlspecialchars($link['original']); ?>" target="_blank" title="<?php echo htmlspecialchars($link['original']); ?>"><?php echo htmlspecialchars($link['original']); ?></a></td>
|
|
<td><?php echo htmlspecialchars($link['clicks']); ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
</main>
|
|
|
|
<footer class="text-center py-4 mt-5">
|
|
<p>© <?php echo date("Y"); ?> ShortKenny. All rights reserved.</p>
|
|
</footer>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
const copyButton = document.getElementById('copy-button');
|
|
const shortUrlInput = document.getElementById('short-url-input');
|
|
const copyToastEl = document.getElementById('copy-toast');
|
|
const copyToast = new bootstrap.Toast(copyToastEl);
|
|
|
|
if (copyButton) {
|
|
copyButton.addEventListener('click', function () {
|
|
shortUrlInput.select();
|
|
document.execCommand('copy');
|
|
copyToast.show();
|
|
});
|
|
}
|
|
|
|
const form = document.getElementById('shorten-form');
|
|
const urlInput = document.getElementById('url');
|
|
form.addEventListener('submit', function(e) {
|
|
if (!urlInput.checkValidity()) {
|
|
e.preventDefault();
|
|
urlInput.classList.add('is-invalid');
|
|
} else {
|
|
urlInput.classList.remove('is-invalid');
|
|
}
|
|
});
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|