Narzędzia użytkownika

Narzędzia witryny


narzedzia:php_url_short

Różnice

Różnice między wybraną wersją a wersją aktualną.

Odnośnik do tego porównania

Poprzednia rewizja po obu stronachPoprzednia wersja
Nowa wersja
Poprzednia wersja
narzedzia:php_url_short [2025/05/12 12:56] administratornarzedzia:php_url_short [2025/05/16 18:49] (aktualna) administrator
Linia 1: Linia 1:
-====== URL Shortener with Simple Flat-File storage ======+====== PHP: 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/password protection, list and delete entries
  * 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://your-domain.com/index.php?view=admin  * 5. Visit the admin panel: http://your-domain.com/index.php?view=admin
  */  */
 +
 +session_start();
  
 // Configuration // Configuration
Linia 30: Linia 32:
 $base_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') $base_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http')
     . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];     . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
 +
 +// Admin credentials
 +$admin_user = 'admin';        // Change to desired username
 +$admin_pass = 'password123';  // Change to desired password
 +
 +// Handle logout
 +if (isset($_GET['action']) && $_GET['action'] === 'logout') {
 +    session_destroy();
 +    header('Location: ' . $base_url);
 +    exit;
 +}
 +
 +// Show login form if accessing admin panel without authentication
 +$view_admin = (isset($_GET['view']) && $_GET['view'] === 'admin');
 +if ($view_admin && !isset($_SESSION['authenticated'])) {
 +    $error = '';
 +    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['username'], $_POST['password'])) {
 +        if ($_POST['username'] === $admin_user && $_POST['password'] === $admin_pass) {
 +            $_SESSION['authenticated'] = true;
 +            header('Location: ' . $base_url . '?view=admin');
 +            exit;
 +        } else {
 +            $error = 'Invalid username or password.';
 +        }
 +    }
 +    ?>
 +    <!DOCTYPE html>
 +    <html lang="en">
 +    <head>
 +        <meta charset="UTF-8">
 +        <meta name="viewport" content="width=device-width,initial-scale=1">
 +        <title>Admin Login</title>
 +        <style>
 +            body { font-family: Arial, sans-serif; max-width: 400px; margin: 50px auto; padding: 0 20px; }
 +            input { width: 100%; padding: 8px; margin: 8px 0; }
 +            input[type="submit"] { width: auto; }
 +            .error { color: red; }
 +        </style>
 +    </head>
 +    <body>
 +        <h1>Admin Login</h1>
 +        <?php if (!empty($error)): ?>
 +            <div class="error"><?php echo htmlspecialchars($error); ?></div>
 +        <?php endif; ?>
 +        <form method="post">
 +            <label>Username:<br><input type="text" name="username" required></label>
 +            <label>Password:<br><input type="password" name="password" required></label>
 +            <input type="submit" value="Login">
 +        </form>
 +    </body>
 +    </html>
 +    <?php
 +    exit;
 +}
  
 // Load existing URLs // Load existing URLs
Linia 38: Linia 94:
 } }
  
-// Handle deletion in admin panel 
 $message = ''; $message = '';
-if (isset($_GET['delete'])) {+ 
 +// Handle deletion in admin panel 
 +if ($view_admin && isset($_GET['delete']) && isset($_SESSION['authenticated'])) {
     $code_to_delete = preg_replace('/[^a-zA-Z0-9]/', '', $_GET['delete']);     $code_to_delete = preg_replace('/[^a-zA-Z0-9]/', '', $_GET['delete']);
     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['REQUEST_METHOD'] === 'POST' && !empty($_POST['url'])) {+if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['url']) && !$view_admin) {
     $original_url = filter_var(trim($_POST['url']), FILTER_VALIDATE_URL);     $original_url = filter_var(trim($_POST['url']), FILTER_VALIDATE_URL);
     if ($original_url) {     if ($original_url) {
Linia 81: Linia 138:
     }     }
 } }
- 
-// Determine view mode 
-$view_admin = (isset($_GET['view']) && $_GET['view'] === 'admin'); 
 ?> ?>
 <!DOCTYPE html> <!DOCTYPE html>
Linia 106: Linia 160:
 <body> <body>
     <div class="nav">     <div class="nav">
-        <a href="<?= htmlspecialchars( +        <a href="<?= htmlspecialchars($base_url) ?>">Shorten URL</a>
-            $base_url +        <?php if (isset($_SESSION['authenticated'])): ?> 
-        ) ?>">Shorten URL</a>+            <a href="<?= htmlspecialchars($base_url . '?view=admin') ?>">Admin Panel</a>
-        <a href="<?= htmlspecialchars( +            <a href="<?= htmlspecialchars($base_url . '?action=logout') ?>">Logout</a> 
-            $base_url . '?view=admin' +        <?php else: ?> 
-        ) ?>">Admin Panel</a>+            <a href="<?= htmlspecialchars($base_url . '?view=admin') ?>">Admin Login</a
 +        <?php endif; ?>
     </div>     </div>
  
Linia 118: Linia 173:
     <?php endif; ?>     <?php endif; ?>
  
-    <?php if ($view_admin): ?>+    <?php if ($view_admin && isset($_SESSION['authenticated'])): ?>
         <h1>Admin Panel</h1>         <h1>Admin Panel</h1>
         <?php if (empty($urls)): ?>         <?php if (empty($urls)): ?>
Linia 151: Linia 206:
 </body> </body>
 </html> </html>
 +
  
  
 </code> </code>
  
narzedzia/php_url_short.1747047385.txt.gz · ostatnio zmienione: przez administrator