PHP Redirect after posting to Database - php

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

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

PHP redirect correct usage on execution of code

We are looking to redirect users to different pages after code has been executed, eg. form validation, session timeouts etc..
Placing the following in the code:
header("Location: http://localhost/example/"); exit();
Appears to do nothing more than exit() the code being executed at the correct point. It does not redirect the page to that URL.
How do we physically redirect the page to another URL.
You likely have whitespace in the output before the Header() function is called. The header cannot be called after ANY output is sent back to the browser.
If you can view the output of that file, you might see that there is some whitespace created by an include or something else in your script prior to the Header() being called.
If you cannot avoid output prior to that call, you might have to resort to a javascript to do the redirection. A JavaScript Window.Location should do the trick.
How about using the HTML Redirect after executing the necessary code?
echo '<meta http-equiv="Refresh" content="0;url=http://localhost/example/" />'

php returning innerHTML

Im trying to update the contents of an element after running some php code. I realize the php is executed first, but I thought by loading the page I could then find the element? However console says cannot find element of null so I guess the page isn't loading before the innerHTML code is running.
Anyone any ideas?
else if(strlen($_POST['username']) < 6){
header("Location: http://webpage/register.html");
echo "document.getElementById('elemID').innerHTML = usename too short";
}
header() instructs your clients to go to the new location, hence outputting anything after that would make no effect to your client as the content of register.html is already handled differently by your server.
If you can change register.html to use php instead, you could pass
header("Location: http://webpage/register.php?msg=username%20too%20short");
Then in your register.php
if(!empty($_GET['msg'])) echo $_GET['msg'];
First, you shouldn't really have any logic after your header Location redirect. It is good practice to put "exit" or "die" after a redirect like that as you can't guarantee that the browser will ever see the next line before redirecting. In fact, you can pretty well guarantee that it will more often not see that code.
If you're going to redirect, put your error as an argument to your redirect URL and have logic there that shows the error like this:
header("Location: http://webpage/register.php?error=username%20too%20short");
Then in you register.php (I renamed it from .html so you can read the error argument) you can reference your error like:
$error = $_GET['error'];
if (!empty($error)) {
//write your error out in some markup or javascript...
}

How can I send a user to another url without a header redirect?

I'm learning PHP and been looking for this for a while. What I want to achieve is something like so:
if (true/false) {
go to this url;
}
Every time I search terms like php redirects or php links etc., 99% of the time I get something "headers". I read that header redirects can achieve this but no code can go before it, that it must be first on the page else it wont work.
If that's so, then how can I achieve this?
i read that header redirects can achieve this but no code can go before it. that it must be first on the page else it wont work.
That's wrong. There must be no output before this. Thus you have to ensure, that you don't echo, print, ?>something<?php (or whatever) anything before.
if (true) {
header('Location: ' . $url, false, 302);
exit; // Ensures, that there is no code _after_ the redirect executed
}
You can ready everything about it in the official manual. Especially:
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.
echo '<script type="text/javascript"> document.location = "http://yoururl.com";</script>'
and this will be executed when this part of script is executed.
You can use this if you need some output before the redirect:
header("refresh: $time_in_seconds; url=$your_url);
You still must call this before output is actually sent however. Send the header, then send your output - the page will "redirect" in the time specified.
Disclaimer: I must admit, I'm not sure what the implications of this are and can't find docs on it, so I can't necessarily recommend it - but I can validate that it works.

How to redirect in 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.

Categories