Daniele Cinà
Daniele Cinà

Reputation: 21

Header Location not working after 3 times

I'm using the function header("Location: example.php") on a page that runs a MySQL query that creates a photogallery and then redirects to the page of the photogallery just created itself. The problem is that, after having created 3 or 4 photogalleries this error shows up:

"Cannot modify header information - headers already sent by ..."

I know that before header() shouldn't be any output, but my question is: why it does work for a few times and then it fails?

<?php 
if(isset($_POST['crea'])){

    $nome_lista = $_POST['nome_lista'];
    $slug_lista = str_replace(' ','_',$nome_lista);

    $luogo_lista = $_POST['luogo_lista'];

    $inserisci = mysql_query("INSERT INTO `prova`(`id_lista`, `nome_lista`,`slug_lista`, `id_utente`, `luogo_lista`,`copertina`) VALUES (NULL,'$nome_lista','$slug_lista','$user_id','$luogo_lista','copertina.jpg')");

    $user_id = $_SESSION['id_utente'];

    $id_lista = mysql_query("
    SELECT id_lista 
    FROM prova 
    WHERE id_utente ='$user_id'
    ORDER BY id_lista DESC");

    $ultima_lista = mysql_result($id_lista,0); 

    if($inserisci){
        $percorso_album = "./img_globali/".$user_id.$username;
        mkdir($percorso_album."/".$slug_lista, 0775);
    }else{
        echo 'Non ho inserito la cartella del nuovo album '.mysql_error();
    } 
    header("location: album.php?id=".$ultima_lista);
    exit;
}
?>

The query is executed, even though the page doesn't redirect

Upvotes: 2

Views: 356

Answers (4)

LucBERTON
LucBERTON

Reputation: 1

Had a specificly similar issue today of header(Location...) not working after 3 times.

I realized that my issue was that i was inside an iframe, so the redirect kept me inside the iframe and adding a new stack...

After redirecting twice i had an iframe inside an iframe inside a iframe... I guess that's why the redirect was not working the 3rd time.

Instead you should redirect to top location :

echo("<script> window.top.location.href = 'myPage.php'</script>");

(I echoed the JS because my controller was not displaying anything in a view)

Hope it helps someone

Upvotes: 0

Ulver
Ulver

Reputation: 905

In your condition the output is sent in else block only. So you will not get that error when $inserisci is true.

if ($inserisci){
    // No output here, so error will happen when in this block
    $percorso_album = "./img_globali/".$user_id.$username;
    mkdir($percorso_album."/".$slug_lista, 0775);       
 }else{
     // You have output here, hence "header already sent" error
     echo 'Non ho inserito la cartella del nuovo album '.mysql_error();

 } 
 header("location: album.php?id=".$ultima_lista);

Upvotes: 1

Hitesh Vala Ahir
Hitesh Vala Ahir

Reputation: 793

header("Location:album.php?id=$ultima_lista");

Upvotes: 0

Evan McMahon
Evan McMahon

Reputation: 99

The headers must be sent before any content, remove any echo commands and it will work. Also, ensure there is no white space or HTML before this section of PHP code.

You can also turn on output buffering using ob_start(); at the very top of your PHP code, and then the headers will be sent before any echo'd variables.

Upvotes: 2

Related Questions