登录php代码保持清爽?

时间:2016-06-16 11:17:24

标签: php html

我的login.php页面没有登录时遇到问题,我无法解决为什么每当我尝试登录时它都会不断刷新页面。我正在使用我的index.php直接重定向到我的login.php,不知道这是否是问题,因为我改变它之前它正在工作。有什么想法吗?

的index.php

<?php
    header("Location: Login.php");
?>

的login.php

<?php
ob_clean();session_start(); 

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

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

$Username = $_POST['username'];
$EnteredPassword = $_POST['password'];

if (isset($_POST['submit'])){
    if (is_dir("USERS/".$Username) === true){
        $myFile=fopen("USERS/".$Username."/Password.txt","r") or exit("Can't open file!");
        $CorrectPassword = fgets($myFile);
        fclose($myFile);

        if ($CorrectPassword == $EnteredPassword){
            $_SESSION['loggedin'] = true;
            header("Location: Home.php");       
        }

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

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

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

        <title>Project Archive - 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="headerLoginDiv">
                <div id="titleLoginDiv">
                    <p>Project Archive</p>
                </div>
            </div>

            <h1 align="center">Login</h1>
            <h3 align="center">Welcome. Please login to continune.</h3>
            <form method="post" action="index.php">
            <div id="userNameLoginDiv">
                <p align="center">Username:</p>
                <input type="text" name="username" size="12">
            </div>  

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

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

1 个答案:

答案 0 :(得分:2)

显然,如果你转到setlocale(LC_ALL, "de_DE.utf8"); $myDate = strtotime('2016-06-17'); //example echo strftime('Write proper format there', $myDate); ,你会要求浏览器转到index.php,无论用户是否登录:

login

代替以上代码,检查并发送用户:

<?php
    header("Location: Login.php"); // This blindly redirects the user to the login page.
?>

此外,请不要忘记在开始时使用<?php // Start the session. session_start(); // Instead check if the user is logged in and then redirect. if (!isset($_SESSION['loggedin'])) header("Location: Login.php"); ?> 开始会话。