PHP Header Location-Redirect Doesn't Work - Why

PHP header location-redirect doesn't work - why?

I reminded myself that I had xDebug installed on the actual test environment and after googling it, I found this site: http://bugs.xdebug.org/view.php?id=532

So I'll downloaded the last version of xDebug and changed the php.ini accordingly for the new file and everything works out like a charm. Headers are being sent - the redirecetion is done and errors are displayed.

Thanks everybody for your help!

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.

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.

header ('Location: ') in PHP not redirecting

I just finally solved it guys!
What I did was ditch mysqli and went using PDO.
Here's my new code:

<?php
session_start();
$h = "localhost";
$u = "root";
$p = "";
$db = "user";
$msg = "";

try
{
$con = new PDO("mysql:host=$h; dbname=$db", $u, $p);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

if(isset($_POST["sub"]))
{
if(empty($_POST["usern"]) || empty($_POST["passw"]))
{
$msg = "<label>Please enter username/password!</label>";
}
else
{
$sqlquery = "SELECT * FROM myuser WHERE username = :username AND password = :password";
$stat = $con->prepare($sqlquery);
$stat->execute(
array(
':username' => $_POST["usern"],
':password' => $_POST["passw"]
)
);
$countrow = $stat->rowCount();
if($countrow > 0)
{
$_SESSION["username"] = $_POST["usern"];
header("Location: homepage.php");
}
else
{
$msg = "<label>Invalid username/password</label>";
}
}
}
}

catch(PDOException $er)
{
$msg = $er->getMessage();
}
?>

PHP Redirect to different url using header( Location: ) does not work

Headers must be set before any data is transmitted, so you can't just stick them in the middle of a file. Quoting the the manual:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

So at the very least you'll need to rewrite your file to:

<?php

header("Window-target: _parent");
header("Location: https://www.w3schools.com/");

?>
<!doctype html>
...

Also, never sleep() in an http(s) response: that response should finish as fast as it can, no matter what content it needs to generate. Sleep has no place in (really any) PHP code.

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 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:



Related Topics



Leave a reply



Submit