2012-07-13 44 views
-1

我找不到我的错误,我可以帮忙吗?我正在尝试使用cookie登录代码。先谢谢你!我似乎不能看到我的错误。我希望有人能看到我缺少的东西。这个错误是在第47行的某个地方,但我知道这并不意味着它就是这样。我的代码中意外的T_ELSE错误,有人能看到吗?

<?php 
    if(isset($_POST['sent']) && $_POST['sent'] == "yes") 
    { 
     foreach($_POST as $field => $value) 
     { 
      if($value == "") 
      { 
       $blank_array[$field]= $value; 
      } 
      else 
      { 
       $good_data[$field]=strip_tags(trim($value)); 
      } 
     } 
    } 

    if(@sizeof($blank_array) > 0) 
    { 
     $message = "<p style='color: red; margin-bottom: 0; font-weight: bold'> Error.</p>"; 
     extract($blank_array); 
     extract($good_data); 
     include("form_log.php"); 
     exit(); 
    } 

    include("dbstuff.php"); 
    $cxn = mysqli_connect($host,$user,$password,$database) or die ("coulnt connect"); 
    $query = "SELECT first_name FROM customer WHERE user_name='$_POST[user_name]' AND  password=md5('$_POST[password]')"; 
    $result = mysqli_query($cxn,$query) or die ("couldnt query"); 
    $n_row = mysqli_num_rows($result); 

    if($n_row < 1) 
    { 
     $message = "<p style='color: red; margin-bottom: 0; font-weight: bold'> Not found.  </p>"; 
     extract($_POST); 
     include("form_log.php"); 
     exit(); 
    } 
    else 
    { 
     $row=mysqli_fetch_assoc($result); 
     setcookie("first_name",$row['first_name']); 
     setcookie("auth","yes"); 
     header("Location: secret_page_cookie.php"); 
    } 

    else 
    { 
     $user_name = ""; 
     $password = ""; 
     include("form_log.php"); 
    } 
?> 

对不起没有缩进,但这很难缩进。第二别的去,如果(@sizeof)..

+7

几乎所有开发人员都使用缩进来格式化他们的代码是有原因的。 – 2012-07-13 17:40:55

+1

我有义务在这里提到SQL注入攻击和弱密码哈希。 – Ryan 2012-07-13 17:43:01

+1

另外,像你这样使用'include'是你很快就会后悔的事情,在没有打开form_log.php文件的情况下实际阅读这段代码中的内容是完全不可能的。看起来像一个理想的候选人转换成我的功能! – fvu 2012-07-13 17:44:02

回答

7
if ($n_row < 1) { 
    $message = "<p style='color: red; margin-bottom: 0; font-weight: bold'> Not found.  </p>"; 
    extract($_POST); 
    include("form_log.php"); 
    exit(); 
} else { 
    $row=mysqli_fetch_assoc($result); 
    setcookie("first_name",$row['first_name']); 
    setcookie("auth","yes"); 
    header("Location: secret_page_cookie.php"); 
} else { 
    $user_name = ""; 
    $password = ""; 
    include("form_log.php"); 
} 

你有两个else声明在这里。您需要确定它属于哪个if声明或将其转换为elseif条件。

4

线47:

else 
{ 
$user_name = ""; 
$password = ""; 
include("form_log.php"); 
} 

只能有一个else,每if这是第二else

2

你有两个其他的块到最后,应该只有一个。而且你的代码可以广泛应用于SQL注入攻击。不要通过串联构造SQL字符串,而是使用参数化查询。

+0

'parameterised'每日一词。 – iambriansreed 2012-07-13 17:57:51

+0

在东方,它可能现在我已经纠正了早先的错字... iPad太聪明...;) – 2012-07-13 17:59:08