header('location: ..') not working

86,508

Solution 1

add this code to first of code:

<?php
ob_start();
?>

Solution 2

You can try the javascript way of redirecting the page:

echo '<script>window.location = "'.$url.'";</script>';

Solution 3

Just try this. it will work

echo "<script type='text/javascript'>  window.location='index.php'; </script>";

instead of header("location: index.php");

Solution 4

Does simple PHP script like the following work?

<?php header('Location: /index.php?page=login'); ?>

You also have to make sure that the code path with $path_choice != null and $form->is_connected() === true is actually taken because your error states don't set HTTP headers at all.

Solution 5

I see a 'destroy_session()', are you using session_start() somewhere? Make sure that NO content (including (session)cookies) is already sent.

Tips for debugging: set error_reporting(E_ALL) (including E_NOTICE). That'll give you the line on which the headers are sent.

Other sources of trouble: - BOM - extra lines before in an included file

Share:
86,508
Fortisimo
Author by

Fortisimo

Updated on February 20, 2020

Comments

  • Fortisimo
    Fortisimo about 4 years

    (1)I'm in the process of uploading my website to a remote web server.

    (2)The site's template system is set up in a way that all of the pages are formed by sending url-encoded get requests to index.php

    (3)Loading up the initial page works. This page determines the location of the next page by evaluating the value of its form.

    (4)The redirection to the next page is performed by doing a: header('location: next_page')

    (5)For some reason, the redirection is not performed. Here's what the code looks like:

    $error = "";
    if(isset($_POST['index_choice'])){
        $path_choice = isset($_POST['path']) ? $_POST['path'] : NULL;
    
        //echo $path_choice;
        //echo $page_inc;
    
        //nothing after this
    
        if($path_choice != null){
    
            if($form->is_connected()){
    
                //if($path_choice != "" || $path_choice != NULL){
                    if($path_choice == "new"){
    
                        //header('location: /login.php');
                        //header('location: page/login');
                        header('location: /index.php?page=login');
                        exit();
    
                    }
                    else{
    
                        //header('location: /amend.php');
                        //header('location: page/amend');
                        header('location: /index.php?page=amend');
                        exit();
                    }
                //}
                /**
                else{
                    //destroy_session();
                    $error = "You haven't selected a path. Please choose a path";
                }
                 *
                 */
            }
            else{
                //destroy_session();
                $error = "Problems with connecting to the database";
            }
        }else{
            //destroy_session();
            $error = "You have not indicated your choice";
        }
    
    }
    

    SOLVED

    It was a matter of having a blank space after a ?> somewhere else in the code. This was revealed to me after placing the following commands at the top of the code:

     error_reporting(E_ALL); ini_set('display_errors', 'On'); 
    

    I'd like to say thanks to all of the people that have tried to help.