PHP, header(redirect) not working on live server
Solution 1
Why not use a meta redirect tag, or a javascript solution?
HTML:
<meta http-equiv="refresh" content="0;url=http://www.site.com/cart.php">
JavaScript #1:
<script>window.location = "http://www.site.com/cart.php";</script>
JavaScript #2: <script>window.navigate("http://www.site.com/cart.php");</script>
Solution 2
Use ob_start()
before header('location: cart.php');
Solution 3
Solution: Update the PHP version
I had the same issue with my hosting,
- Header redirect not working
- Issue with SESSION
- etc
I got it solved by the hosting provider by updating the version of PHP
He just changed the php version from php 5.4 to 5.6 and it might have fixed the issue
So Ask your Hosting Provider to Update the PHP version.
Solution 4
put ob_start()
in the first line of your code,
ob_start();
if(isset($_GET['elimina_id'])){
if (in_array($_GET['elimina_id'], $_SESSION['cart']) ) {
$index = (array_keys($_SESSION['cart'], $_GET['elimina_id']));
$i = $index[0];
unset($_SESSION['cart'][$i]);
header('location: cart.php');
}
}
Solution 5
do like this
<?php
ob_start();
if(isset($_GET['elimina_id'])){
if (in_array($_GET['elimina_id'], $_SESSION['cart']) ) {
$index = (array_keys($_SESSION['cart'], $_GET['elimina_id']));
$i = $index[0];
unset($_SESSION['cart'][$i]);
header('location: cart.php');
}
}
ob_end_flush();
?>
`
Ovidiu
Updated on June 04, 2022Comments
-
Ovidiu almost 2 years
I have this code
if(isset($_GET['elimina_id'])){ if (in_array($_GET['elimina_id'], $_SESSION['cart']) ) { $index = (array_keys($_SESSION['cart'], $_GET['elimina_id'])); $i = $index[0]; unset($_SESSION['cart'][$i]); header('location: cart.php'); } }
Basically, I want to delete a item in the cart. The thing is, on the test page, localhost, everything works, but on live server I have problems with
header()
. If I putheader()
the item won't be deleted, the page only reloads without any action taken effect. If I don't put theheader()
, after I click the 'Delete item' link, nothing happens, but then if I manually reload the page it works, the item deletes. On localhost I don't have this problem, what could it be? The php version is OK, could it be some settings in the .ini file ? Hope you can help me, Thanks -
Ovidiu about 11 yearsSorry, ob_start() worked indeed, but now all items in the cart are deleted if I click 'Delete item', instead of just the one I clicked
-
Ovidiu about 11 yearsThank you, this worked (only the HTML method), the only glitch is that during the redirect the table resizes, it becomes twice width/height. What I still don't understand is why header() doesn't work