必须重新加载页面才能解析 if 语句

Have to reload page for if statement to be parsed

第一:代码!

loginform.html

<form action="" method="post" id="loginform">

                <h3>Login</h3>
                <input type="text" name="username" placeholder="Username">
                <br>
                <br>
                <input type="password" name="password" placeholder="Password">
                <br>
                <input type="submit" name="logsubmit" value="Login" class="registerbutton">
            </form>

login.php

<?php 
require_once("../resources/config.php");
require_once("../resources/library/dbconnect.php");

function checkUser($con) {

   if (isset($_POST['username']) && isset($_POST['password'])){
            $username = $_POST['username'];
            $pw       = md5($_POST['password']);

$sql="SELECT * FROM `users` WHERE username='$username' and pw='$pw'";

       $result = mysqli_query($con, $sql);
       $row    = mysqli_fetch_assoc($result);

    if ($result==true && $username == $row["username"] && $pw==$row["pw"]) {


            $_SESSION["logged_in"] = 1;
            $_SESSION["admin"] = $row["admin"];
            $_SESSION["username"] = $row["username"];
        }

        else {

        $msg = "Das war nichts! Passwort oder Username falsch? <br>".mysqli_error($con);
        unset($_SESSION["logged_in"]);

        }
    }
}
checkUser($connection);
header('location: ../public_html/index.php');
exit;
?>

index.php

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Barstone</title>
    <link rel="stylesheet" href="css/default.php" type="text/css">
    <link href='http://fonts.googleapis.com/css?family=Bitter:700&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
</head>
<body>
    <?php require_once("../resources/config.php");
          require_once("../resources/library/header_nav.php");?>
    <div class="pagewrapper">

        <div class="register-container">

            <?php if(!isset($_SESSION['logged_in']) || !isset($_POST['regsubmit']))           
            {require_once("../resources/library/registerform.php");}

             if (isset($_POST['regsubmit']) && $_SESSION['register_check']==true) {printf("Success! Welcome %s!",$_POST['username']);}

            ?>

        </div>


        <div class="login-container">
            <?php if (isset($_SESSION['logged_in'])) { printf ("<form action='../resources/library/logout.php' method='post' id='loginform'><h3> Hello %s!                           </h3> Nice to see you!<input type='submit' class='logoutbutton' value='logout'>                                         </form>",$_SESSION["username"]);
       }?>
             <?php if (isset($_POST["logsubmit"])) {require_once("../resources/library/login.php"); 
                        }
else { if (!isset($_SESSION['username'])) {require_once("../resources/library/loginform.html");}}
                ?>

        </div>
        <div class="content">
        <?php require_once("../resources/library/articles.php");?>
        </div>
    </div>
</body>

</html>

relevant index.php part

    {printf ("<form action='../resources/library/logout.php' method='post' id='loginform'>
   <h3> Hello %s!</h3> Nice to see you!
   <input type='submit' class='logoutbutton' value='logout'>
   </form>",$_SESSION["username"]);}
    ?>

   <?php if (isset($_POST["logsubmit"])) {require_once("../resources/library/login.php");}
        else {if (!isset($_SESSION['username'])) 
               {require_once("../resources/library/loginform.html");}
                 }?>
 </div>

这应该做什么:

如果用户没有登录,显示loginform.html。 如果按下登录按钮,请使用 login.php 登录用户。

有人使用登录表单登录网站后,它会显示一条欢迎消息和一个用于注销的新按钮。

这是做什么的:

显示登录表单工作正常。

有人登录后,什么都不显示。但是重新加载页面后,按钮在那里并且工作正常。

$_SESSION['logged_in'] 变量是用登录名设置的,但为什么页面需要重新加载才能正确解释此语句?

我承认我这样做的方式不一定 'best practice' 我愿意接受任何建议。还在学习。 :)

用于测试:http://hsturnierv2.pixelpioniere.net/public_html/index.php 使用密码 "test"

以 "test" 身份登录

嗯,你基本上在做什么:

<?php if (isset($_SESSION['logged_in'])) 
    // output something
?> 
<?php 
if (isset($_POST["logsubmit"])) { 
     // process login with login.php
} else {
     if (!isset($_SESSION['username'])) {
        //display login form   
     }
?>

好的,这里发生了什么......

  1. 用户第一次访问网站。 (未登录且未提交登录表单)

    $_SESSION['logged_in'] 未设置并且 // 输出未达到的内容

    $_POST['logsubmit'] 未设置并且 $_SESSION ['username'] 未设置 所以显示登录表单

  2. 用户提交登录表单 $_SESSION['logged_in'] 未设置并且 // 输出未达到的内容

    $_POST['logsubmit'] 已设置并调用 login.php 如果 login.php 中没有输出,页面将保持空白。 我假设您在 login.php

  3. 中设置了会话变量
  4. 用户登录后重新加载页面 $_SESSION['logged_in'] 现在设置 -> 你到达 // output something

我个人会在单独的文件中处理登录:

<?php 
if(!isset($_SESSION['logged_in'])) {
    // redirect to login.php
}
?>
// here comes the content when user is logged in

在login.php中:

<?php 
    if(isset($_POST['logsubmit']) {
         // handle login and redirect to page that needs login if successfull 
    }

    if($loginError) { // print login errors }

    // print login form 
?>

编辑:不要忘记 session_start()(在包含的文件中 session_start() 不是必需的)

问题很可能是您

header('location: ../public_html/index.php');

而不是:

header('Location: ../public_html/index.php');

在 login.php 中。它应该有一个大写的 L。它没有并且你没有重定向,因此空白页 ...

更新

所以你重定向正常,但你的脚本由于某种原因而死了。这不是一个逻辑错误,而是一个致命的脚本错误,它会在你的脚本输出更多内容之前杀死你的脚本...... 您的代码是:

<?php if (isset($_SESSION['logged_in'])) 
{ 
    printf ("<form action='../resources/library/logout.php' method='post' id='loginform'>
    <h3> Hello %s!</h3> Nice to see you!
    <input type='submit' class='logoutbutton' value='logout'>                                          
    </form>",$_SESSION["username"]);
   }
?>
<?php if (isset($_POST["logsubmit"])) 
{
    require_once("../resources/library/login.php"); 
}else 
{ 
    if (!isset($_SESSION['username'])) {require_once("../resources/library/loginform.html");
    }
}
?>

所以我明白了。当有人实际登录时 index.php 需要 login.php 完成后发送重定位 header 并退出。但是输出已经开始了!你已经有一半的页面出来了! 因此 header 发出错误(可能由于 php.ini 设置而未显示)然后退出。因此你得到半页。

您应该将 index.php 代码更改为:

<?php
session_start();
if (isset($_POST["logsubmit"])) 
{
    require_once("../resources/library/login.php"); 
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Barstone</title>
    <link rel="stylesheet" href="css/default.php" type="text/css">
    <link href='http://fonts.googleapis.com/css?family=Bitter:700&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
</head>
<body>
    <?php require_once("../resources/config.php");
          require_once("../resources/library/header_nav.php");?>
    <div class="pagewrapper">

        <div class="register-container">

            <?php if(!isset($_SESSION['logged_in']) || !isset($_POST['regsubmit']))           
            {
                 if (isset($msg)) echo $msg .'<br>';
                 require_once("../resources/library/registerform.php");}

             if (isset($_POST['regsubmit']) && $_SESSION['register_check']==true) {printf("Success! Welcome %s!",$_POST['username']);}

            ?>

        </div>


        <div class="login-container">
            <?php if (isset($_SESSION['logged_in'])) { printf ("<form action='../resources/library/logout.php' method='post' id='loginform'><h3> Hello %s!                           </h3> Nice to see you!<input type='submit' class='logoutbutton' value='logout'>                                         </form>",$_SESSION["username"]);
       }else { if (!isset($_SESSION['username'])) {require_once("../resources/library/loginform.html");}}
                ?>

        </div>
        <div class="content">
        <?php require_once("../resources/library/articles.php");?>
        </div>
    </div>
</body>

</html>

这会阻止 header 之前的输出,并且如果您输入错误的凭据也会显示错误。然后你还应该更改你的 login.php 文件,这样它就不会每次都重定向,而只是在登录成功时才重定向,如下所示:

<?php 
require_once("../resources/config.php");
require_once("../resources/library/dbconnect.php");

function checkUser($con) {

   if (isset($_POST['username']) && isset($_POST['password'])){
            $username = $_POST['username'];
            $pw       = md5($_POST['password']);

$sql="SELECT * FROM `users` WHERE username='$username' and pw='$pw'";

       $result = mysqli_query($con, $sql);
       $row    = mysqli_fetch_assoc($result);

    if ($result==true && $username == $row["username"] && $pw==$row["pw"]) {


            $_SESSION["logged_in"] = 1;
            $_SESSION["admin"] = $row["admin"];
            $_SESSION["username"] = $row["username"];
            $msg='OK';
        }

        else {

        $msg = "Das war nichts! Passwort oder Username falsch? <br>".mysqli_error($con);
        unset($_SESSION["logged_in"]);

        }
    }
    return $msg
}
$result = checkUser($connection);
if ($msg=='OK')
{
    header('location: ../public_html/index.php');
    exit;
}
?>