Fortisimo
Fortisimo

Reputation: 1002

header('location: ..') not working

(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.

Upvotes: 14

Views: 88079

Answers (11)

Harpal Singh
Harpal Singh

Reputation: 702

Make sure there should be no whitespace at the beginning of your code

Eg:

------whitespace----
<?php
.
.
.
. ?>

Remove space from first line such that <?php must be in first line.

Upvotes: 0

Hariharaselvam
Hariharaselvam

Reputation: 163

Just try this. it will work

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

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

Upvotes: 4

Clayton Kappaz
Clayton Kappaz

Reputation: 1

Well, if you tried everything, this example can help (HTML/PHP):

<?php
    header("HTTP/1.0 404 Not Found");
    echo '<html>
            <head>
                <meta http-equiv="Refresh" content="0;url=http://www.url.com/index.php?code=404" />
            </head><body></body>
          </html>';
?>

Upvotes: 0

shasi kanth
shasi kanth

Reputation: 7094

You can try the javascript way of redirecting the page:

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

Upvotes: 8

phpapprentice
phpapprentice

Reputation: 19

Just make sure before your php code you do not have a space because this does matter.

For example

<?php
some code this is the correct way
?>

--------------

<?php
some code this is the incorrect way the space above matters
?>

Upvotes: 0

Erfan Safarpoor
Erfan Safarpoor

Reputation: 5293

add this code to first of code:

<?php
ob_start();
?>

Upvotes: 14

Nerudo
Nerudo

Reputation: 942

Make sure before <?php of the php file you dont have a space. It might help if you changed the file encoding type to UTS_8 without BOM and backspace just before <?php and make sure you dont have session start() after anything in the file. It should be just after

Upvotes: 1

Karst Lok
Karst Lok

Reputation:

Looks like you're echo-ing text to the browser before sending the header('location'). You can not send content to your browser before executing a header(), as your echo will force a header to be sent. Comment these lines out and see if it works:

// echo $path_choice;
// echo $page_inc;

Now your header will be sent and you will be redirected.

Upvotes: 16

joschi
joschi

Reputation: 13101

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.

Upvotes: 2

Owen Allen
Owen Allen

Reputation: 11968

I believe you need to check your if-stack and make sure you are getting into each of your loops. Place an echo after each if statement to make sure the header() is even being evaluated, your loop is prolly being cut earlier than you expect.

My suspect has to do with the usage of null and $path_choice != null. Have you tried is_null() or isset() since they were designed specifically to check if a variable is null. Also, the !== operand may work in this case too, but i'm not sure offhand if PHP has that.

Upvotes: 1

Lekensteyn
Lekensteyn

Reputation: 66465

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

Upvotes: 2

Related Questions