我的课程有什么问题?

时间:2014-11-11 08:47:34

标签: php session

当我尝试转到主页时,页面变为空白,这是因为会话标记,因为当我删除它们时页面会显示。

主页:

<?php

session_start();

include 'includes/db_connect.php';

if(!isset($_SESSION['LoggedIn']) && !isset($_SESSION['Username']))
{
 header('Location:home.php');
?>
<!DOCTYPE html>
<html>
<head>
    <title>Home</title>
    <link rel="stylesheet" href="styles/home.css" type="text/css" >
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
    <script type="text/javascript" src="js/nav.js" ></script>
    <script type="text/javascript">
    <!--
        function toggle_visibility() {
           var e = document.getElementById("nav");
           if(e.style.display == 'table')
              e.style.display = 'none';
           else
              e.style.display = 'table';
        }
    //-->
    </script>
</head>

<body>
    <div class="Menu" >
        <div class="middle" >
            <div class="profilepic" >
                <a href="profile.php" >
                    <img src="" ></img>
                </a>
            </div>
            <div class="search" >
            <form method="POST" action="search.php" >
                <input type="search" id="search-input" class="search-input" placeholder="Please enter a search term!" minlength="1" >
            </form>
            </div>
            <p><?php echo '$_SESSION["Username"]'; ?></p>
        </div>
        <div class="navigation">
            <div class="openMenu" id="openMenu" ><button onclick="toggle_visibility();">Menu</button></div>
            <ul id="nav" >
                <li><a href="profile.php" ><i id="navicon" class="fa fa-user fa-2x" ></i></a></li>
                <li><a href="" ><i id="navicon" class="fa fa-envelope-o fa-2x" ></i></a></li>
                <li><a href="" ><i id="navicon" class="fa fa-bars fa-2x" ></i></a></li>
                <li><a href="" ><i id="navicon" class="fa fa-group fa-2x" ></i></a></li>
                <li><a href="" ><i id="navicon" class="fa fa-rss fa-2x" ></i></a></li>
                <li><a href="" ><i id="navicon" class="fa fa-clock-o fa-2x" ></i></a></li>
                <li><a href="" ><i id="navicon" class="fa fa-edit fa-2x" ></i></a></li>
                <li><a href="" ><i id="navicon" class="fa fa-gear fa-spin fa-2x" ></i></a></li>
            </ul>
        </div>
    </div>

    <div class="status" >
    <form action="" method="GET" >
        <div class="upload" >
            <input type="file" name="videofilename" accept="video/*" class="upload" />
        </div>
        <div class="upload">
            <input type="file" name="audiofilename" accept="audio/*" class="upload" />
        </div>
        <div class="upload" >
            <input type="file" name="imagefilename" accept="image/*" />
        </div>
        <textarea class="statusText" id="statusText" rows="1" cols="60" placeholder="Update Your Status..." ></textarea>
        <input type="submit" value="Post!" >
    </form>
    </div>
</body>
</html>

这是我存储会话的登录页面:

<?php

session_start();

include 'includes/db_connect.php';
?>
<html>
<head>
<title>Howlers | Login</title>
</head>

<body>
<div class="login" >
<?php

if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
 header('Location=home.php');
 ?> 
 <?php
}
elseif(!empty($_POST['username']) && !empty($_POST['password']))
{
$username = mysqli::real_escape_string($_POST['username']);
$password = md5(mysqli::real_escape_string($_POST['password']));

$checklogin = mysqli::query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");

if(mysqli::num_rows($checklogin) == 1)
{
    $row = mysqli::fetch_array($checklogin);
    $email = $row['Email'];

    $_SESSION['Username'] = $username;
    $_SESSION['Email'] = $email;
    $_SESSION['LoggedIn'] = 1;

    header('Location=home.php');
}
else
{
    echo "<h1>Error</h1>";
    echo "<p>Sorry, your account could not be found. Please <a href='login.php'>click here to try again</a>.</p>";
}
}
else
{
?>

<h1>Login</h1>

<p>Thanks for visiting! Please either login below, or <a href="register.php">click here to register</a>.</p>

<form method="post" action="login.php" name="loginform" id="loginform">
<fieldset>
    <label for="username">Username:</label><input type="text" name="username" id="username" /><br />
    <label for="password">Password:</label><input type="password" name="password" id="password" /><br />
    <input type="submit" name="submit" id="submit" value="Login" />
</fieldset>
</form>

<?php
}
?>
</div>
</body>
</html>

这是结果的图片:

http://prntscr.com/5580lh

我不确定我在做错了什么可以请任何人帮助我:(?

3 个答案:

答案 0 :(得分:1)

错误之一是登录页面中的重定向:

你写header('Location=home.php');

等于错误,请使用双点:header('Location:home.php');

答案 1 :(得分:1)

您没有在主页中关闭if条件:

if(!isset($_SESSION['LoggedIn']) && !isset($_SESSION['Username']))
{
 header('Location:home.php');
}

你忘了关闭if条件这是致命的错误!

答案 2 :(得分:0)

在您的网页中进行以下更改

首页

<?php

session_start();

include 'includes/db_connect.php';

if(!isset($_SESSION['LoggedIn']) && !isset($_SESSION['Username']))
{
 header('Location:home.php');
}

?> ...

登录页面

    ...
<?php

    if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
    {
     header('Location:home.php');
     ?> 
     <?php
    }
    ?> 
...

...    
if(mysqli::num_rows($checklogin) == 1)
    {
        $row = mysqli::fetch_array($checklogin);
        $email = $row['Email'];

        $_SESSION['Username'] = $username;
        $_SESSION['Email'] = $email;
        $_SESSION['LoggedIn'] = 1;

        header('Location:home.php');
    }
...