How to redirect in PHP - php

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.

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");

redirection in a required page in php

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);";

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!

PHP Redirect after posting to Database

I'm using this code ...
<?php
if (isset($_POST['submitButton'])) {
mysql_query("UPDATE notes SET Note=('$_POST[note]')
WHERE UserID='19'");
mysql_close($con);
header('Location: editrem3.htm'); //clears POST
}
?>
How do I redirect the page after the user clicks on submit and the data has been posted?
First, make sure that you are entering into the if() condition by echo ing something inside the if() block.
If it's working correct, then make sure that you've sent NOTHING before the header() is executed. Because when you use header() in a PHP file, there MUST NOT be any other output statements before that. So, check for any HTML code or echos before it.
Then if you are sure that header() is being executed, and still it's not redirecting, make sure that the target file exists.
Extra: ALWAYS add exit() immediately after header() redirects. Else the code will continue executing and can reveal your sensitive data.
Using the relative path to the file is probably causing issues. Try it using the full path (also probably outside the if{} would make more sense).
PHP might be able to parse the relative path, but the w3 spec explicitly states to use absolute URIs
edit: To elaborate, handing a relative path off to a browser for redirect is playing with fire. The browser might think it knows where it is in your site's hierarchy, but it might be actually in a different spot.
Also, if you're not getting a 404 page or something similar, then the relative path might not be your issue, but it might help you later on down the road
use meta redirects, something like
<meta http-equiv="refresh" content="0; url=http://example.com/">
but you could do something like
<meta http-equiv="refresh" content="0; url=index.php">
works fine with me

When do header(..) statements take effect in PHP?

after all stuff behind is run
immediately
Which is the case? Can anyone verify this?
Refer the following link.
http://php.net/manual/en/function.header.php
To make sure that a page is directly redirected, add a exit; after the header.
depends on the output_buffering setting
headers_sent() function can verify
If output buffering is disabled then it will be sent immediately. It must be sent before any other content (however you can perform processing logic before sending the header)
Depends on output buffering, but the PHP interpreter still only uses them after having interpreted all the code before, just like a normal function. Still, there must not be any HTML before a header(), or things will screw up.

Categories