I have two scripts get.php and auth.php where i 've required auth.php in get.php, so here's the deal the redirection statement in auth.php i.e, header() is not working for some reason, any quick thoughts on this problem if this can be achieved or not??
As per my understanding, your problem is that:
header("location:[XYX.PHP]") is not working.
It generally does not work due to some output is print already on the page.
Please use
ob_start();
at very the beginning of the page.
This starts output buffering.
And the redirection works.
header("Location: $URL") may not work if you already sent headers by some echo, print_r or similar function.
Look at your web server error logs, does it mention something like "headers already sent" ? If yes that means you're probably outputting something before your header() function call
get.php
<?php include_once("auth.php"); ?>
auth.php
Redirect using the code below which is based on the example found at http://php.net/manual/en/function.header.php.
<?php header("Location: http://www.stackoverflow.com/"); ?>
If it still doesn't work, make sure (like it says on http://php.net/manual/en/function.header.php) that you are not outputting any html or even blank spaces or before calling the header() function. (Perhaps your get.php page is outputting empty space or HTML tags before including auth.php.)
you can use javascript function for this task
echo "window.open(url1, "name1", params);";
Related
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");
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
When i put my website online, all of the headers will not work. But when i built it, just on a local database etc. it worked "Works" fine.
I get this ERROR on the website currently:
Warning: Cannot modify header information - headers already sent by (output started at /customers/5/8/4/infuze.dk/httpd.www/index.php:22) in /customers/5/8/4/infuze.dk/httpd.www/includes/functions.php on line 16
I used my google foo a little and as far as i can understand is that i cant output any data before i use headers. But i dont thing that is the case here.
Here is the code i used in on the site.
if(empty($_GET['page'])){
header('location: index.php?page=homepage');
}
or at least one example
I usually get the error "Cannot modify header information - headers already sent by" when the page you're trying to redirect from contains information. Check to see if the script uses echo, include, require, etc., or contains HTML markup before the line at which you try to redirect.
<p>Hello!</p>
<?php
header('location: index.php?page=homepage');
?>
Will NOT work, because it sends the browser information before trying to redirect.
<?php
echo "Hi!";
header('location: index.php?page=homepage');
?>
Also won't work, because it also sends the browser information before trying to redirect.
<?php
header('location: index.php?page=homepage');
include('setup.txt');
?>
<h1>Amazing page!</h1>
WILL work, because it tries to send the browser information AFTER the redirect, but not before.
Headers won't redirect in the following conditions:
Any blank space is remaining in the file after ?> tag.
To avoid this, please avoid ending ?> tag (file end).
Please check whether, any echo or print statement is written accidentally.
Also, please check whether any HTML tag is remaining there.
If you follow, all this, your script will work.
Finally, in the starting of the file, put
ob_start()
This function will store all your page's output in a buffer and thus your redirection will work.
Try this;
<?php
ob_start();
// code
ob_end_flush();
?>
>> Call ob_start() at start of the script.
>> call ob_end_flush() at the end of script.
Thanks!
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.
I want to redirect from login page to my main page using php.
I use the following line of code: header('location:index.php');
inspite of redirection i received the error like:
Warning: Cannot modify header information -
headers already sent by (output started at C:\wamp\www\student\login.php:18)
in C:\wamp\www\student\login.php on line 19
This error occures if you print something before header() function.
For example:
<?php
echo 'test';
header('location:index.php');
exit;
?>
or even:
<html>
<head> .....
<?php
echo 'test';
header('location:index.php');
exit;
?>
You have to move this piece of PHP code before any operation that gives you an output.
You can also do the following trick but it is the second way you should try:
<?php
echo 'test';
ob_start();
flush();
header('location:index.php');
exit;
?>
you need to turn on the output buffer by inserting
ob_start();
at first line of php code
If you have already "echo'd" or "print'd" anything onto the page, either inside your script or outside of any set of PHP tags, then you cannot send any headers anymore. This is what your error message is stating.
Also, you should (try) to use full paths in location tags, it's better for SEO to use full URLs for every link on your website, let alone the redirects.
make sure that the header function is called before any response is outputted, e.g. header() function must be called before any echo functions or print_r, try removing the spaces before the <?php opening tag.
Its very difficult to decide what constitutes output to a page. I tried to eliminate my problem by removing all "echo's", "prints" etc but couldnt make the redirection work. I think there was a problem with a returned sql query. Adding the buffer and flushing it cured the problem.
You need to find out whether the header was sent already sent by checking line by line with header_sent() it will return true or false. If it's already sent you can't use header(). Try meta http-equiv="refresh" content="0;URL='your url'" /.Don't forget to add open and closing tags.<>
I wrote
<?
header("Location:http://example.com");
?>
but Redirect is not occured.
How to redirect?
But I do not have authority to edit php.ini
So safe_mode is on in php.ini
Try:
header("Location: http://example.com");
HTTP headers need to exactly follow the spec. More directly here (Location header):
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
One possible issue is that there was something got "printed out" before you issue the above code. So check your code so that there is nothing got "echoed" before reached this line.
Two things:
You have to make sure you haven't sent any other HTML before sending your header.
You should also exit or die() after your header() call.
See this post for more detailed information.
You can also use JavaScript to do the redirect but I suspect PHP is probably a better idea in your situation.
Make sure you alway add die() after the header() call. This is extremely important if anything is output below the header() that the user is not supposed to see.
Make sure you have nothing prior to the opening "
If that still doesn't work, are you getting any sort of error message?
Alternatively, use:
<meta http-equiv="refresh" content="0;url=http://foo.com">
somewhere in your <head> section.
Source.