Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP, header(redirect) not working on live server

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 put header() the item won't be deleted, the page only reloads without any action taken effect. If I don't put the header(), 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

like image 614
Ovidiu Avatar asked Jun 02 '26 06:06

Ovidiu


2 Answers

Use ob_start() before header('location: cart.php');

like image 198
Shijin TR Avatar answered Jun 03 '26 21:06

Shijin TR


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>

like image 27
Vlad Avatar answered Jun 03 '26 19:06

Vlad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!