How can i login with curl to a website

657 Views Asked by At

I have read a lot of articles and SO questions about this but i am not successful to do this . here is some reference refer1 refer2 What i want is to login into a website through curl . What i did is given below. Its on local system

url for this is http://192.168.1.31/Eb/user/login/loginnew

public function actionLoginnew()
    {
        $fields = array(
                        'User'=>array(
                        'email' => '[email protected]',
                        '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;
    }

this is my yii function and the website i want to login into is in cake php .

it is showing me that the login done . But when i hit the url http://192.168.1.31/Eb/projectmanager/ it again redirects me to login page .

login script on the 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'));
    }

the cookie file is generate and the content is

# 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

please help me to solve this .Thanks

1

There are 1 best solutions below

6
On

Login script you have to write in http://192.168.1.31/Eb/projectmanager/login

match with passed credentials and set session on success.