简体   繁体   English

在PHP中进行表单验证后,标题重定向

[英]Header Redirect after form Validation in PHP

I am trying this code as part of form processing: 我正在尝试将此代码作为表单处理的一部分:

<?php

if(isset($_POST['senderEmail'])) 
    {
     try
     {

     require '_php/_security/validation.php';  //SEE BELOW

     $rules = array(               
            'senderEmail' => 'validEmail',
            'emailTextbox' => 'validTextbox',
        );

        $validation = new Validation();

        if ($validation->validate($_POST, $rules) == TRUE) {  
            require("_php/database/dbProcessing.php"); //Form Proccessing for database inclusion
        }

        else {  
          foreach($validation->emailErrors as $error){
            $emailErrors[] = $error;
            $_SESSION['$emailErrors'] = $emailErrors;

            header('Location:indexmobile.php#emailErrors'); 
            die('ABORT!');
        }
    }

    }
   catch (PDOException $e)
    {

      $error = 'Error adding elements to database: ' . $e->getMessage();
      echo "Error: " . $error;
      exit();
    }

exit();
}
?>

The validation.php where I do my validation has this: 我在其中进行验证的validation.php具有以下内容:

<?php

class Validation {

public $errors = array();
public function validate($data, $rules) {
    $valid = TRUE;
    foreach ($rules as $fieldname => $rule) {
        $callbacks = explode('|', $rule);
        foreach ($callbacks as $callback) {
            $value = isset($data[$fieldname]) ? $data[$fieldname] : NULL;
            if ($this->$callback($value, $fieldname) == FALSE) $valid = FALSE;
        }
    }
   return $valid;
}

public function validEmail($value, $fieldname) {
    $valid = !empty($value); 
    if ($valid  == FALSE) {
        $this->emailErrors[] = "The $fieldname is required";
        return $valid;
    } else {
        $valid = filter_var($value, FILTER_VALIDATE_EMAIL);
        if ($valid  == FALSE) $this->emailErrors[] = "The $fieldname needs to be a valid email";
        return $valid;
    }
}

public function validTextbox($value, $fieldname) {
    $valid = !empty($value);   
    if ($valid  == FALSE) {
        $this->emailErrors[] = "The $fieldname is required";
        return $valid;
    } else {
        $whitelist = '/^[a-zA-Z0-9 ,\.\+\\n;:!_\-@]+$/';  
        $textarea = strip_tags($value);
        $textarea = mysql_real_escape_string($textarea);  
        $valid = preg_match($whitelist, $textarea);
        if ($valid  == FALSE) $this->errors[] = "The $fieldname contains invalid characters";
        return $valid; 
     }
  }

}

Upon using this, Im have issues with the redirect (I think). 使用此功能后,我在重定向方面遇到了问题(我认为)。 It seems further that Im having errors in validation. 我似乎在验证方面还存在错误。 My questions are thus: 因此,我的问题是:

  1. Am I doing the header redirect correctly? 我是否正确执行标题重定向? I've read that " header() must be called before any actual output is sent,.." So is this the reason why this redirect is incorrect? 我已经读到“必须在发送任何实际输出之前调用header(),..”那么这就是重定向不正确的原因吗? how to make a redirect if i need to show/send something to the redirected page? 如果我需要在重定向页面上显示/发送内容,如何进行重定向?
  2. function validTextbox always ends up an error that the field is empty. 函数validTextbox总是会出现一个错误,指出该字段为空。 Why so? 为什么这样?
  3. Is my entire process of form validation a good way of validating form fields (which i learned from watching an online tutorial)? 表单验证的整个过程是否是验证表单字段的好方法(这是我从观看在线教程中学到的)? What is a better way? 有什么更好的方法?
  4. Is there something wrong with error reporting in this case? 在这种情况下,错误报告有问题吗?

Thank you for those who replies. 感谢您的答复。 I am new to PHP and trying my best to learn the language. 我是PHP的新手,正在努力学习该语言。

1 - There are several ways to pass on a message to the page you are redirecting to. 1-有几种方法可以将消息传递到您要重定向到的页面。 One is through $_GET like this 一个是通过$_GET这样的

$message="Some message for the next page.";
$message=urlencode($message);
header("Location:page.php?message=".$message);

then on page.php 然后在page.php

if(!empty($_GET['message']))
{
$_GET['message'];
}

similarly you can also use the session (less secure) 同样,您也可以使用会话(不太安全)

$_SESSION['message']='some other message';

then on page.php 然后在page.php

if (!empty($_SESSION['message']))
{
echo $_SESSION['message'];
unset($_SESSION['message']);
}

2 - I would have to see what you are passing to your validate function. 2-我必须查看您传递给validate函数的内容。 You should do a var_dump of $_POST and add that to your question. 您应该执行$_POSTvar_dump并将其添加到您的问题中。

3 - It depends on your criteria. 3-这取决于您的标准。 If you are just checking for emptiness its overkill. 如果您只是在检查是否为空,那它就是多余的。 I don't know what text you need / consider valid, but a regex is a reasonable way of enforcing validation. 我不知道您需要/认为有效的文本,但是正则表达式是执行验证的合理方法。

4 - See #2. 4-参见#2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM