Javascript 我无法在会话中正确保存用户详细信息
我正在尝试创建聊天框,在登录并保存到数据库后,我无法在会话中正确保存用户id 这是登录功能Javascript 我无法在会话中正确保存用户详细信息,javascript,ajax,codeigniter,session,Javascript,Ajax,Codeigniter,Session,我正在尝试创建聊天框,在登录并保存到数据库后,我无法在会话中正确保存用户id 这是登录功能 public function login() { $this->form_validation->set_rules('email', 'Username', 'trim|required|xss_clean'); $this->form_validation->set_rules('password', 'Password', 'trim|required|x
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>
var user_id=“”;
var base_url=“”;
同侪聊天
输入聊天:
如果会话中已经定义了用户id,则无需每次通过javascript发送该id,或在页面中将其定义为javascript变量
在控制器中,您必须给出通过javascript传递的变量名,但您调用的是会话数据,它将为您提供id而不是变量名
$user_id = $this->input->post($this->session->userdata("user_id")); // wrong
$user_id = $this->input->post("user_id"); // right
$user_id = $this->session->userdata("user_id"); // this way without pass through javascript
代码中有几个错误: 我从您的型号开始:
if ($q) {
$this->abt_db->set_session($q);
redirect('index.php/abovetheblues/abt_abovetheblues');
} else {
$this->show_login(true);
}
这里最重要的问题是:你不应该在数据库中存储用户的密码,这是一个非常糟糕的做法,使用PHP函数和BLOWFISH散列类型来加密密码,然后将加密版本保存在你的数据库中。有两种类似的方法可能需要考虑。
仔细检查内部评论:
function check_login($email, $password)
{
$this->load->database();
$this->db->from('user')
->where('email', $email)
->where('password', $password); // <-- Check the encryped password
$q = $this->db->get();
if ($q->num_rows() == 1) {
// Set the users details into the $details property of this class
$this->details = $q->first_row();
// Call set_session to set the user's session
$this->set_session();
return TRUE;
}
return FALSE;
}
function set_session()
{
$this->session->set_userdata(array(
'user_id' => $this->details->user_id,
'email' => $this->details->email,
'username' => $this->details->username/*,
// You don't need to store this in session
// If user is logged-in, his/her user_id would be in session
// you can easily check whether user is logged-in or not
// by looking for user_id in session.
'isLoggedIn' => true*/
));
}
function add_chat_message($user_id, $chat_message_content)
{
// You are binding two parameter to SQL query,
// But the following query has three question mark.
// $query_str = "INSERT INTO chat_message(user_id, chat_message_content) VALUES (?,?,?)";
$query_str = "INSERT INTO chat_message(user_id, chat_message_content) VALUES (?,?)";
$this->db->query($query_str, array($user_id, $chat_message_content));
}
在您的Controller/add\u chat\u messages
方法中,使用$this->input->post($this->session->userdata(“user\u id”)
是错误的
如果用户必须登录网站,则无需通过POST请求传递用户的id
。只需从会话中阅读即可:
function add_chat_messages()
{
// The following statement is wrong!
//$user_id = $this->input->post($this->session->userdata("user_id"));
$user_id = $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);
}
最后,在客户端,JavaScript部分:
您不需要向服务器发送用户id,因为这些用户可以访问已登录的聊天页面:
$.post(base_url + "index.php/abovetheblues/add_chat_messages", {
chat_message_content : chat_message_content/*,
user_id : user_id*/ // <-- This is not necessary
}, function(data) {
alert(data);
},"json");
更新#2:
将检查登录
和设置型号中的会话
功能更改为:
function check_login($email, $password)
{
$this->load->database();
$this->db->from('user')
->where('email', $email)
->where('password', $password);
$q = $this->db->get();
if ($q->num_rows() == 1) {
// Return the users details
return $q->first_row('array');
}
return FALSE;
}
function set_session($details)
{
$this->session->set_userdata(array(
'user_id' => $details['user_id'],
'email' => $details['email'],
'username' => $details['username']
));
}
在您的控制器中:
if ($q) {
$this->abt_db->set_session($q);
redirect('index.php/abovetheblues/abt_abovetheblues');
} else {
$this->show_login(true);
}
最后,在JS文件中,从$.post
方法中删除json
数据类型
还有一件事,在blues/add\u chat\u messages
方法上方不向输出发送任何内容(不回显任何内容),因此返回的数据
为空:
$.post(base\u url+“index.php/overseblues/add\u chat\u messages”{
聊天室信息内容:聊天室信息内容/*,
用户id:用户id*/
},函数(数据){
//数据将是空的
警报(数据);
});
如果您还发布了一个测试URL,那么我们可以查看该站点。如果我必须通过javascriptwith javascript传递它,我该如何编写它?只需添加$user\u id=$this->input->post(“user\u id”);我已经做了这些更正,但它仍然没有在会话中保存用户Id,如果必须通过JavascriptI,我如何发布用户Id?我之前已经这样做了,但仍然不起作用,当我在firebug中查看响应时,它似乎没有在会话中保存用户Id,可能是firebug阻止了它吗?@Omade I高度记录M要存储会话并将$config['sess\u encrypt\u cookie']
设置为TRUE
。另外,在set\u session()
函数中,确保$this->details->user\u id
存在且不为空(使用var\u dump
)。对add\u chat\u message()执行相同的测试
Controller/method for$user\u id
。这就是我的问题,当我做测试时,user\u id是空的。我怀疑的另一件事是我用Javascript写的ajax帖子,但老实说,我库存充足,不知道该怎么办
if ($q) {
$this->abt_db->set_session($q);
redirect('index.php/abovetheblues/abt_abovetheblues');
} else {
$this->show_login(true);
}