0
你好,我試圖看看其他人,但無法找到我的基於他們的代碼和解決方案不工作的原因。如果我禁用(註釋掉)表單驗證,我可以獲取發佈數據以顯示。但如果留在它裏面總是出現錯誤,並且不顯示任何後期數據。我有自動加載我的模型,網址,表格,文本,安全,form_validationcodeigniter表單驗證總是假的
在控制器
// Log in user
public function login(){
$data['title'] = 'Sign In';
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
// validate rules for login cedentials
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
// if login credentials are not entered (validation is false) show login view
if(!$this->form_validation->run()){
$this->load->view('templates/header');
$this->load->view('users/login', $data);
$this->load->view('templates/footer');
echo "Not valid!";
echo "<br>";
print_r($this->input->post());
echo $this->input->post('username');
echo validation_errors();
echo $this->form_validation->run();
} else {
// Get username
$username = $this->input->post('username');
// Get and encrypt the password
$password = $this->input->post('password');
// Login user with model (database)
$user_id = $this->user_model->login($username, $password);
if($user_id){
// Create session
$user_data = array(
'user_id' => $user_id,
'username' => $username,
'logged_in' => true
);
// set session user data
$this->session->set_userdata($user_data);
// Set message
$this->session->set_flashdata('user_loggedin', 'You are now logged in');
redirect('users/index');
} else {
// Set message
$this->session->set_flashdata('login_failed', 'Login is invalid');
redirect('users/login');
}
}
} // end function login
鑑於形式
<?php echo form_open('users/login'); ?> <!-- the action is the controller users and the function register -->
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="my-input-control-100p">
<div class="form-group">
<input type="text" name="username" class="form-control" placeholder="Enter Username" required autofocus>
</div>
</div>
<div class="form-group">
<input type="password" name="password" class="form-control" placeholder="Enter Password" required autofocus>
</div>
<button type="submit" class="btn btn-primary btn-block">Login</button>
<br>
<div class="center_login">
<small><a href="<?php echo site_url("pages/view/register") ?>">Register here</a> </small>
<br>
<br>
<small><a href="<?php echo site_url("pages/view/forgot") ?>">Forgot Password or User Name</a> </small>
</div>
</div>
</div>
<?php echo form_close(); ?>
純文本密碼應該保存爲密碼驗證程序。當保存密碼驗證者只使用哈希函數是不夠的,只是添加一個鹽沒有提高安全性。相反,用隨機鹽迭代HMAC約100ms持續時間,然後用散列表保存鹽。更好的是使用諸如'PBKDF2','Rfc2898DeriveBytes','password_hash','Bcrypt','passlib.hash'或類似函數的函數。關鍵是要讓攻擊者花費大量時間通過暴力破解密碼。 – zaph
與翻轉的邏輯,我至少可以打印出用戶名和密碼(如果($ this-> form_validation-> run()!== false){) – tomcc84
這是關於保護用戶和他們的憑據,而不是開發人員的便利。永遠不要在服務器上以純文本保存密碼。使用PHP使用['password_hash'](http://php.net/manual/en/function.password-hash.php)和['password_verify'](http://php.net/manual/en/function。 password-verify.php),這對安全且易於使用。 – zaph