如果字段为空,登录表单是否有效?

时间:2013-10-03 16:56:58

标签: php forms

我已经设置了一个内部网站,用于登录少数用户,用户不会改变,只能由授权人员完成。

所以我需要一些非常简单的东西,尽可能不使用数据库。

所以我制作了以下脚本,除了我的一个测试人员尝试登录两个字段为空之外,效果很好......并且它有效吗?

无法弄明白。

登录

    <form id="slick-login" action="_login_action.php" method="post">
<label for="username">Username</label>
<input type="text" name="username" class="placeholder" placeholder="username"/>
<label for="password">Password</label>
<input type="password" name="password" class="placeholder" placeholder="password"/>

<input type="submit" value="Login" />

_login_action.php

    <?php
$usernames = array("admin", "user", "guest", "input");
$passwords = array("admin123", "user123", "guest123", "input123");
$page = "index.php";
for($i=0;$i<count($usernames);$i++){
  $logindata[$usernames[$i]]=$passwords[$i];
}
if($logindata[$_POST["username"]]==$_POST["password"]){
session_start();
$_SESSION["username"]=$_POST["username"];
header('Location: '.$page);
exit;
}else{
header('Location: login.php?wrong=1');
exit;
}
?>

这是我在需要保护的页面上包含的文件;

<?php
session_start();
if(!isset($_SESSION["username"])&& !empty($_SESSION['username'])){
header('Location: login.php');
exit;
}
?>

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

需要再检查一次:

if(!empty($_POST["username"]) && $logindata[$_POST["username"]]==$_POST["password"])
相关问题