Add-cart.php — Num
The newline characters ( \r\n ) inject log entries, corrupting log files, evading intrusion detection systems, or filling disk space (log injection DoS).
false, 'message' => 'Method Not Allowed']); exit; // 3. Capture inputs and strictly enforce numeric casting $product_id = filter_input(INPUT_POST, 'product_id', FILTER_VALIDATE_INT); $quantity_num = filter_input(INPUT_POST, 'num', FILTER_VALIDATE_INT); // 4. Validate that numeric variables meet business thresholds if ($product_id === false || $product_id <= 0) http_response_code(400); echo json_encode(['success' => false, 'message' => 'Invalid product identifier.']); exit; if ($quantity_num === false || $quantity_num <= 0 || $quantity_num > 100) http_response_code(400); echo json_encode(['success' => false, 'message' => 'Quantity must be an integer between 1 and 100.']); exit; try // 5. Use Prepared Statements to securely verify product existence and price // This stops SQL Injection vulnerabilities dead in their tracks $stmt = $pdo->prepare("SELECT id, name, price, stock_qty FROM products WHERE id = :id LIMIT 1"); $stmt->execute([':id' => $product_id]); $product = $stmt->fetch(PDO::FETCH_ASSOC); if (!$product) http_response_code(404); echo json_encode(['success' => false, 'message' => 'Product not found.']); exit; // 6. Check inventory limits on the server side if ($product['stock_qty'] < $quantity_num) http_response_code(409); echo json_encode(['success' => false, 'message' => 'Requested quantity exceeds available stock.']); exit; // 7. Initialize the cart session structure if missing if (!isset($_SESSION['cart'])) $_SESSION['cart'] = []; // 8. Safely append or increment items using the product_id as the primary key if (isset($_SESSION['cart'][$product_id])) // Recalculate and enforce strict limits on cumulative totals $new_total_qty = $_SESSION['cart'][$product_id]['quantity'] + $quantity_num; if ($new_total_qty > $product['stock_qty']) http_response_code(409); echo json_encode(['success' => false, 'message' => 'Cannot add more. Inventory limit reached.']); exit; $_SESSION['cart'][$product_id]['quantity'] = $new_total_qty; else // Store only critical reference tokens in the session; do not trust client-side prices $_SESSION['cart'][$product_id] = [ 'id' => (int)$product['id'], 'name' => htmlspecialchars($product['name'], ENT_QUOTES, 'UTF-8'), 'price' => (float)$product['price'], 'quantity' => $quantity_num ]; // 9. Compute the collective item count for real-time front-end UI badges $total_cart_items = 0; foreach ($_SESSION['cart'] as $item) $total_cart_items += $item['quantity']; echo json_encode([ 'success' => true, 'message' => 'Product successfully added to your cart.', 'cart_count' => $total_cart_items ]); exit; catch (PDOException $e) // Log the error internally; do not expose internal structural database schemas to users error_log("Database Error inside add-cart.php: " . $e->getMessage()); http_response_code(500); echo json_encode(['success' => false, 'message' => 'An internal backend processing error occurred.']); exit; Use code with caution. 🔍 Code Breakdown and Best Practices Input Validation via filter_input add-cart.php num
.notification-success background: green; color: white; The newline characters ( \r\n ) inject log
// Function to get product details (example) function getProductDetails($product_id) // Replace with your database query $products = [ 1 => ['name' => 'Product 1', 'price' => 29.99, 'stock' => 50], 2 => ['name' => 'Product 2', 'price' => 49.99, 'stock' => 30], 3 => ['name' => 'Product 3', 'price' => 19.99, 'stock' => 100], ]; return isset($products[$product_id]) ? $products[$product_id] : null; Validate that numeric variables meet business thresholds if
