我正在尝试创建一个聊天框,但在登录并保存在数据库中后,我无法让 user_id 在会话中正确保存。
这是登录功能
public function login() {
$this->form_validation->set_rules('email', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
if ($this->form_validation->run() == FALSE) {
// return main page if submitted form is invalid.
$this->load->view('abt_login');
} else {
$this->load->model('abt_db');
$q = $this->abt_db->check_login(
$this->input->post('email'), $this->input->post('password')
);
if ($q) {
redirect('index.php/abovetheblues/abt_abovetheblues');
$this->abt->set_session();
} else {
$this->show_login(true);
}
}
}
这是我的 Javascript 代码
$(document).ready(function(){
$("a#submit").click(function(){
var chat_message_content = $("input#chat").val();
if(chat_message_content == ""){
return false;
}
$.post(base_url + "index.php/abovetheblues/add_chat_messages", {
chat_message_content : chat_message_content,
user_id : user_id
},
function(data){
alert(data);
},"json");
return false;
});
return false;
});
这是我的控制器
function add_chat_messages() {
// Grab the $chat_message_content, $user_id
$user_id = $this->input->post($this->session->userdata("user_id"));
$chat_message_content = $this->input->post('chat_message_content');
$this->abt_db->add_chat_message($user_id, $chat_message_content);
}
这是我的模型
function check_login($email, $password) {
$this->load->database();
// Query to retrieve the user's details
// based on the received username and password
$this->db->from('user');
$this->db->where('email', $email);
$this->db->where('password', $password);
$q = $this->db->get()->result();
// The results of the query are stored in $q.
// If a value exists, then the user account exists and is validated
if (is_array($q) && count($q) == 1) {
// Set the users details into the $details property of this class
$this->details = $q[0];
// Call set_session to set the user's session
$this->set_session();
return true;
}
return false;
}
function set_session() {
// session->set_userdata is a CodeIgniter function that
// stores data in a cookie in the user's browser. Some of the values are built in
// to CodeIgniter, others are added (like the user_id).
$this->session->set_userdata(array(
'user_id' => $this->details->user_id,
'email' => $this->details->email,
'username' => $this->details->username,
'isLoggedIn' => true
)
);
}
function add_chat_message($user_id, $chat_message_content) {
$query_str = "INSERT INTO chat_message(user_id, chat_message_content) VALUES (?,?,?)";
$this->db->query($query_str, array($user_id, $chat_message_content));
}
还有我的查看页面
<script type="text/javascript">
var user_id = "<?php echo $this->session->userdata("user_id"); ?>";
var base_url = "<?php echo base_url();?>";
</script>
<!--loads the header-->
<?php $this->load->view('abt-header'); ?>
<!--this is the login page-->
<div data-role="page" id="Abt-chat" data-add-back-btn="true">
<div data-role="header" data-position="fixed">
<h1>Peer Chat</h1>
</div>
<div data-role="content">
<div data-role="fieldcontain">
<div id="chat_viewport"></div>
<p>
<label>Input Chat: </label>
<input name="chat" id="chat" type="text" value=""/>
</p>
<p>
<?php echo anchor('#', 'Send Chat', array('title' => 'Send Chat', 'id' => 'submit')); ?>
</p>
</div>
<?php echo form_close(); ?>
</div>
</div>