我在CodeIgniter中写了登录表单。当我写错了数据时,它会将信息提交给数据库,它会给我带来我想要的错误和验证错误。但是,当我写出正确的用户名并通过并按LOG IN
时,它什么也没做,但当我刷新页面时,它显示我已登录。我希望它在我按下登录后立即重定向主页面。我认为问题出在ajax或控制器上,但我并不确切知道。因为当我按正确的用户名登录并传递它时,它在控制台中什么都不显示。
以下是我的观点:
<div id="loginmsg"></div>
<?php $attributes = array('class' => 'rex-forms', 'name' => 'loginform', 'id' => 'loginform'); ?>
<?= form_open_multipart('user/login', $attributes); ?>
<div class="form-group">
<input name="usernameforlog" id="usernameforlog" type="text" class="form-control" placeholder="Имя пользователя">
</div>
<div class="form-group">
<input name="passwordforlog" id="passwordforlog" type="password" class="form-control" placeholder="Пароль">
</div>
</div>
<div class="modal-footer">
<button type="submit" name="submitforlog" class="rex-bottom-medium rex-btn-icon btnsubmitforlog">
<span class="rex-btn-text">войти</span>
<span class="rex-btn-text-icon"><i class="fa fa-arrow-circle-o-right"></i></span>
</button>
</div>
</form>
这是我的控制器(我评论了重定向行,因为它不起作用):
public function login(){
$data['title'] = 'Sign In';
$validator = array('success' => false, 'messages' => array());
$validate_data = array(
array(
'field' => 'usernameforlog',
'label' => 'username',
'rules' => 'trim|required|alpha_dash'
),
array(
'field' => 'passwordforlog',
'label' => 'password',
'rules' => 'trim|required|md5'
)
);
$this->form_validation->set_rules($validate_data);
$this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>');
if ($this->form_validation->run() === FALSE)
{
// fails
$validator['success'] = false;
foreach ($_POST as $key => $value) {
$validator['messages'][$key] = form_error($key);
}
}else {
// Get username
$username = $this->input->post('usernameforlog');
// Get and encrypt the password
$password = $this->input->post('passwordforlog');
// Login user
$user_id = $this->user_model->login($username, $password);
if($user_id){
// Create session
$user_data = array(
'instructor_id' => $instructor_id,
'id' => $id,
'instructors_slug' => $username,
'name' => $name,
'logged_in' => true
);
$this->session->set_userdata($user_data);
$validator['success'] = true;
$validator['messages'] = array();
//redirect('');
} else {
$validator['success'] = false;
$validator['messages'] = '<div class="alert alert-danger text-center">Неверный логин или пароль</div>';
}
}
echo json_encode($validator);
}
这是我的模特:
public function login($username, $password) {
//validate
$this->db->where('instructors_slug', $username);
$this->db->where('password', $password);
$result = $this->db->get('instructors');
if($result->num_rows() == 1){
return $result->row(0)->id;
} else {
return false;
}
}
这是ajax文件:
$(document).ready(function() {
$("#loginform").unbind('submit').bind('submit', function() {
var form = $(this);
$.ajax({
url: form.attr('action'),
type: form.attr('method'),
data: form.serialize(),
dataType: 'json',
success:function(response) {
console.log(response);
if(response.success) {
//i want here to redirect main page
}
else {
$("#loginmsg").html(response.messages);
$.each(response.messages, function(index, value) {
var element = $("#"+index);
$(element)
.closest('.form-group')
.removeClass('has-error')
.removeClass('has-success')
.addClass(value.length > 0 ? 'has-error' : 'has-success')
.find('.text-danger').remove();
$(element).after(value);
});
}
} // /success
}); // /ajax
return false;
});
});
答案 0 :(得分:0)
更改控制器中的以下代码:
if($user_id){
// Create session
$user_data = array(
'instructor_id' => $instructor_id,
'id' => $id,
'instructors_slug' => $username,
'name' => $name,
'logged_in' => true
);
$this->session->set_userdata($user_data);
$validator['success'] = true;
$validator['messages'] = array();//In case no data needs to be passed
// redirect('');//Comment this part as it will redirect and don't pass the data to the AJAX call
}else{
//Your else part
}
//here just json encode and return .
答案 1 :(得分:0)
设置会话时,问题出在数组中。
未定义变量$ instructor_id,$ id等
def password_checker():
program_acceptance = "Welcome to the Program! "
acceptable_password = "Godisgood"
print("Please Enter the Password")
while True:
password = input("Password: ")
if password == acceptable_password:
print(program_acceptance)
break
if password != acceptable_password:
print("Invalid password, please try again...")
password_checker()