Solved

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

Posted on 2011-09-09
9
171 Views
Last Modified: 2012-05-12
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???
0
Comment
Question by:intellisource
  • 3
  • 3
  • 3
9 Comments
 

Author Comment

by:intellisource
ID: 36511452
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

0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 36511476
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.
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 36511620
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.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 22

Expert Comment

by:Kim Walker
ID: 36511626
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.
0
 
LVL 22

Accepted Solution

by:
Kim Walker earned 500 total points
ID: 36511650
I just recognized (I think) what you're trying to do. Add a couple of parentheses to line 24:

$data .= (($n > 0)?"&":"").$forms[$form][$n]["name"]."=".urlencode($forms[$form][$n]["value"]);

Open in new window

0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 36511672
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


0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 36511690
Maybe it simply easier to ...

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

Open in new window

0
 

Author Comment

by:intellisource
ID: 36516008
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

0
 

Author Closing Comment

by:intellisource
ID: 36516018
thanks this worked :D can't believe i missed something small like that... :o
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
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.

773 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