Różnice między wybraną wersją a wersją aktualną.
Poprzednia rewizja po obu stronachPoprzednia wersjaNowa wersja | Poprzednia wersja | ||
narzedzia:php_url_short [2025/05/12 12:56] – administrator | narzedzia:php_url_short [2025/05/16 18:49] (aktualna) – administrator | ||
---|---|---|---|
Linia 1: | Linia 1: | ||
- | ====== URL Shortener with Simple Flat-File storage ====== | + | ====== |
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 12: | Linia 12: | ||
<?php | <?php | ||
/* | /* | ||
- | * Simple Flat-File URL Shortener with Admin Panel | + | * Simple Flat-File URL Shortener with Admin Panel and Basic Authentication |
* Single PHP file solution using a JSON file for storage. | * Single PHP file solution using a JSON file for storage. | ||
* Features: | * Features: | ||
* - Shorten URLs | * - Shorten URLs | ||
* - Redirect shortened URLs | * - Redirect shortened URLs | ||
- | * - Admin view to list and delete entries | + | * - 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 25: | Linia 25: | ||
* 5. Visit the admin panel: http:// | * 5. Visit the admin panel: http:// | ||
*/ | */ | ||
+ | |||
+ | session_start(); | ||
// Configuration | // Configuration | ||
Linia 30: | 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 38: | Linia 94: | ||
} | } | ||
- | // Handle deletion in admin panel | ||
$message = ''; | $message = ''; | ||
- | if (isset($_GET[' | + | |
+ | // Handle deletion in admin panel | ||
+ | if ($view_admin && | ||
$code_to_delete = preg_replace('/ | $code_to_delete = preg_replace('/ | ||
if (isset($urls[$code_to_delete])) { | if (isset($urls[$code_to_delete])) { | ||
Linia 66: | Linia 123: | ||
// Handle form submission for shortening | // Handle form submission for shortening | ||
$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 81: | Linia 138: | ||
} | } | ||
} | } | ||
- | |||
- | // Determine view mode | ||
- | $view_admin = (isset($_GET[' | ||
?> | ?> | ||
< | < | ||
Linia 106: | Linia 160: | ||
< | < | ||
<div class=" | <div class=" | ||
- | <a href="<? | + | <a href="<? |
- | | + | |
- | | + | |
- | <a href="<? | + | <a href="<? |
- | | + | |
- | ) ?>"> | + | <a href="<? |
+ | <?php endif; ?> | ||
</ | </ | ||
Linia 118: | Linia 173: | ||
<?php endif; ?> | <?php endif; ?> | ||
- | <?php if ($view_admin): | + | <?php if ($view_admin |
< | < | ||
<?php if (empty($urls)): | <?php if (empty($urls)): | ||
Linia 151: | Linia 206: | ||
</ | </ | ||
</ | </ | ||
+ | |||
</ | </ | ||