2016-03-29 105 views
-6

我正在登录系统,我想,如果用户输入了错误的用户名&密码将他返回到登录页面,如果其确定重定向到admin/index.php它不是工作,没有错误。php登录不工作

功能代码和HTML代码

if(isset($_POST['submit_form'])){ 
     $username = mysqli_real_escape_string($_POST['username']); 
     $password = mysqli_real_escape_string($_POST['password']); 
     $query = mysqli_query("SELECT * FROM users WHERE username = '$username' AND password ='{$password'"); 
     if($query==false){ 
      set_message(" Your password or username are wrong "); 
      redirect("login.php"); 
     }else{ 
      redirect("admin"); 
     } 
    } 

的HTML代码

<form class="form-inline" role="form" action="" method="post" enctype="multipart/form-data"> 
    <div class="form-group">   
     <label class="form-group" for="username">username</label> 
     <input type="text" name="username" class="form-control" id="username" placeholder="username"> 
    </div> 
    <div class="form-group"> 
     <label class="sr-only" for="password">Password</label> 
     <input type="password" name="password" class="form-control" id="password" placeholder="password"> 
    </div> 
    <button type="submit" name="submit_form" class="btn btn-black"> 
    <input type="hidden" name="submit_form" value="<?php echo session_id()"/> 
     Login 
    </button> 
</form> 

功能

function redirect($location) { 
    header("Location : $location"); 
} 

function query($sql){ 
    global $connection ; 
    return mysqli_query($connection, $sql); 
} 

function confirm($result){ 
    global $connection; 
    if(!$result){ 
     die("QUERY FAILED " . mysqli_error($connection)); 
    } 
} 

function escape_string($string){ 
    global $connection ; 
    return mysqli_real_escape_string($connection, $string); 
} 
+0

请不要直接在函数中使用全局变量,而应将其用作参数。 –

+3

“*它不工作,没有错误*” - 不工作*如何*?发生了什么,什么不发生?我们不知道这一点,因为我们所知道的只是您向我们展示的代码,这并不是很重要。你是否启用了['error_reporting(E_ALL);'](http://php.net/manual/en/function.error-reporting.php)?你甚至不会为你的函数显示代码(比如'query()','confirm()','redirect()')或你如何使用这个函数。 – Qirel

+0

如果用户名和密码正确,它应该将我重定向到admin/index.php。我在问题 – medboy94

回答

0

是的,对不起,我只是习惯了这件事情。感谢您指出错误。

//this will eliminate the BOT hitting up the forms 
    if(isset($_POST['submit_form']) && $_POST['submit_form']==session_id()){ 
    $username = mysql_real_escape_string($_POST['username']); 
    $password = mysql_real_escape_string($_POST['password']); 
    $query = mysqli_query("SELECT * FROM users WHERE username = '$username' AND password ='$password'"); 
    if($query==false){ 
     set_message(" Your password or username are wrong "); 
     redirect("login.php"); 
    }else{ 
     redirect("admin"); 
    } 
} 

而且我更新了表单。

<form class="form-inline" role="form" action="" method="post" enctype="multipart/form-data"> 
<div class="form-group">   
    <label class="form-group" for="username">username</label> 
    <input type="text" name="username" class="form-control" id="username" placeholder="username"> 
</div> 
<div class="form-group"> 
    <label class="sr-only" for="password">Password</label> 
    <input type="password" name="password" class="form-control" id="password" placeholder="password"> 
</div> 
<button type="submit" name="submit_form" class="btn btn-black"> 

    Login 
</button> <input type="hidden" name="submit_form" value="<?php echo session_id()"/> 

解析了查询时,我会用sprintf。它消除了SQL注入。你可以这样做:

希望这对你有效。