Automatic Login using Curl?

Discussion in 'PHP' started by mokimofiki, Sep 13, 2012.

  1. #1
    I am trying to create a script to automatically login to a website using curl and am really racking my brain. Any help would be appreciated.

    curl.php
    <?php
    class Curl {
    
        public $cookieJar = "";
    
        // Make sure the cookies.txt file is read/write permissions
        public function __construct($cookieJarFile = '/var/www/html/cookies.txt') {
            $this->cookieJar = $cookieJarFile;
        }
    
        function setup() {
            $header = array();
            $header[0]  = "Accept: text/xml,application/xml,application/xhtml+xml,";
            $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
            $header[]   = "Cache-Control: max-age=0";
            $header[]   = "Connection: keep-alive";
            $header[]   = "Keep-Alive: 300";
            $header[]   = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
            $header[]   = "Accept-Language: en-us,en;q=0.5";
            $header[]   = "Pragma: "; // browsers keep this blank.
    
            curl_setopt($this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7');
            curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header);
            curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookieJar);
            curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookieJar);
            curl_setopt($this->curl, CURLOPT_AUTOREFERER, true);
            curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
        }
    
        function get($url) {
            $this->curl = curl_init($url);
            $this->setup();
    
            return $this->request();
        }
    
        function getAll($reg, $str) {
            preg_match_all($reg, $str, $matches);
            return $matches[1];
        }
    
        function postForm($url, $fields, $referer = '') {
            $this->curl = curl_init($url);
            $this->setup();
            curl_setopt($this->curl, CURLOPT_URL, $url);
            curl_setopt($this->curl, CURLOPT_POST, 1);
            curl_setopt($this->curl, CURLOPT_REFERER, $referer);
            curl_setopt($this->curl, CURLOPT_POSTFIELDS, $fields);
            return $this->request();
        }
    
        function getInfo($info) {
            $info = ($info == 'lasturl') ? curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL) : curl_getinfo($this->curl, $info);
            return $info;
        }
    
        function request() {
            return curl_exec($this->curl);
        }
    }
    ?>
    Code (markup):
    login.php
    <?php
    include('curl.php');
    $curl = new Curl();
    
    $url = "https://myvi.net/login/?ReturnUrl=http%3a%2f%2fmyvi.net%2f";
    $fields = "Username=xxxxxx&Password=xxxxxx";
    
    // Calling URL
    $referer = "http://myvi.net";
    
    $html = $curl->postForm($url, $fields, $referer);
    
    echo $html;
    ?>
    Code (markup):
    I of course replaced the actual username and password with "xxxxxx".

    Thank you in advance
     
    mokimofiki, Sep 13, 2012 IP
  2. Web Solutions

    Web Solutions Peon

    Messages:
    64
    Likes Received:
    1
    Best Answers:
    5
    Trophy Points:
    0
    #2
    What is the problem ?
     
    Web Solutions, Sep 13, 2012 IP
  3. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #3
    It just goes to a blank page and I cannot figure out where I am going wrong. I'm hoping that someone here can see an error in my code.
     
    mokimofiki, Sep 14, 2012 IP
  4. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #4
    If you can't take 5 minutes to properly explain the problem there is no reason why anyone should spend 30 seconds offering you help.
     
    NetStar, Sep 14, 2012 IP
  5. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #5
    The problem is that it looks like it works but the page it gets to just shows a blank page. I have all the code there and I don't see an issue with the code just the result. If I could give more detail on my problem then I wouldn't have one.

    Anyone with actual input would be appreciated and those that want to answer a multiple choice question should just move on.
     
    mokimofiki, Sep 17, 2012 IP
  6. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    any debugging have you tried? first things i would do is put error_reporting(E_ALL) at the top of the script. Then var_dump($html) instead of echoing it. If that still yields nothing, in the postForm() function try replacing return $this->request(); with var_dump(curl_exec($this->curl));
     
    szalinski, Sep 20, 2012 IP