<?php
session_start();

if (!isset($_SESSION['cart'])) {
    $_SESSION['cart'] = [];
}

if (isset($_POST['title'])) {
    $book = [
        'title'  => $_POST['title'],
        'author' => $_POST['author'],
        'image'  => $_POST['image']
    ];

    $exists = false;
    foreach ($_SESSION['cart'] as $item) {
        if ($item['title'] == $book['title']) { $exists = true; break; }
    }
    if (!$exists) $_SESSION['cart'][] = $book;
}

$back = isset($_POST['current_url']) && $_POST['current_url'] != '' ? $_POST['current_url'] : 'index.php';
header('Location: ' . $back);
exit;
