Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7159463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:14:08+00:00 2026-05-28T13:14:08+00:00

I have this code for logging into Google using Simple DOM Parser with curl.

  • 0

I have this code for logging into Google using Simple DOM Parser with curl. I’ve tried adding in the cookiejar file, but to no avail. I keep getting the message:

Your browser’s cookie functionality is turned off. Please turn it on.

Any idea on how to solve this?

Here’s my code for reference:

$html = file_get_html('https://accounts.google.com/ServiceLogin?hl=en&service=alerts&continue=http://www.google.com/alerts/manage');

//... some code for getting post data here

$curl_connection = curl_init('https://accounts.google.com/ServiceLoginAuth');
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_connection, CURLOPT_COOKIEJAR, COOKIEJAR);
curl_setopt($curl_connection, CURLOPT_COOKIEFILE, COOKIEJAR);
curl_setopt($curl_connection, CURLOPT_HEADER, true);  
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($curl_connection, CURLOPT_TIMEOUT, 120);
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

$result = curl_exec($curl_connection);
curl_close($curl_connection);

echo $result;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:14:09+00:00Added an answer on May 28, 2026 at 1:14 pm

    Here is some modified code that works.

    It first requests the login page to get the initial cookies and extract the required values for the login form. Next it performs a post to the login service. It then checks to see if it is trying to use javascript and meta tags to redirect to the destination URL.

    It seemed like you already have code for grabbing the form fields, so I didn’t post mine, but if you need it let me know. Just make sure $formFields is an associative array with keys being the field name, and the value being the field value.

    <?php
    
    /**
     * Log in to Google account and go to account page
     *
     */
    
    $USERNAME = 'youraccount@gmail.com';
    $PASSWORD = 'password';
    $COOKIEFILE = 'cookies.txt';
    
    // initialize curl handle used for all requests
    $ch = curl_init();
    
    // set some options on the handle
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $COOKIEFILE);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $COOKIEFILE);
    curl_setopt($ch, CURLOPT_HEADER, 0);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
    curl_setopt($ch, CURLOPT_TIMEOUT, 120);
    
    // url of our first request fetches the account login page
    curl_setopt($ch, CURLOPT_URL, 
      'https://accounts.google.com/ServiceLogin?hl=en&service=alerts&continue=http://www.google.com/alerts/manage');
    $data = curl_exec($ch);
    
    // extract form fields from account login page
    $formFields = getFormFields($data);
    
    // inject email and password into form
    $formFields['Email']  = $USERNAME;
    $formFields['Passwd'] = $PASSWORD;
    unset($formFields['PersistentCookie']);
    
    $post_string = http_build_query($formFields); // build urlencoded POST string for login
    
    // set url to login page as a POST request
    curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    
    // execute login request
    $result = curl_exec($ch);
    
    // check for "Redirecting" message in title to indicate success
    // based on your language - you may need to change this to match some other string
    if (strpos($result, '<title>Redirecting') === false) {
        die("Login failed");
        var_dump($result);
    }
    
    // login likely succeeded - request account page; unset POST so we do a regular GET
    curl_setopt($ch, CURLOPT_URL, 'https://myaccount.google.com/?utm_source=OGB');
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_POSTFIELDS, null);
    
    // execute request for login page using our cookies
    $result = curl_exec($ch);
    
    echo $result;
    
    
    // helpef functions below
    
    // find google "#gaia_loginform" for logging in
    function getFormFields($data)
    {
        if (preg_match('/(<form.*?id=.?gaia_loginform.*?<\/form>)/is', $data, $matches)) {
            $inputs = getInputs($matches[1]);
    
            return $inputs;
        } else {
            die('didnt find login form');
        }
    }
    
    // extract all <input fields from a form
    function getInputs($form)
    {
        $inputs = array();
    
        $elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);
    
        if ($elements > 0) {
            for($i = 0; $i < $elements; $i++) {
                $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);
    
                if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
                    $name  = $name[1];
                    $value = '';
    
                    if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
                        $value = $value[1];
                    }
    
                    $inputs[$name] = $value;
                }
            }
        }
    
        return $inputs;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code that performs an ajax call and loads the results into
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
I have this code: myVariable which I want to change into trace(myVariable: + myVariable);
I have this code :- using (System.Security.Cryptography.SHA256 sha2 = new System.Security.Cryptography.SHA256Managed()) { .. }
I have this code: cursor = conn.cursor() cursor.execute((insert into new_files (videos_id, filename, is_processing) values
I have this simple code that needs to get a chunk of a large
I have written a simple log4net wrapper. I was wondering whether this wrapper code
I have this code in jQuery, that I want to reimplement with the prototype
I have this code: chars = #some list try: indx = chars.index(chars) except ValueError:
I have this code: CCalcArchive::CCalcArchive() : m_calcMap() { } m_calcMap is defined as this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.