[英]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: 因此,我的问题是:
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. 您应该执行$_POST
的var_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.