我正在尝试创建简单的服务器端验证,这是我的方法
public function create() {
$name = $_POST['name'];
session_start();
unset($_SESSION['errors']);
$count = $this->model->checkIfUserExists($name);
if($count > 0) {
$_SESSION['errors'] = array(
'message' => 'User Already Exists',
'variables' => array(
'name' => $_POST['name'],
'password' => $_POST['password'],
),
);
header('location: ' . URL . 'user/registration');
exit;
}
$data = array();
$data['name'] = $_POST['name'];
$data['password'] = $_POST['password'];
$this->model->create($data);
header('location: ' . URL);
}
以及来自registration.php的代码
if (isset($_SESSION['errors']) && count($_SESSION['errors']) > 0) {
echo '
- ';
foreach ($_SESSION['errors'] as $error) {
echo '
' . $error['message'] . '';}
echo '
';unset($_SESSION['errors']);
}
?>
但我得到了错误
Warning: Illegal string offset 'message' in C:\xampp\htdocs\test\views\user\registration.php on line 6
�
Notice: Undefined index: message in C:\xampp\htdocs\test\views\user\registration.php on line 6
怎么解决?