-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_item_wishlist.php
61 lines (48 loc) · 2.02 KB
/
add_item_wishlist.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
session_start();
// Returning an error if an account is not logged in
if (!isset($_SESSION['id'])) {
// Go back to the store page
// Get the referer URL
$referer = $_SERVER['HTTP_REFERER'];
// Check if the referer has the "product_id" parameter
if (strpos($referer, 'product_id=') !== false) {
// If the "product_id" parameter is present, preserve only the "product_id" parameter
$refererParts = parse_url($referer);
$newReferer = $refererParts['scheme'] . '://' . $refererParts['host'] . $refererParts['path'] . '?product_id=' . $_GET['product_id'] ;
} else {
// If the "product_id" parameter is not present, remove all existing parameters
$newReferer = $referer;
}
// Add success parameter to the URL
$newReferer .= (strpos($newReferer, '?') === false ? '?' : '&') . 'error=Sign in or Create an Account to add items to your wishlist';
// Redirect to the new URL
header('Location: ' . $newReferer);
exit();
}
// Removing the item from the user's wishlist
$id = $_SESSION['id'];
$productID = $_GET['product_id'];
require_once 'db_conn.php';
$sql = "INSERT INTO wish_list_items (product_id, user_id) VALUES ($productID, $id)";
mysqli_query($conn, $sql);
//Add the item from the sesion wishlist
$_SESSION['wishlist'][] = $productID;
// Go back to the store page
// Get the referer URL
$referer = $_SERVER['HTTP_REFERER'];
// Check if the referer has the "product_id" parameter
if (strpos($referer, 'product_id=') !== false) {
// If the "product_id" parameter is present, preserve only the "product_id" parameter
$refererParts = parse_url($referer);
$newReferer = $refererParts['scheme'] . '://' . $refererParts['host'] . $refererParts['path'] . '?product_id=' . $_GET['product_id'] ;
} else {
// If the "product_id" parameter is not present, remove all existing parameters
$newReferer = $referer;
}
// Add success parameter to the URL
$newReferer .= (strpos($newReferer, '?') === false ? '?' : '&') . 'success=Your item has been added to the wishlist';
// Redirect to the new URL
header('Location: ' . $newReferer);
exit();
?>