问题
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
回答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>
回答2:
Use ob_start() before header('location: cart.php');
回答3:
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');
}
}
回答4:
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();
?>
`
回答5:
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.
回答6:
this will work
<script type="text/javascript">
<!--
window.location="http://www.newlocation.com";
//-->
</script>
来源:https://stackoverflow.com/questions/16358361/php-headerredirect-not-working-on-live-server