Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get hidden fields from Form using JavaScript, Jquery or PHP

Posted on 2010-08-29
5
Medium Priority
?
944 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&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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 14

Expert Comment

by:sam2912
ID: 33555438
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:Gurvinder Pal Singh
ID: 33555867
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
ID: 33556931
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 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 33558558
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
ID: 33575000
Thanks Ray, your correct, I should find out from Google the best way to go about this.
0

Featured Post

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

721 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