Reputation:
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");
}
Upvotes: 1
Views: 3176
Reputation: 44
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
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation:
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()
Upvotes: 0
Reputation: 4466
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.
Upvotes: 1
Reputation: 491
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.
Upvotes: 2
Reputation: 605
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...
Upvotes: 1
Reputation: 32740
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
Upvotes: 2