Solved

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

Posted on 2011-09-09
9
167 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
Comment Utility
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 21

Expert Comment

by:Kim Walker
Comment Utility
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:RQuadling
Comment Utility
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
 
LVL 21

Expert Comment

by:Kim Walker
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 21

Accepted Solution

by:
Kim Walker earned 500 total points
Comment Utility
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:RQuadling
Comment Utility
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:RQuadling
Comment Utility
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
Comment Utility
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
Comment Utility
thanks this worked :D can't believe i missed something small like that... :o
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 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

9 Experts available now in Live!

Get 1:1 Help Now