简体   繁体   中英

What's wrong with my sessions?

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

one of error is the redirect in the login-page:

you write header('Location=home.php');

The equal is wrong, use the double-point: header('Location:home.php');

You didn't close the if condition proper in home page:

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

you forgot to close the if condition which was fatal error!

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');
    }
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM