我已经阅读了很多关于这个的文章和问题,但我没有成功做到这一点。 这里有一些参考参考1 参考2 我想要的是通过以下方式登录网站 curl .我所做的如下。它在本地系统上
网址http://192.168.1.31/Eb/user/login/loginnew
public function actionLoginnew()
{
$fields = array(
'User'=>array(
'email' => 'xxxxxxx@zzzzzzzzzzz.com',
'password' => 'yyyyyy'
)
);
$urltopost="http://192.168.1.31/Eb/projectmanager/login";
$ckfile = tempnam ("/tmp", 'cookiename');
$fields_string = http_build_query($fields);
$ch = curl_init ($urltopost);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, false);
$returndata = curl_exec ($ch);
$headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);
curl_close ($ch);
print_r($headers);die;
}
这是我的Yii功能,我想登录的网站是蛋糕PHP。
它向我显示登录完成.但是当我点击网址http://192.168.1.31/Eb/projectmanager/
它再次将我重定向到登录页面时。
http://192.168.1.31/Eb/projectmanager/login
上的登录脚本
public function admin_login()
{
$this->__login();
$this->render('admin_login');
}
private function __login()
{
$this->layout = 'default';
if (AuthComponent::user())
$this->redirect(array('controller' => 'projects', 'action' => 'index'));
if ($this->request->is('post'))
{
$this->User->set($this->request->data);
if ($this->Auth->login())
{
echo "logged in";
if ($this->request->data['User']['remember_me'] == 1)
{
unset($this->request->data['User']['remember_me']);
$this->request->data['User']['password'] = $this->Auth->password($this->request->data['User']['password']);
$this->Cookie->write('cepp_pauth', $this->request->data['User'], true, '2 weeks');
}
if (!AppAuth::is(UserRoles::Admin) && AppConfig::read('System.maintenance') === true)
{
$this->Session->setFlash(__('The system is in maintenance mode.'), Flash::Error);
$this->redirect($this->Auth->logout());
}
$this->Session->setFlash(__('You successfully logged in.'), Flash::Success);
if (!empty($this->request->params['admin']))
$this->redirect(array('controller' => 'projects', 'action' => 'index'));
else
$this->redirect($this->Auth->redirectUrl());
} else
{
unset($this->request->data['User']['password']);
$this->Session->setFlash(__('The username/password you provided were incorrect.'));
}
}
$this->set('title_for_layout', __('Login'));
}
生成 cookie 文件,内容为
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.
#HttpOnly_192.168.1.31 FALSE / FALSE 1434106233 CLIENTENGAGE in7nhb9gd2l24fioso3cq0ol16
请帮我解决这个问题.谢谢
编写的登录脚本http://192.168.1.31/Eb/projectmanager/login
与传递的凭据匹配,并将会话设置为成功。