PHP header location not working with clean urls - php

<?php
$result = mysqli_query($link, $query);
$url=$_SERVER['REQUEST_URI'];
if (!$result) {
$url.="/signup";
header("Location:".$url);
} else {
$url.="/home";
header("Location:".$url);
}
?>
Hello friends I am in a bit problem I am doing a project and using clean urls my htaccess is good everything is going good but after a signup form submission when I want to change header location
php gives an error can't change header location
kindly help

I would post a comment but I dont have enought rep.
Anyway, probably you are seeing error about headers already sent. It is common problem and it have nothing to do with clean urls. what you can do?
1. Make sure you are not outputting anything before header() call
Problem is based on fact how web server and HTTP works. When you are outputting server will send headers and content as soon as it is ready. And you may be outputting even whitespaces, so double check this first.
2. Turn on output buffering
If for any reason you need to output something before headers manipulation, you can turn on output buffering. This way server first "buffers" your data and then sents everything out. Hovewer you are paying in load time for this. Use only rarely, when it is really needed!
You can use a ob_start()for this

If that is your PHP script, I can see whitespaces before <?php tag.
<?php
^^^^--- WHITESPACES
These whitespaces will be written to output buffer even before PHP interpreter kicks in, and when you call header() it will give an error. This is because no output should be sent to client before calling header().

Related

HTML Login form with PHP Authentication [duplicate]

Does somebody know why my header() does not redirect?
The last part of my script is:
header("location: test.php");
die('died');
It writes:
died.
:(((
It should has to redirect before it dies, but it does not.
Do you have you any idea?
It's probably that you're calling header() after you are outputting some text/HTML to the browser, which is a no-no. Your header() call will be ignored if even so much as a single space of output has been sent before the call.
In other words, your header() code needs to be at the start of your script, before you display anything to the user. If that still isn't working, make sure you don't have any spaces or other whitespace by mistake outside of your php tags.
Maybe there is some invisible output before header, which is preventing setting header, and informative warnings are suppressed.
Additionally Location-Headers should contain an absolute URL:
// Show Errors
error_reporting(E_ALL);
ini_set('display_errors','On');
// Redirect
header('Location: http://example.com/path/test.php');
die('redirect');
You should use a fully-qualified address for your location header, and not output any content:
header('Location: http://example.com/test.php');
die();
Additionally make sure that no other content was sent before setting the header, otherwise it wont be sent as the headers will have already been sent to the browser.
location: should be Location:.
Moreover...
Scroll to the top of this page.
Click on your account name.
Click on a random question
Check mark a random answer
Come back to find more serieus answers
Good luck.
Just resolve it by removing ALL things before the php tag.
Remove the SPACE before the php tag
Select all before the tag and then push the erase button.
I was stuck on this error, too, and tried to show errors with error_reporting(E_ALL),
but this didn't worked for me. Info: I was using a hosting service on Strato.
The solution to all this was answered by Floem, thanks for this great setup.
If you ever don't get errors in case you put error_reporting(E_ALL) once in,
-> You must additionally add ini_set('display_errors', 'On'), to force PHP to show all your Errors. This solved my Error issue on
Then it's possible to read out, why PHP wouldn't make your redirect.
One tip on my side: I would recommend you to build a little redirect Class, as shown in this Video: Redirect Class for easy use, if you're going to use this redirect more than usual.
If the file containing the 'header()' instruction is required directly/indirectly in a HTML file, then the instruction {include 'someFile'} should be the first line of that HTML file. Place this instruction on the top of your HTML file, as shown in this image.
Add
ob_start();
before
header("location: test.php");

header within html document not working - new server

I moved my documents to a new host and the headers stopped working(refresh,redirect,etc).They used to work in my old server.I have googled and tried adding a ob_start before sending headers, that did not work.
Here is a part of the code...
if(isset($_GET['reflink']))
{
echo '<h3>Already Logged In<h3><p>Please logout before registering an account.Redirecting you back to where you came from...';
header('Refresh: 3; url="http://www.xacnr.com'.$_GET['reflink'].'"');
}
*It used to work before, it must be a problem with the server settings or something :|
A short answer is:
It is not possible to send http headers after the output of anything else.
So if you want to output headers, you must do this at the beginning of your output. To be even more precise: HTTP-Headers must be the first thing of your output, if you have to send them - before anything else.
Please read the documentation:
http://www.php.net/manual/en/function.header.php
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
Please be also aware of the fact that the Refresh-Header is AFAIK not part of the official HTTP-Standard. It's a jurassic artifact from Netscape which will be still accepted and interpreted by most browsers, but this may change even without special notice.
If you need such a refresh and if you want to stay on the safe side, you should consider using the Meta-Refresh within the HTML-Header.
Please read here:
http://en.wikipedia.org/wiki/Meta_refresh
BTW: It's also a bad idea to use unsanitized, unprocessed values from $_GET, $_POST etc. Your example should never be used in any public available environment.
You do an output (echo) before you send the header:
echo '<h3>Already Logged In<h3><p>Please logout before registering an account.Redirecting you back to where you came from...';
header('Refresh: 3; url="http://www.xacnr.com'.$_GET['reflink'].'"');
The simple but strict rule is:
No output before the header!
From the Docs:
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.
It is a very common error to read code with include, or require,
functions, or another file access function, and have spaces or empty
lines that are output before header() is called. The same problem
exists when using a single PHP/HTML file.

PHP header(); reliability

I need to redirect users if I don't want them to be able to access a certain page. How reliable is header('Location: ../acc/login.php'); for example? Can browsers ignore 302 errors, and is this the right way? Thanks in advance!
It depends a lot what you're trying to do. Technically spoken, header() is somewhat reliable. Only somewhat, because many PHP users have problems with it and to not get it to work.
PHP will prevent it from working if output has been already send to the browser. A drastic example:
<protected page content here>
<?php
header('Location: login-first.php');
exit();
?>
This would not work at all. You would eventually see even an error message with a warning.
Headers - by design - need to be send out before any other content (response body). They can not be send any longer if the response body has already started and PHP can't help you then in that case.
However, if you send headers before the response body, that function will work. Also the risk obviously to mess something up is not that drastic any longer, too:
<?php
header('Location: login-first.php');
exit();
?>
<protected page content here>
You can rely on header(), but make sure you called die(), exit() or return after that. Otherwise, script will continue its execution, which is potential security issue.
The browser can ignore header('Location: '); forwarding.
That is why you should always return after a call to a header() forward so the rest of your code does not execute should the browser not honor the forwarding.
It is the correct way to do things tho.
I would send the header command and then the exit command "exit()" (to stop running the php code on the server) before displaying the rest of the page. This way the user would never be sent the page content even if they ignored the 302 redirection.
And yes the user can ignore the 302 redirection:
http://www.webmasterworld.com/html/3604591.htm
header is 100% reliable.
However header('Location: ../acc/login.php') will be evaluated in the browser to a real location on your website, and ../acc/login.php wil not form a url that is valid!

Why does this header location redirect work after content has already been echoed?

<?
echo "lalala";
header("Location: http://www.google.com/");
If i put this in a plain php file and deliver over a standard apache2 server with mod-php (PHP Version 5.3.2-1ubuntu4.10) the redirect to google works.
<?
echo "lalala";
flush();
header("Location: http://www.google.com/");
this code does obviously not produce a working redirect.
My question is how the first code is beeing processed and why it works. Because I remember times when things like this were not possible. Is mod-php or apache intelligent enough to buffer the whole request and arrange headers before content?
And:
Can I rely on this if I make sure I don't flush the output manually? Because it would make my application much easier...
Output buffering is probably enabled by default. You should enable it manually if you want to rely on this functionality.
http://php.net/manual/en/function.ob-start.php
The header function ADDS an http common header to the HTTP response. So, the redirect is setted and the browser gets the 302 message before showing you the output.
flush orders php to send the http response already prepared at the point it is called. That's why the second code won't set the header (it must be setted before sending ANY output).
And, the PHP should not output a single thing until:
The script is processed (even if an error stops the parsing)
you set it to send the output somewhere in the script with flush()
Finally, check this on output control http://www.php.net/manual/en/intro.outcontrol.php

alternate php redirection method that works with output preceding it

so apparently if you do this:
<?php
echo 'something';
header("Location: http://something/");
?>
it will not work because there is an output preceding the header...
is there any other alternative php redirection method that works straight from php without installing anything and in which it will still work even if there's an output preceding it so that I don't have to worry about making sure that there is no output before, etc...
not, unless you do something in javascript or html tags in the page that you output itself
if preceding output is a problem
you can also use output buffering, see ob_start, ob_get
to get around that
There is no other way to do a php redirect, but you can fool it to still work even with code prior. You would buffer the content and only output it if there is no redirect or reaches the end of the script. Note: this may be resource heavy in some cases.
ob_start()
....CONTENT...
ob_end_flush();
There are no ways in PHP except using header()... before output is sent (headers be already sent)...
You can either use meta refresh in HTML that is set at zero seconds, or javascript.
But I wouldn't recommend javascript as some will have it disabled.
You could use a meta refresh tag.
You understand why this is impossible, right?
As soon as you echo "something" you have sent content to the client, and as part of that client headers were already sent. You can't retroactively modify headers you already sent, and you can't make two responses to one HTTP request.
ob_start() and ob_end_flush() will buffer the output instead of sending it to the client, which will allow you to get around this problem, BUT
a better solution would be to:
separate your logic code from your template so that you don't write anything to the screen until you already know you aren't going to redirect.

Categories