Solved

Get hidden fields from Form using JavaScript, Jquery or PHP

Posted on 2010-08-29
5
880 Views
Last Modified: 2012-05-10
Hi
I am trying to auto login to Google Analytics,  I use the form below which works fine if I have the correct hidden fields, but apparently Google now changes them on a daily basis

https://www.google.com/accounts/ServiceLoginBoxAuth
<input type="hidden" name="GA3T" value="4oCGYxIWWGUE">
<input type="hidden" name="GALX" value="g2BycIQxWzI">

Is there a way to automatically go to https://www.google.com/accounts/ServiceLoginBoxAuth get the hidden fields and substitute in my script, can this be done perhaps using JavaScript, jquery or PHP?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<a href="javascript:gi('googleanalyticslogin').submit();">Sign in to mywebsite.com Analytics Account</a>The invisible form
<form id="googleanalyticslogin" action="https://www.google.com/accounts/ServiceLoginBoxAuth" method="post">
<input type="text" name="Email" class="gaia le val" id="Email" size="18" value="admin@ mywebsite.com" />
<input type="password" name="Passwd" class="gaia le val" id="Passwd" size="18" value="mypassword" />
<input type="checkbox" name="PersistentCookie" value="yes" />
<input type="hidden" name="rmShown" value="1" />
<input type="hidden" name="continue" value="http://www.google.com/analytics/home/?et=reset&amp;hl=en-US" />
<input type="hidden" name="service" value="analytics" />
<input type="hidden" name="nui" value="1" />
<input type="hidden" name="hl" value="en-US" />
<input type="hidden" name="GA3T" value="4oCGYxIWWGUE">
<input type="hidden" name="GALX" value="g2BycIQxWzI">
<input type="submit" />
</form>
</body>
</html>

Thanks in advance for you feedback.

0
Comment
Question by:sabecs
5 Comments
 
LVL 14

Expert Comment

by:sam2912
Comment Utility
There is no way to get this done with JavaScript (jQuery is a JavaScript library). Might be possible to do with PHP and parse the page to get the values, but then Google might decide to change their layout which could break it then.
0
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
you can fetch all the hidden fields like this

var elements = document.getElementByTagName("input");

for (var counter = 0;
(counter < elements.length) && (elements[counter].type == "hidden");
counter++ )
{
   var hiddenelement = elements[counter];
}
0
 
LVL 9

Expert Comment

by:Shahzad Fateh Ali
Comment Utility
Here is total jquery based solution.

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<title>Testing</title>
		
		<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
		<script type="text/javascript">
// your html response from ajax call to google (form html)
                    var html  = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head><body><a href="javascript:gi(\'googleanalyticslogin\').submit();">Sign in to mywebsite.com Analytics Account</a>The invisible form<form id="googleanalyticslogin" action="https://www.google.com/accounts/ServiceLoginBoxAuth" method="post"><input type="text" name="Email" class="gaia le val" id="Email" size="18" value="admin@ mywebsite.com" /><input type="password" name="Passwd" class="gaia le val" id="Passwd" size="18" value="mypassword" /><input type="checkbox" name="PersistentCookie" value="yes" /><input type="hidden" name="rmShown" value="1" /><input type="hidden" name="continue" value="http://www.google.com/analytics/home/?et=reset&amp;hl=en-US" /><input type="hidden" name="service" value="analytics" /><input type="hidden" name="nui" value="1" /><input type="hidden" name="hl" value="en-US" /><input type="hidden" name="GA3T" value="4oCGYxIWWGUE"><input type="hidden" name="GALX" value="g2BycIQxWzI"><input type="submit" /></form></body></html>';
			$(function(){
				html = $(html);
                                html.find('input').each(function(){
// parse html and get names of all input fields.
alert($(this).attr('name'));
                                });
				
			});
		</script>

	</head>
	<body>
	</body>
</html>

Open in new window

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
What you might be looking for is a CURL script that will extract the form values from the web page and post the login.  Google uses a "form token" so you must perform this extraction each time you want an automated login.

I do not recommend using this technique.  Their terms of service may prohibit this technique.  

You should ask Google for a formal interface like an API.  Each CURL login script is a separate research project, and any changes by Google may render your code useless without warning.  OTOH, they have been very respectful of versioned API interfaces.

Best of luck with your project, ~Ray
<?php // RAY_temp_curl_login.php
error_reporting(E_ALL);
echo "<pre>\n";

// THE REPLACEMENTS (CASE SENSITIVE) ARE THE LOGIN CREDENTIALS FOR THE SITE
$replacements["Email"]  = 'Your Email';
$replacements["Passwd"] = 'Your PWord';

// READ THE FORUM PAGE WITH THE LOGIN FORM
$baseurl = 'https://www.google.com/accounts/ServiceLoginBoxAuth';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $baseurl);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR,  'cookie.txt');
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);

// CALL THE WEB PAGE
$htm = curl_exec($ch);
$err = curl_errno($ch);
$inf = curl_getinfo($ch);
if ($htm === FALSE)
{
    echo "\nCURL GET FAIL: $baseurl CURL_ERRNO=$err ";
    var_dump($inf);
    die();
}


// REMOVE THE END-OF-LINE CHARACTERS
$htm = str_replace(PHP_EOL, "", $htm);

// ISOLATE THE FORM
$form   = explode("<form",$htm);
$form   = explode("</form>",$form[1]);
$inputs = explode("<input",$form[0]);
$post   = "";

foreach($inputs as $key => $val)
{
    // IDENTIFY THE ACTION SCRIPT
    $action = strpos($val, "action");
    if($action !== false)
    {
        // EXTRACT THE ACTION SCRIPT NAME FROM THE FORM INPUT (MAY NOT BE NEEDED)
        $actstart = strpos($val, "\"", $action+1);
        $actend   = strpos($val, "\"", $actstart+1);
        $posturl  = substr($val, $actstart+1, ($actend-$actstart-1));
        continue;
    }

    // IDENTIFY THE INPUT FIELDS BY NAME AND VALUE PAIRS
    $name = strpos($val, "name");
    if($name !== false)
    {
        // EXTRACT THE NAME FROM THE FORM INPUT
        $namestart = strpos($val, "\"", $name+1);
        $nameend   = strpos($val, "\"", $namestart+1);
        $strname   = substr($val, $namestart+1, ($nameend-$namestart-1));

        // EXTRACT THE VALUE
        $strvalue = '';
        $value = strpos($val, "value");
        if($value !== false)
        {
            $valuestart = strpos($val, "\"", $value+1);
            $valueend   = strpos($val, "\"", $valuestart+1);
            $strvalue   = substr($val, $valuestart+1, ($valueend-$valuestart-1));
        }

        // IF NO VALUE TRY TO REPLACE
        if (empty($strvalue))
        {
            foreach ($replacements as $k => $v)
            {
                if ($k == $strname) $strvalue = $v;
            }
        }
        $post .= "&" . $strname . "=" . $strvalue;
    }
}

// DECLOP LEFTMOST AMPERSAND
$post=substr($post,1);

// SET THE LOGIN URL FROM THE ACTION SCRIPT - NOT NEEDED FOR GOOGLE
// $posturl = $baseurl . '/' . $posturl;
$posturl = $baseurl;

// NOW POST THE DATA WE HAVE FILLED IN
curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

// ACTIVATE THIS TO SEE THE RAW POST DATA
// var_dump($post);

// WAIT A RESPECTABLE PERIOD OF TIME
sleep(1);

// CALL THE WEB PAGE
$xyz = curl_exec($ch);
$err = curl_errno($ch);
$inf = curl_getinfo($ch);
if ($xyz === FALSE)
{
    echo "\nCURL POST FAIL: $posturl CURL_ERRNO=$err ";
    var_dump($inf);
    die();
}

// NOW ON TO THE NEXT PAGE
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/accounts/ManageAccount');
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');

$xyz = curl_exec($ch);
$err = curl_errno($ch);
$inf = curl_getinfo($ch);
if ($xyz === FALSE)
{
    echo "\nCURL 2ND GET FAIL: $posturl CURL_ERRNO=$err ";
    var_dump($inf);
    die();
}

// SHOW OFF THE DATA AFTER THE LOGIN
echo htmlentities($xyz);

Open in new window

0
 

Author Closing Comment

by:sabecs
Comment Utility
Thanks Ray, your correct, I should find out from Google the best way to go about this.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now