|
本帖最后由 xiaoxinxx 于 2014-10-10 16:39 编辑
curl模拟登录代码:
<?php
header("content-type:text/html;charset='utf-8'");
$login_url = 'http://192.168.1.100/app/index.php/auth/do_login';//登录页地址
$post_fields = array();
$post_fields['loginfield'] = 'user';
$post_fields['loginsubmit'] = 'true';
$post_fields['user'] = 'cj';
$post_fields['pass'] = 'a';
$cookie_file = tempnam('./temp','cookie');
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
$res=curl_exec($ch);
echo $res;
curl_close($ch);
?>
处理登录代码:
function do_login()
{
$username = $this->input->post("user");
$password = $this->input->post("pass");
try {
$this->load->model("User_Model");
$user = $this->User_Model->find_user_byname(trim($username));
if($user->pw != md5(trim($password))) throw new Exception("用户名或密码错误!");
$this->load->library('session');
$this->session->set_userdata(array(
'user_id' => $user->tid,
'user_name' => $user->xm;
));
$msg['code'] = 1;
$msg['msg'] = "登录成功";
$msg['user_id'] = $user->tid;
$msg['user_name'] = $user->xm;
$msg['session_id'] = $this->session->userdata('session_id');
} catch (Exception $e) {
$msg['code'] = 0;
$msg['msg'] = $e->getMessage();
}
echo json_encode($msg);
}
问题:
采用curl模拟登录后返回的$msg的值和正常登录返回的$msg的值都是一样的。但是采用curl模拟登录之后,调用
echo json_encode($this->session->userdata("user_id"));却获取不到数据,而正常登录之后调用
echo json_encode($this->session->userdata("user_id"));同样的代码却能够获取到数据。这个问题怎么解决?
|
|