【发布时间】:2017-05-04 17:02:44
【问题描述】:
我正在使用 GUMP https://github.com/Wixel/GUMP 进行服务器端表单验证,并且对重定向后显示消息有疑问。
我想在提交后验证表单数据,如果出现错误则重定向到表单,但我不知道在重定向后将错误传递给表单的最佳方式。
我读过这个问题Header Redirect after form Validation in PHP,它提出了两种方法:
1.
$message="Some message for the next page.";
$message=urlencode($message);
header("Location:page.php?message=".$message);
2.
$_SESSION['message']='some other message';
答案的作者认为方法1更安全,但你能告诉我为什么会这样吗?
我也看过php-form-builder类https://github.com/lkorth/php-form-builder-class是怎么做的,他们好像用的是方法2:
/*Valldation errors are saved in the session after the form submission, and will be displayed to the user
when redirected back to the form.*/
public static function setError($id, $errors, $element = "") {
if(!is_array($errors))
$errors = array($errors);
if(empty($_SESSION["pfbc"][$id]["errors"][$element]))
$_SESSION["pfbc"][$id]["errors"][$element] = array();
foreach($errors as $error)
$_SESSION["pfbc"][$id]["errors"][$element][] = $error;
}
那么,我的问题是,哪种方法最好?使用$_GET 或会话变量传递错误?
附言如果我遗漏了什么,并且有一种更容易/内置于 GUMP 中的方法,请指出!
【问题讨论】:
标签: php validation redirect