dream8877 2014-11-11 08:47
浏览 57
已采纳

我的课程有什么问题?

When i try to go to the home page, the page becomes blank and it is because of the session tags because when i remove them the page shows.

Home page:

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

This is my login page where the session is stored:

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

here is a picture of the outcome:

http://prntscr.com/5580lh

I am not sure what i am doing wrong here can anyone please help me :(?

  • 写回答

3条回答 默认 最新

  • duanjiyun7391 2014-11-11 08:58
    关注

    Make following changes in your pages

    Home Page

    <?php
    
    session_start();
    
    include 'includes/db_connect.php';
    
    if(!isset($_SESSION['LoggedIn']) && !isset($_SESSION['Username']))
    {
     header('Location:home.php');
    }
    
    ?> ...
    

    Login Page

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

    and

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

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi