Jump to content

PHP CURL Cookie problem..


pmpysz

Recommended Posts

Hi, I have a script that works on my godaddy shared hosting account, but when I moved it to their new gird hosting, it no longer works. I did not write the script, but from what I understand, it's not making the cookies. I don't have a clue why this script would be working on one server and not another. Please let me know if you know how to fix it or point me in a good direction of where to learn how? I've been googling everything I can think of since last night, so any help would be greatly appreciated. Thanks.

 

//enable debug
    define('DEBUG', true);

    //check for curl supporting
    if (!function_exists('curl_init')){
        echo "Curl is not supported";
        exit;
    }


    /**
     *
     * Dump variable for debugging
     *
     * @param   var   variable to dump
     *
     */
    function debug($var){
        echo @DEBUG ? '<strong>>> </strong>' . (is_string($var) ? htmlentities($var) :
                str_replace("\n", '<br>', htmlentities(var_export($var, true)))) . '<br>' : '';
    }

    /**
     *
     * Log in to example.com
     *
     * @param   email   email to log in
     * @param   pass    password to log in
     * @param   error   return error message
     * @return  cookie file with session data on success, FALSE on error
     *
     */
    function login($email, $pass, &$error = null) {
        $error = "Unknown error";
        //get unique cookie file
        $cookie_file = '/tmp/cookiejar-' .
                sprintf( "%u", crc32( $email.$pass)).rand(0, 9999999);

        //initialize curl session
        $ch = curl_init('https://secure.example.com/index.cfm?fuseaction=login.process');
        //set cookie file
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
        //enable certificate verifying
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        //set user agent
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla 4.01');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLINFO_HEADER_OUT, 1);

        //set curl header
        curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
                'Host: secure.myspace.com',
                'Content-Type: application/x-www-form-urlencoded',
                'Accept-Language: en'
        ));

        //set curl post data (login, password) 
        curl_setopt( $ch, CURLOPT_POST, 1 );
        curl_setopt($ch, CURLOPT_POSTFIELDS,
                "ctl00%24ctl00%24cpMain%24cpMain%24LoginBox%24Email_Textbox=" . urlencode($email) . "&" .
                "ctl00%24ctl00%24cpMain%24cpMain%24LoginBox%24Password_Textbox=" . urlencode($pass) . "&d1lb=");

        //execute curl session
        $content = curl_exec($ch);

        //check for curl error
        if($curl_error = curl_error($ch)){
            $error = "Curl: ($curl_error)";
        //check for authorization error
        }elseif(preg_match('/SplashStatus=Invalid/', $content)){
            $error = 'Invalid email or password'; 
        //login success
        }elseif(preg_match('/EXSession=/', $content)){
            $error = false;
        }

        //close curl session
        @curl_close($ch);

        //return cookie file with session data on success or FALSE on error
        return $error ? false : $cookie_file;
    }

Link to comment
https://forums.phpfreaks.com/topic/181149-php-curl-cookie-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.