无法在网站上打开网页PHP

时间:2016-02-17 15:37:02

标签: php

我的网站上有三个页面索引(登录页面)主页(导航)项目创建和管理(信息)现在登录后没有任何问题但是当我尝试从家里到项目创建和管理它接缝像我'我立即被重定向回主页。它通过url条目或主页导航执行相同的操作。这是我的代码:

索引

<!DOCTYPE html>
<?php
        session_start();        
        $username = "admin";
        $password = "collins1";

        if (isset($_GET['logout'])){
        session_destroy();  
        }

    if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
        header("Location: home.php");
    }

    if (isset($_POST['username']) && isset($_POST['password'])){
        if ($_POST['username'] == $username && $_POST['password'] == $password)
        {   
            $_SESSION['loggedin'] = true;
            header("Location: home.php");
        }

        else 
        {
            echo '<font color="#FF0000"><p align="center">Username or Password incorrect please try again</p></font>';
        }
    }



?>

<html>
    <head>

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Login</title>

        <link href="../CSS/boilerplate.css" rel="stylesheet" type="text/css">
        <link href="../CSS/master.css" rel="stylesheet" type="text/css">

        <script src="../JAVASCRIPT/respond.min.js"></script>

</head>
<body link="black">
    <div class="gridContainer clearfix">

        <div id="borderDiv">

            <div id="headerDiv">
                <p>Welcome</p>
            </div>

            <div id="subHeaderDiv">
                <p>Please login to continue to the Project Creation and Management System</p>
            </div>

            <form method="post" action="index.php">
                <div id="userNameLoginDiv">
                    <p>Username:</p>
                    <input type="text" name="username" size="12">
                </div>  

                <div id="userPasswordLoginDiv">
                    <p>Password:</p>
                    <input type="password" name="password" size="12">
                </div>

                <div id="loginBtnDiv">
                    <input id="button" type="submit" value="Login">
                </div>
            </form>

        </div>

    </div>
</body>
</html>

<!DOCTYPE html>
<?php
    session_start();

    if (isset($_GET['logout'])){
    session_destroy();  
    }

    if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
        header("Location: index.php");
    }



?>


<html>
    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <title>Home</title>
        <link href="../CSS/boilerplate.css" rel="stylesheet" type="text/css">
        <link href="../CSS/master.css" rel="stylesheet" type="text/css">

        <script src="../JAVASCRIPT/respond.min.js"></script>

    </head>

    <body link="black">

    <div class="gridContainer clearfix">

      <div id="headerDiv">
        <p>Home</p>
        </div>

        <a href="index.php?logout"><font color="#000000">Logout</font></a>

        <div id="homeBtn1"> <a href="http://collins.sulmaxmarketing.com/Project_Creation_and_Management"><img src="../button.png" alt="Project Creation and Management"></a>
            <div id="homeBtnText1">
              <a href="http://collins.sulmaxmarketing.com/Project_Creation_and_Management"><font color="#000000" ><p>Project Creation and Management<p></font></a>
            </div>
      </div>


    </div>
    </body>
</html>

项目创建和管理

<!DOCTYPE html>
<?php
    session_start();

    if (isset($_GET['logout'])){
    session_destroy();  
    }

    if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
        header("Location: index.php");
    }



?>


<html>
    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <title>Home</title>
        <link href="../CSS/boilerplate.css" rel="stylesheet" type="text/css">
        <link href="../CSS/master.css" rel="stylesheet" type="text/css">

        <script src="../JAVASCRIPT/respond.min.js"></script>

    </head>

    <body link="black">

    <div class="gridContainer clearfix">

      <div id="headerDiv">
        <p>Project Creation & Management</p>
        </div>
    <a href="index.php?logout"><font color="#000000">Logout</font></a>


    </div>
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

你应该编辑下面的代码

<?php
    session_start();        
    $username = "admin";
    $password = "collins1";

    if (isset($_GET['logout'])){
    session_destroy();  
    }

if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
    header("Location: home.php");
}

if (isset($_POST['username']) && isset($_POST['password'])){
    if ($_POST['username'] == $username && $_POST['password'] == $password)
    {   
        $_SESSION['loggedin'] = true;
        header("Location: home.php");
    }

    else 
    {
        echo '<font color="#FF0000"><p align="center">Username or Password incorrect please try again</p></font>';
    }
}

&GT?; ...

因为session_start()必须高于每个输出字符串。您打印了<!DOCTYPE html>,使会话无法启动

答案 1 :(得分:0)

session_start();必须高于所有页面中的所有其他内容。否则,无法创建和保存session variables。因此,您的if页面中的第二个Project Creation and Management将被调用。那就是问题!