PHP Header redirect not working
Look carefully at your includes - perhaps you have a blank line after a closing ?> ?
This will cause some literal whitespace to be sent as output, preventing you from making subsequent header calls.
Note that it is legal to leave the close ?> off the include file, which is a useful idiom for avoiding this problem.
(EDIT: looking at your header, you need to avoid doing any HTML output if you want to output headers, or use output buffering to capture it).
Finally, as the PHP manual page for header points out, you should really use full URLs to redirect:
Note: HTTP/1.1 requires an absolute
URI as argument to Location:
including the scheme, hostname and
absolute path, but some clients accept
relative URIs. You can usually use
$_SERVER['HTTP_HOST'],
$_SERVER['PHP_SELF'] and dirname() to
make an absolute URI from a relative
one yourself:
header location not working in my php code
That is because you have an output:
?>
<?php
results in blank line output.
header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP
Combine all your PHP codes and make sure you don't have any spaces at the beginning of the file.
also after header('location: index.php');
add exit();
if you have any other scripts bellow.
Also move your redirect header after the last if
.
If there is content, then you can also redirect by injecting javascript:
<?php
echo "<script>window.location.href='target.php';</script>";
exit;
?>
Php header location redirect not working
Pekka answered my question in the comments. He didn't post an answer, so I am now. Use the exit()
method after the header redirect. For some reason the rest of the code of the page continues to execute after the header() method redirect. When the rest of the code executes, the echo statement is outputted to the page. And you can't redirect using the header function after you output to the page. To avoid rest of the code from executing, use exit()
. Thanks Pekka.
UPDATE: When using the web browser Internet Explorer, I have noticed that $_POST['cancel'] is not reliable. I am not exactly sure why this is, but I suspect IE posts additional variables on a form submit, specifically the variable 'cancel' is posted. I solved this by using a variable name other than 'cancel'. The combination of using exit() and a unique variable name is working for me.
The PHP header() function is not redirecting
If you're using header("Location: ");
after you've output content make sure you've put ob_start();
earlier in the script. ob_start();
buffers your script, so nothing is output until you either call ob_end();
or the end of the file is reached. Calling header("Location: ");
after content has already been output to the browser can cause problems.
PHP header redirect not working
Try:
error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);
flush();
header("Location: http://www.website.com/");
die('should have redirected by now');
See what you get. You shouldn't use ^ (xor) in your error_reporting() call because you're unintentionally asking for all errors EXCEPT notices and warnings.. which is what a 'headers already sent' error is.
Edit:
Also try putting flush() right above your header() call.
PHP Header location redirect not working. How to rectify
It's due to the spaces at the beginning of the file.
Make sure there is nothing out of <?php and ?>
before the redirect is done as the headers should be sent before any content of the page has been sent. If you enable the php warnings there should be a warning about this. Also make sure there is nothing printed in the included connection.php
file before the redirect is done.
In your case some spaces have been sent so that the header redirection will not be done.
It could be still done if the web server or the script had an output buffer but since you say it's not working it doesn't seem to be the case.
PHP Header Redirect Does not Work Within The Ajax Request
You can use JavaScript to redirect in the ajax success callback function:
success: function(data) {
window.location.href = 'http://localhost/profil.php';
}
It can't be done with a PHP header redirect, because that will only redirect the ajax call, not the original browser window.
PHP header redirect not working with Variable
Use " " quotes when you are using variable inside string.So replace header('Location: $url') ;
with header("Location: $url") ;
PHP header redirect not working on form submit
header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
And in your case, you are using echo before header()
So you can use a redirect method(that i use in all my projects, no problems ever)
<?php
if ($connect->query($sql) === TRUE) {
echo "<script> parent.self.location = \"hub.php\";</script>";
echo "Success";
} else {
echo "Error: " . $sql . "<br>" . $connect->error;
}
?>
Related Topics
Access Controller Method from Another Controller in Laravel 5
How to Locate the PHP.Ini File (Xampp)
Mongodb Duplicate Documents Even After Adding Unique Key
In Where Shall I Use Isset() and !Empty()
PHP: Sort and Count Instances of Words in a Given String
PHP Array Merge Two Arrays on Same Key
PHP: Writing a Simple Removeemoji Function
How Exactly Is a PHP Script Executed
How to Find the Dates Between Two Specified Date
How to Import a .SQL File in MySQL Database Using PHP
When to Use Strtr VS Str_Replace
How to Check If a File Is Mp3 or Image File
Why Is Pdo Better For Escaping MySQL Queries/Querystrings Than MySQL_Real_Escape_String
Converting a Simplexml Object to an Array
PHP, MySQL Error: Column Count Doesn't Match Value Count At Row 1