Php redirect does not change url

I know this is a very common question, but I could not find the answer

I'v post.php, which send the mail form to the .php controller. When it goes correctly, in the controller it does:

header("Location: ./post?ok=1"); 

the message is actually inserted, and it goes through the line above, but the URL in the browser seems to never change, and there was no ok parameter at the end,

I could use a session variable to store such a success / failure parameter, but this method should work

post.php:

 <form action="controller" method="post"> <input name="test" value='test' type="text" /> <input type="submit" value="post" /> </form> 

controller.php:

 <?php header("Location: ./post?ok=1"); ?> 

Change 1:

 header("Refresh:1;url=http://localhost/test/post?ok=1"); //doesn't work better 

Edit 2:

in post.php top I put

 debug("post l ".count($_POST)); debug("get l ".count($_GET)); 

they display 0 before serving, and 0 after also

edit3: it works

blocked by:

 if ($success){ header("Location: ./post?ok=1"); } header("Location: ./post"); 

which should be:

 if ($success){ header("Location: ./post?ok=1"); } else { header("Location: ./post"); } 

thanks all

+6
source share
5 answers

First of all, you should not use $_SERVER['HTTP_REFERER'] , this is not safe. It is also possible that the client will not send this header anyway. Right now I don’t know how, but I’m sure that it can be used for a high-performance game on your site for something evil.

Better you should know where the form data came from and redirect the user there.

By the way, your redirect may not work as expected, because the URL already contains a question mark. Therefore, you need to add additional parameters using the & sign.

For redirection, I would suggest the HTTP status of the 302 Found response. But you must be sure that they do not send anything else before. There should be no HTML output or empty lines until the next header line:

 header("Location: http://".$_SERVER['HTTP_HOST']."/your/source.form", true, 302); 
+3
source

Try using the HTTP status code 303 See Oter:

 header("Location: your/location", true, 303); 
+3
source

There may be problems on the server side (for example, your code issues something before setting the headers). To debug this, you need to set display_errors = On in php.ini or temporarily enable it in your code using ini_set('display_errors', 1) .

In most cases, the error "Cannot modify header information - headers already sent." . Make sure there are no print , echo tags, or any space or extra line after the PHP close tag ( ?> )

+1
source

This may be a second server redirect problem.

 header('Location: post.php?ok=1', true, 303); exit; 

Try adding the .php extension to the url to make sure the post is post? ok = 1 is not redirected to post.php by the server (e.g. htaccess). In an additional redirect, you may lose the get parameters. Then remember to add the output after the header is redirected.

+1
source

Actually ... it looks like you are not following the call to header () with "exit". Examples:

 header( 'location: ' . $location ); exit; header( 'location: ' . $location, true, 301 ); exit; 

When you β€œexit” after a call, the URL changes in the address bar of the browser because the code cannot continue to execute. Otherwise, the code continues to run, causing the URL to remain unchanged.

0
source

Source: https://habr.com/ru/post/917411/


All Articles