0

我已经阅读了很多关于此的文章和 SO 问题,但我没有成功做到这一点。这是一些参考参考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 函数,我想登录的网站在 cake php 中。

它向我显示登录已完成。但是当我点击 url 时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

请帮我解决这个问题。谢谢

4

1 回答 1

0

您必须在 http://192.168.1.31/Eb/projectmanager/login中编写登录脚本

与传递的凭据匹配并在成功时设置会话。

于 2015-06-12T07:16:38.210 回答