使用php / cookies创建登录系统

时间:2016-01-29 09:29:47

标签: php session cookies

如果有人不介意帮助我,我真的很挣扎。我肯定没有php大师这肯定显示!

我正在尝试使用php / sessions / cookies等登录系统,所有我似乎到目前为止都是一个错误,每当我尝试登录(它的意思是登录然后基本上说你好“用户”以及退出按钮。我会尝试附加/减少我的能力。

提前致谢!

index.php(带登录按钮的主登录页面)

<?php
include('includes/sessions.inc.php');
?>
<?php 
        /////////////////////////// conditional includes here
        if(isset($_SESSION['login'])){
            include('includes/session-logout.inc.php');
        }else{
            include('includes/session-login.inc.php');
        }
?>

会话的login.php

<div id="login">
<?php 
error_reporting(E_ALL|E_STRICT); ini_set('display_errors', true);

if($_SESSION['count'] > 0 && $_SESSION['count'] <= 3 ){
echo "<p class=\"error\">Sorry incorrect</p>";
}
if($_SESSION['count'] > 3){
    echo "<p class=\"error\">Too many attempts</p>";
}else{
// don't forget to close this with } after the HTML form
?>

<form id="loginForm" name="loginForm" method="post" action="logic/checklogin.php">

              <label for="username">Username</label>
              <input name="username" type="text" id="username">
              <label for="password">Password</label>
              <input name="password" type="password" id="password">
              <input type="submit" name="Submit" value="Login">
</form>
<?php 
}

?>
</div>

sessions.inc.php

<!doctype html><?php
////////////////////// START SESSION HERE ///////////////////////////
session_set_cookie_params(0, '/~b4019635', 'homepages.shu.ac.uk', 1, 1);
session_start();
?></html>

checklogin.php

<?php
error_reporting(E_ALL|E_STRICT); ini_set('display_errors', true);
// check login and create SESSION and count attempts
include('../includes/sessions.inc.php');
//check login / password combination
if ($_POST['username'] == "admin" && $_POST['password'] == "letmein"){
$_SESSION['login'] = 1;
}else{
 if(!isset($_SESSION['count'])){

    // first fail
    $_SESSION['count'] = 1;
    }else{
    // 2nd or more fail
    $_SESSION['count']++;
    }
}


// redirect browser
header("Location: ".$_SERVER['HTTP_REFERER']);
?>

我觉得像session_start();如果在检查器中没有创建cookie,那么它与它有关吗?

1 个答案:

答案 0 :(得分:0)

放置session_start();在<!doctype html>标记之前。确保之前没有其他输出。

相关问题