开发者

PHP - Dealing with POST and header include

开发者 https://www.devze.com 2023-04-06 11:24 出处:网络
Say I have a form where the user can sign in, and when the user fills out the form it posts back to the same page, checks for errors with the username and password, and logs the suer in if all is swel

Say I have a form where the user can sign in, and when the user fills out the form it posts back to the same page, checks for errors with the username and password, and logs the suer in if all is swell. At the moment I'm doing this by loading the form under this condition:

if($_SERVER['REQUEST_METHOD'] != 'POST') {

and if this isn't true then check for errors and log in etc.

The problem is that this all happens in signin.php. At the top of signin.php I am including header.php and connect.php, which do exactly what their names suggest. When I post back to the page the changes开发者_StackOverflow in sign in status are not shown in the header file (there is a userbar in this file, similar in nature to the one on this site). The sign in works, but I have to navigate to a different page for the header to show 'signed in' instead of 'sign in/register' or whatever.

Have I gone about this in the wrong way, or is there just something I need to add to make my header update? Cheers.

P.S: I am pretty new to php so something not too complicated would be preferable please.


You probably need to redirect (using header()) after the POST code happens:

if($_SERVER['REQUEST_METHOD'] != 'POST') {
    ..
}
else {
    ..
    header('Location: ' . basename($_SERVER['REQUEST_URI']));
    exit;
}


there are 2 problems with your setup violating very basic yet strict rules of the web-development

  1. There ought to be redirect after any POST request. Can be done as shown in the other answer.
  2. There must be no HTML shown before all logic is done. So, your header.php should be called AFTER form checking, not before it.
0

精彩评论

暂无评论...
验证码 换一张
取 消