1
php header("location: test.php")

not working. But java script redirect working .

I tried with header() doesn't redirects?

header("location: test.php");

but i tried with java script its working

window.location ="http://www.test.com/test.php"

what is the problem. please give some solution

before calling header there no echo ( output commands )

there is no error / warning messages

My exact code :

if($contlogin >0 && $LoginID!="")
{      
    $_SESSION['LoginID'] = $LoginID;
    if($_SESSION['currentUrl']) {
        header("location: http://".$_SESSION['currentUrl']);
    }
    else {
        if($LoginID==1) {
            header("location:admin/index.php");
        }
        else {
            header("location:dashboard.php");
        }
    }
}
else {   
    header("location:index.php?err=1");
}
2
  • 2
    Can you paste all of your PHP code? Commented Jan 22, 2013 at 5:04
  • Open up the site in Chrome and press F12 and look at your headers for the page - did it even include the Location tag? Commented Jan 22, 2013 at 5:07

8 Answers 8

3

Place a exit() after header("location: test.php");

Sign up to request clarification or add additional context in comments.

Comments

2

Check if there is space before your php tag <?php

Check your error_reporting is on or off, if it is off make it on

2 Comments

there no space after or before <?php ?>
check in all the php pages included in your page, before the header statement.
2

It sounds like you probably have an error in your PHP, likely a syntax error. From the command line you can run PHP's lint checker to look for errors:

php -l scriptname.php

Alternatively, enabling error_reporting either in php.ini or at runtime in your script might help you figure out what's going on.

If you haven't checked it yet, your webserver's error log may also provide a clue as to what's happening.

Comments

1

Sorry I cant comment until I get my rep up so I gotta post this But your php opening tag might have just a space before it, and that causes the header not to work... so <?php might have a space or something before it...

Comments

1

Shouldn't the l in location: should be L .

header("Location:test.php");

Also use exit() after header() to avoid any problems and stop execution of script as suggested by another poster.

2 Comments

both i tried header("Location:test.php"); and header("location:test.php");
Could you also check if any of your if-else case is even triggered by commenting header() and echoing for all the three cases
0

some reasons for not working header(); 1. if there is space before the php tag it wont work 2. echo statement before header() if u found this Add exit() or die() after the header()

Comments

0

Your PHP code must be sending some information to the page before header kicks in. In my case I had some JavaScript code in the body section. Which causes some data (although not immediately visible) causing the location not to work.

Please check your logs for some entry such as:

[Mon Apr 13 13:53:29 2015] [error] [client 192.168.109.109] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/xxx.php:208) in /var/www/html/xxx.php on line 565, referer: https://www.acme.com/xxx.php

Comments

0

You need to add the ob_start(); function in the top of the php page, then redirect function will work fine.

link : http://php.net/manual/en/function.ob-start.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.