Reputation: 1065
I have searching functionality working with GET, like if I search for 'Apple', it will redirect to this particular URL: /search.php?search=Apple
.
Now if I add this product in cart, it will go to addtocart.php file & should redirect to the current page. In addtocart.php file I've code like this, if everything goes fine, it should go to this URL: $page?message=Product has been added to your cart
. Now the issue is as two '?' comes in URL, it doesn't display message on the first place & doesn't display the page with search results.
For finding the current page URL, I've this code.
$page = $_SERVER["REQUEST_URI"];
$page=(stristr($page,"/"));
$_SESSION['page'] = $page;
In addtocart.php file, I get the value of $page redirect to the same page after adding the product in cart. So I've this code for redirecting:
header ("Location: $page?message=Product added to your cart");
So ultimately URL that it redirects to looks like: search.php?search=apple?message=Product added to your cart
. And it doesn't actually pass the parameter & hence no searching can be done. Anything that I missed and you can help with?
Upvotes: 2
Views: 101
Reputation: 5238
$delimiter = parse_url($url, PHP_URL_QUERY) ? '&' : '?';
header("Location: {$page}{$delimiter}message=$message");
Also, you can use strrpos($url, '?') !== false
instead of parse_url($url, PHP_URL_QUERY) === ''
.
Upvotes: 0
Reputation: 514
When passing a second parameter you need to use &
instead of the second ?
.
The header path would then be:
header ("Location: $page&message=Product added to your cart");
If you might have some paths that don't include an initial value you can check for the presence of the the ?
and then alter the appending character.
E.g.
<?php if(preg_match('/\?/', $_SERVER['REQUEST_URI'])) { $page .= '&'; }
else{ $page .= '?'; }
$page .= "message=Product added to your cart";
header ("Location: $page");
?>
Upvotes: 1
Reputation: 3427
try this
header ("Location: $page&message=Product added to your cart");
Upvotes: 0