我試圖創建簡單的服務器端驗證數據,這是我的方法
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的
<?php
if (isset($_SESSION['errors']) && count($_SESSION['errors']) > 0) {
echo '<ul>';
foreach ($_SESSION['errors'] as $error) {
echo '<li>' . $error['message'] . '</li>';
}
echo '</ul>';
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
如何解決?
你可以嘗試的你的var_dump' $ _SESSION ['錯誤']'? – Gwendal
是的,我做了'var_dump($ _ SESSION ['errors']);'工作。結果:'array(2){[「message」] => string(19)「User already already exists」[「variables」] => array(2){[「name」] => string(5) ['password'] => string(5)「admin」}}' – Heidel
這兩個文件請在'<?php'後面寫'session_start();'然後檢查一次 –