Php 如何使用curl登录网站

Php 如何使用curl登录网站,php,curl,yii,remote-login,Php,Curl,Yii,Remote Login,我读过很多关于这方面的文章和问题,但我没有成功地做到这一点。这里有一些参考,我想通过curl登录到一个网站。下面是我所做的。它是基于本地系统的 这个的url是http://192.168.1.31/Eb/user/login/loginnew 这是我的yii函数,我想登录的网站是用php制作的 它向我显示登录已完成。但是当我点击网址时http://192.168.1.31/Eb/projectmanager/ 它再次将我重定向到登录页面 上的登录脚本http://192.168.1.31/Eb/

我读过很多关于这方面的文章和问题,但我没有成功地做到这一点。这里有一些参考,我想通过curl登录到一个网站。下面是我所做的。它是基于本地系统的

这个的url是http://192.168.1.31/Eb/user/login/loginnew

这是我的yii函数,我想登录的网站是用php制作的

它向我显示登录已完成。但是当我点击网址时http://192.168.1.31/Eb/projectmanager/ 它再次将我重定向到登录页面

上的登录脚本http://192.168.1.31/Eb/projectmanager/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

请帮我解决这个问题。谢谢您必须使用的登录脚本


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

请检查我已将脚本添加到此页面。您需要检查$this->Auth->login正在返回的内容。它正在返回true或false。我要在这种情况下走。因为curl响应打印已登录;意味着它在工作。这里为请求的url设置了会话。返回成功响应后,请为请求的域设置会话。登录响应后,您需要在客户端站点设置会话。检查会话并决定重定向到何处。您是否验证了登录参数名称以及curl调用是否成功登录?您可能需要检查输出。Yii倾向于根据内部数组索引命名参数,这些索引并不总是像您在curl参数中发布的那样干净。是的,它正在打印处于if$this->Auth->login条件下的登录。所以这意味着它是身份验证。但不创建会话变量。我认为由于域和子域的原因,您刚刚注意到您为curl指定了cookie jar和cookie文件-不要。只需指定cookiejar。您的登录不应使用现有cookie,只应设置它们。我已删除cookie文件。另一个想法是,您的yii会话提供程序是否设置为使用cookie?它有其他模式!例如,“会话”=>数组“会话名”=>“站点访问”、“cookieMode”=>“仅限”、“保存路径”=>“/path/to/new/directory”,
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'));
    }
# 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