瀏覽代碼

Using shorter urls (only 5 characters) instead of the 64 for better sharing and reading

sebastian 1 周之前
父節點
當前提交
55e32e4583
共有 2 個文件被更改,包括 8 次插入4 次删除
  1. 7 3
      list.php
  2. 1 1
      packing.php

+ 7 - 3
list.php

@@ -32,8 +32,8 @@ if (empty($validIds)) {
 sort($validIds);
 $idsStr = implode(',', $validIds);
 
-// Generate deterministic hash from the sorted activity IDs
-$hash = hash('sha256', $idsStr);
+// Generate deterministic 5-character hash from the sorted activity IDs
+$hash = substr(hash('sha256', $idsStr), 0, 5);
 
 // Check if this exact selection already has a hash stored
 $stmt = $pdo->prepare("SELECT hash FROM selection_hashes WHERE activity_ids = ?");
@@ -41,7 +41,11 @@ $stmt->execute([$idsStr]);
 $existing = $stmt->fetchColumn();
 
 if ($existing) {
-    $hash = $existing;
+    if ($existing !== $hash) {
+        // Overwrite old (long) hash with the new short hash
+        $stmt = $pdo->prepare("UPDATE selection_hashes SET hash = ? WHERE activity_ids = ?");
+        $stmt->execute([$hash, $idsStr]);
+    }
 } else {
     // Store new hash
     $stmt = $pdo->prepare("INSERT INTO selection_hashes (hash, activity_ids) VALUES (?, ?)");

+ 1 - 1
packing.php

@@ -6,7 +6,7 @@ require_once __DIR__ . '/db.php';
 $pdo = get_pdo();
 
 $hash = isset($_GET['h']) ? trim($_GET['h']) : '';
-if (!preg_match('/^[a-f0-9]{64}$/', $hash)) {
+if (!preg_match('/^[a-f0-9]{5}$/', $hash)) {
     http_response_code(400);
     die('<p>Invalid or missing list hash.</p>');
 }