Link to home
Start Free TrialLog in
Avatar of intellisource
intellisourceFlag for South Africa

asked on

why are arrays being dropped, and individual array elements that need to, not?

hi. i am at my end's wits here, this one is especially for Ray_Paseur and kaufmed to toggle over.
i have used the functions developed in other php/regular expression posts - to retrieve the form and elements as an array, giving info on wether the element is disabled or not.
now the weird thing is, the array gets emptied when i wish to step through it and prepare the POST strings? 0o
the code i am talking about is as follows:
// load form
$url = "https://lmx.leads360.com/web/login.aspx";
$browser = file_get_contents($url);
$forms = getforms($browser);
$form = "form1";
// drop hidden elements and populate form
for ($i = count($forms[$form]) - 1; $i > 0; $i--) {
	if (($forms[$form][$i]["checked"]=="false" && ($forms[$form][$i]["type"] == "checkbox" || $forms[$form][$i]["type"] == "radio")) || $forms[0][$i]["disabled"]=="true") {
			unset($forms[$form][$i]);
	}
	if ($forms[$form][$i]["name"]=="usernameTextBox") {
		$forms[$form][$i]["value"]="user@domain";
	}
	if ($forms[$form][$i]["name"]=="passwordTextBox") {
		$forms[$form][$i]["value"]="P455W0rd";
	}
}
// print_r of the array here prints as expected but it's not removing the items - ergo something is wrong with the if statement around the unset function - but i cannot see what -_-
// print_r($forms);

// compile post strings
$action = $forms[$form][0]["action"];
$url = http_build_url($url,$action,HTTP_URL_JOIN_PATH);
$data = "";
for ($n = 1; $n < count($forms[$form]); $n++) {
	$data .= ($n > 0)?"&":"".$forms[$form][$n]["name"]."=".urlencode($forms[$form][$n]["value"]);
}

// print post data
echo $url."\n".
	$data;

Open in new window

the root question is i guess, what the heck am i doing wrong that it does not return as expected???
Avatar of intellisource
intellisource
Flag of South Africa image

ASKER

ok sorted out the arrays elements being dropped, also the execution of the http_build_url function.
now the only problem is to figure out why the heck it's emptying the array after creating the post string. :(
the code has changed to follows:
// retrieve form
$url = "https://lmx.leads360.com/web/login.aspx";
$browser = file_get_contents($url);
$forms = getforms($browser);
$form = "form1";
// drop hidden elements and populate form
for ($i = count($forms[$form]) - 1; $i > 0; $i--) {
	if (($forms[$form][$i]["checked"]=="false" && ($forms[$form][$i]["type"] == "checkbox" || $forms[$form][$i]["type"] == "radio")) || $forms[$form][$i]["disabled"]=="true") {
			unset($forms[$form][$i]);
	} elseif ($forms[$form][$i]["name"]=="usernameTextBox") {
		$forms[$form][$i]["value"]="pierre@greywacke.co.za";
	} elseif ($forms[$form][$i]["name"]=="passwordTextBox") {
		$forms[$form][$i]["value"]="Wacker82118";
	}
}
// print_r of the array here prints as expected but it's not removing the items 0o
print_r($forms);
// compile post strings
$action = $forms[$form][0]["action"];
$url = http_build_url($url,array($action),HTTP_URL_JOIN_PATH);
$data = "";
for ($n = 1; $n < count($forms[$form]); $n++) {
	if (isset($forms[$form][$n])) {
		$data .= ($n > 0)?"&":"".$forms[$form][$n]["name"]."=".urlencode($forms[$form][$n]["value"]);
	}
}
// print action & post data (post data remains blank :|)
echo $url."\n".
	$data;

Open in new window

Just perusing your code I noticed at the end of line 8 you have $forms[0][$i] where elsewhere on that line you have $forms[$form][$i]. I'm going to look a little deeper, but wondered if that was correct.
Can you either supply the getforms() function or a print_r($forms) before you start processing it please.

At this stage we don't know if the issue lies outside of this code.
The way I read this, your $data should be a string of ampersands "&&&&" since that's all that will be appended to $data if $n>0 which is always is since it starts with 1.
ASKER CERTIFIED SOLUTION
Avatar of Kim Walker
Kim Walker
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try
$data .= (($n > 0) ? "&" : "") . $forms[$form][$n]["name"] . "=" . urlencode($forms[$form][$n]["value"]);

Open in new window


The third expression doesn't terminate at the .

So you're current code looks like ..

if ($n >0) {
 $data .= '&';
} else {
 $data .= '' . $forms[$form][$n]["name"] . "=" . urlencode($forms[$form][$n]["value"]);
}

Open in new window


rather than ...

if ($n >0) {
 $data .= '&';
} else {
 $data .= '';
}
$data .= $forms[$form][$n]["name"] . "=" . urlencode($forms[$form][$n]["value"]);

Open in new window


Maybe it simply easier to ...

if ($n >0) {
 $data .= '&';
}
$data .= $forms[$form][$n]["name"] . "=" . urlencode($forms[$form][$n]["value"]);

Open in new window

the current code is below the returned array, the function getforms behaves as expected and returns the following array from the print_r in line 17:
Array
(
    [form1] => Array
        (
            [0] => Array
                (
                    [type] => form
                    [action] => login.aspx
                )

            [1] => Array
                (
                    [input] => input
                    [type] => hidden
                    [name] => __VIEWSTATE
                    [value] => /wEPDwULLTEwMTAwMDM0ODIPZBYCAgMPZBYCAg0PFgIeC18hSXRlbUNvdW50AgEWAmYPZBYCAgEPFgIeBFRleHQF3gU8ZGl2Pg0KPHA+T24gOC8xOS8wOSB3ZSByZWxlYXNlZCBtaW5vciB1cGRhdGVzIHRvIEV4cHJlc3MuIFBsZWFzZSByZXZpZXcgdGhlIDxhIGhyZWY9Imh0dHA6Ly9sZWFkczM2MC56ZW5kZXNrLmNvbS9mb3J1bXMvMTY0MzYvZW50cmllcy80OTk5OCIgdGFyZ2V0PSJfYmxhbmsiPnJlbGVhc2Ugbm90ZXM8L2E+IGZvciBkZXRhaWxzLiBXZSBhcmUgdmVyeSBpbnRlcmVzdGVkIGluIHlvdXIgZmVlZGJhY2ssIGlmIHlvdSBoYXZlIGFueSBmZWF0dXJlIHJlY29tbWVuZGF0aW9ucywgcGxlYXNlIHBvc3QgdGhlbSBpbiBvdXIgPGEgaHJlZj0iaHR0cDovL2xlYWRzMzYwLnplbmRlc2suY29tL2ZvcnVtcy8xNjQzOS9lbnRyaWVzIiB0YXJnZXQ9Il9ibGFuayI+dXNlciBmb3J1bTwvYT4uPC9wPg0KPHVsPg0KPGxpPldlIG5vdyBvZmZlciA1IHRlbXBsYXRlczogTW9ydGdhZ2UsIERlYnQvTG9hbk1vZCwgSW5zdXJhbmNlIChIb21lL0F1dG8pLCBJbnN1cmFuY2UgKEhlYWx0aC9MaWZlKSwgR2VuZXJpYzwvbGk+DQo8bGk+TmV3IDxhIGhyZWY9Imh0dHBzOi8vbG14LmxlYWRzMzYwLmNvbS9oZWxwIiB0YXJnZXQ9Il9ibGFuayI+aGVscCBmb3J1bXM8L2E+IGFuZCB0aWNrZXRpbmcgc3lzdGVtPC9saT4NCjxsaT5OZXcgZGVkaWNhdGVkIEV4cHJlc3Mgc3VwcG9ydCBwZXJzb24gYW5kIGNoYXQgbm93IGF2YWlsYWJsZSBmb3IgdHJpYWwgYW5kIHBheWluZyBjbGllbnRzPC9saT4NCjwvdWw+DQo8L2Rpdj4NCmRkSh+rfjq3ECYgva0xikqoCQO0DWY=
                    [checked] => false
                    [disabled] => false
                )

            [2] => Array
                (
                    [input] => input
                    [type] => hidden
                    [name] => __EVENTVALIDATION
                    [value] => /wEWBQL7tbDVBgLw0JndDgLi/qahAwKSuuDUCwKCkfPgDOt1y12mZhJ/qB81miBJ4pLiwjLK
                    [checked] => false
                    [disabled] => false
                )

            [3] => Array
                (
                    [input] => input
                    [type] => text
                    [name] => usernameTextBox
                    [value] => pierre@greywacke.co.za
                    [checked] => false
                    [disabled] => false
                )

            [4] => Array
                (
                    [input] => input
                    [type] => password
                    [name] => passwordTextBox
                    [value] => Wacker82118
                    [checked] => false
                    [disabled] => false
                )

        )

)

Open in new window

the array always has the form as the first element in the array.
// retrieve form
$url = "https://lmx.leads360.com/web/login.aspx";
$browser = file_get_contents($url);
$forms = getforms($browser);
$form = "form1";
// drop hidden elements and populate form
for ($i = count($forms[$form]) - 1; $i > 0; $i--) {
	if (($forms[$form][$i]["checked"]=="false" && ($forms[$form][$i]["type"] == "checkbox" || $forms[$form][$i]["type"] == "radio")) || $forms[$form][$i]["disabled"]=="true") {
			unset($forms[$form][$i]);
	} elseif ($forms[$form][$i]["name"]=="usernameTextBox") {
		$forms[$form][$i]["value"]="pierre@greywacke.co.za";
	} elseif ($forms[$form][$i]["name"]=="passwordTextBox") {
		$forms[$form][$i]["value"]="Wacker82118";
	}
}
// print_r of the array here prints as expected 0o
print_r($forms);
// compile post strings
$action = $forms[$form][0]["action"];
$url = http_build_url($url,array($action),HTTP_URL_JOIN_PATH);
$data = "";
// $forms array is cleared out by here - WHY????
for ($n = 1; $n < count($forms[$form]); $n++) {
	if (isset($forms[$form][$n])) {
		$data .= (($n > 0)?"&":"").$forms[$form][$n]["name"]."=".urlencode($forms[$form][$n]["value"];
	}
}
// print action & post data (post data remains blank :|)
echo $url."\n".
	$data;

Open in new window

thanks this worked :D can't believe i missed something small like that... :o