Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 954
  • Last Modified:

Get hidden fields from Form using JavaScript, Jquery or PHP

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
sabecs
Asked:
sabecs
1 Solution
 
sam2912Commented:
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
 
Gurvinder Pal SinghCommented:
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
 
Shahzad Fateh AliWeb Solutions Architect - VentureDive (Pvt) LtdCommented:
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
 
Ray PaseurCommented:
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
 
sabecsAuthor Commented:
Thanks Ray, your correct, I should find out from Google the best way to go about this.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now