Różnice między wybraną wersją a wersją aktualną.
Nowa wersja | Poprzednia wersja | ||
narzedzia:php_url_short [2025/05/12 12:18] – utworzono administrator | narzedzia:php_url_short [2025/05/16 18:49] (aktualna) – administrator | ||
---|---|---|---|
Linia 1: | Linia 1: | ||
- | ====== Simple Flat-File | + | ====== |
Skrypt w języku PHP pozwalający na skracanie długich adresów URL | Skrypt w języku PHP pozwalający na skracanie długich adresów URL | ||
Linia 5: | Linia 5: | ||
Dostępny pod linkiem: | Dostępny pod linkiem: | ||
- | https:// | + | https:// |
Kod rozwiązania: | Kod rozwiązania: | ||
Linia 12: | Linia 12: | ||
<?php | <?php | ||
/* | /* | ||
- | * Simple Flat-File URL Shortener | + | * Simple Flat-File URL Shortener |
* Single PHP file solution using a JSON file for storage. | * Single PHP file solution using a JSON file for storage. | ||
+ | * Features: | ||
+ | * - Shorten URLs | ||
+ | * - Redirect shortened URLs | ||
+ | * - Admin view with login/ | ||
* Usage: | * Usage: | ||
* 1. Place this file (e.g., index.php) on your server. | * 1. Place this file (e.g., index.php) on your server. | ||
Linia 19: | Linia 23: | ||
* 3. Visit the script in your browser to shorten URLs. | * 3. Visit the script in your browser to shorten URLs. | ||
* 4. Access shortened URLs via: http:// | * 4. Access shortened URLs via: http:// | ||
+ | * 5. Visit the admin panel: http:// | ||
*/ | */ | ||
+ | |||
+ | session_start(); | ||
// Configuration | // Configuration | ||
Linia 25: | Linia 32: | ||
$base_url = (isset($_SERVER[' | $base_url = (isset($_SERVER[' | ||
. '://' | . '://' | ||
+ | |||
+ | // Admin credentials | ||
+ | $admin_user = ' | ||
+ | $admin_pass = ' | ||
+ | |||
+ | // Handle logout | ||
+ | if (isset($_GET[' | ||
+ | session_destroy(); | ||
+ | header(' | ||
+ | exit; | ||
+ | } | ||
+ | |||
+ | // Show login form if accessing admin panel without authentication | ||
+ | $view_admin = (isset($_GET[' | ||
+ | if ($view_admin && !isset($_SESSION[' | ||
+ | $error = ''; | ||
+ | if ($_SERVER[' | ||
+ | if ($_POST[' | ||
+ | $_SESSION[' | ||
+ | header(' | ||
+ | exit; | ||
+ | } else { | ||
+ | $error = ' | ||
+ | } | ||
+ | } | ||
+ | ?> | ||
+ | < | ||
+ | <html lang=" | ||
+ | < | ||
+ | <meta charset=" | ||
+ | <meta name=" | ||
+ | < | ||
+ | < | ||
+ | body { font-family: | ||
+ | input { width: 100%; padding: 8px; margin: 8px 0; } | ||
+ | input[type=" | ||
+ | .error { color: red; } | ||
+ | </ | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | <?php if (!empty($error)): | ||
+ | <div class=" | ||
+ | <?php endif; ?> | ||
+ | <form method=" | ||
+ | < | ||
+ | < | ||
+ | <input type=" | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | <?php | ||
+ | exit; | ||
+ | } | ||
// Load existing URLs | // Load existing URLs | ||
Linia 31: | Linia 92: | ||
$json = file_get_contents($storage_file); | $json = file_get_contents($storage_file); | ||
$urls = json_decode($json, | $urls = json_decode($json, | ||
+ | } | ||
+ | |||
+ | $message = ''; | ||
+ | |||
+ | // Handle deletion in admin panel | ||
+ | if ($view_admin && isset($_GET[' | ||
+ | $code_to_delete = preg_replace('/ | ||
+ | if (isset($urls[$code_to_delete])) { | ||
+ | unset($urls[$code_to_delete]); | ||
+ | file_put_contents($storage_file, | ||
+ | $message = "Entry ' | ||
+ | } else { | ||
+ | $message = "Code not found."; | ||
+ | } | ||
} | } | ||
Linia 46: | Linia 121: | ||
} | } | ||
- | // Handle form submission | + | // Handle form submission |
- | $message = ''; | + | |
$short_url = ''; | $short_url = ''; | ||
- | if ($_SERVER[' | + | if ($_SERVER[' |
$original_url = filter_var(trim($_POST[' | $original_url = filter_var(trim($_POST[' | ||
if ($original_url) { | if ($original_url) { | ||
Linia 72: | Linia 146: | ||
< | < | ||
< | < | ||
- | body { font-family: | + | body { font-family: |
- | input[type=" | + | input[type=" |
input[type=" | input[type=" | ||
.message { margin: 20px 0; color: red; } | .message { margin: 20px 0; color: red; } | ||
.result { margin: 20px 0; } | .result { margin: 20px 0; } | ||
+ | table { width: 100%; border-collapse: | ||
+ | th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } | ||
+ | th { background-color: | ||
+ | .actions a { color: #c00; text-decoration: | ||
+ | .nav { margin-bottom: | ||
</ | </ | ||
</ | </ | ||
< | < | ||
- | <h1>Simple Flat-File | + | <div class=" |
+ | <a href="<? | ||
+ | <?php if (isset($_SESSION[' | ||
+ | <a href="<? | ||
+ | <a href="<? | ||
+ | <?php else: ?> | ||
+ | <a href="<? | ||
+ | <?php endif; ?> | ||
+ | </div> | ||
<?php if ($message): ?> | <?php if ($message): ?> | ||
<div class=" | <div class=" | ||
<?php endif; ?> | <?php endif; ?> | ||
- | | + | |
- | <input type=" | + | <?php if ($view_admin && isset($_SESSION[' |
- | <input type=" | + | < |
- | </ | + | <?php if (empty($urls)): |
- | <?php if ($short_url): | + | <p>No entries found.</ |
- | <div class=" | + | <?php else: ?> |
- | Short URL: <a href="<? | + | < |
- | </ | + | < |
+ | <?php foreach ($urls as $code => $url): ?> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | <td class=" | ||
+ | <a href="<? | ||
+ | </ | ||
+ | </ | ||
+ | <?php endforeach; ?> | ||
+ | </ | ||
+ | <?php endif; ?> | ||
+ | <?php else: ?> | ||
+ | < | ||
+ | | ||
+ | <input type=" | ||
+ | <input type=" | ||
+ | </ | ||
+ | <?php if ($short_url): | ||
+ | <div class=" | ||
+ | Short URL: <a href="<? | ||
+ | </div> | ||
+ | <?php endif; ?> | ||
<?php endif; ?> | <?php endif; ?> | ||
</ | </ | ||
</ | </ | ||
+ | |||
+ | |||
</ | </ | ||