dongshi1606 2015-06-04 15:06
浏览 54
已采纳

必须重新加载要解析的if语句的页面

First: Code!

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>

What this is supposed to do:

If the user is not logged in, show the loginform.html. If the login button got pressed, use the login.php to log the user in.

After someone has used the login-form to log in to the website, it displays a little welcome message and a new button for logout purposes.

What this does:

Displaying the login form works fine.

After someone logged in, it displays nothing. But after reloading the page, the button is there and works fine.

The $_SESSION['logged_in'] variable is set with the login, but why does the page need another reload to interpret this statement correctly?

I admit that the way I do it isn't necessarily 'best practice' and I am open for any advice. Still learning. :)

For testing: http://hsturnierv2.pixelpioniere.net/public_html/index.php login as "test" with pw "test"

  • 写回答

2条回答 默认 最新

  • dongmuzhan4705 2015-06-04 15:49
    关注

    The problem is most probably the fact that you have

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

    instead of:

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

    in login.php. It should have a capital L. It doesn't and you get no redirection, thus the blank page ...

    UPDATE

    So you redirect ok but your script dies for some reason. It is not a logical error rather a fatal script error that kills your script before it can output anything more ... Your code is:

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

    So i got it. When someone is actually logging in index.php requires a login.php which when done sends a relocation header and exits. But the output has already started! you have already half of the page out! So the header issues an error (which is not displayed probably due to php.ini settings) and then exits. Therefore you get half a page.

    You should change your index.php code to:

    <?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>
    

    This prevents output before header and also shows the error if you enter wrong credentials. Then you should also change your login.php file so that it doesn't redirect every time, rather only if the login was successful, like so:

    <?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;
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能