Narzędzia użytkownika

Narzędzia witryny


narzedzia:php_url_short

To jest stara wersja strony!


Simple Flat-File URL Shortener

Skrypt w języku PHP pozwalający na skracanie długich adresów URL

Dostępny pod linkiem:

https://wiki.ostrowski.net.pl/urlshort

Kod rozwiązania:

index.php
<?php
/*
 * Simple Flat-File URL Shortener
 * Single PHP file solution using a JSON file for storage.
 * Usage:
 * 1. Place this file (e.g., index.php) on your server.
 * 2. Ensure the script has write permissions to the directory.
 * 3. Visit the script in your browser to shorten URLs.
 * 4. Access shortened URLs via: http://your-domain.com/index.php?c=SHORTCODE
 */
 
// Configuration
$storage_file = __DIR__ . '/urls.json';
$base_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http')
    . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
 
// Load existing URLs
$urls = [];
if (file_exists($storage_file)) {
    $json = file_get_contents($storage_file);
    $urls = json_decode($json, true) ?: [];
}
 
// Handle redirection if 'c' parameter is present
if (isset($_GET['c'])) {
    $code = preg_replace('/[^a-zA-Z0-9]/', '', $_GET['c']);
    if (isset($urls[$code])) {
        header('Location: ' . $urls[$code]);
        exit;
    } else {
        http_response_code(404);
        echo "<h1>404 Not Found</h1><p>Short URL not found.</p>";
        exit;
    }
}
 
// Handle form submission
$message = '';
$short_url = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['url'])) {
    $original_url = filter_var(trim($_POST['url']), FILTER_VALIDATE_URL);
    if ($original_url) {
        // Generate unique code
        do {
            $code = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 6);
        } while (isset($urls[$code]));
        // Save mapping
        $urls[$code] = $original_url;
        file_put_contents($storage_file, json_encode($urls, JSON_PRETTY_PRINT));
        $short_url = $base_url . '?c=' . $code;
    } else {
        $message = 'Please enter a valid URL.';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Simple URL Shortener</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 600px; margin: 50px auto; padding: 0 20px; }
        input[type="url"] { width: 80%; padding: 8px; }
        input[type="submit"] { padding: 8px 16px; }
        .message { margin: 20px 0; color: red; }
        .result { margin: 20px 0; }
    </style>
</head>
<body>
    <h1>Simple Flat-File URL Shortener</h1>
    <?php if ($message): ?>
        <div class="message"><?php echo htmlspecialchars($message); ?></div>
    <?php endif; ?>
    <form method="post">
        <input type="url" name="url" placeholder="Enter URL to shorten" required>
        <input type="submit" value="Shorten">
    </form>
    <?php if ($short_url): ?>
        <div class="result">
            Short URL: <a href="<?php echo $short_url; ?>" target="_blank"><?php echo $short_url; ?></a>
        </div>
    <?php endif; ?>
</body>
</html>
narzedzia/php_url_short.1747045099.txt.gz · ostatnio zmienione: przez administrator