Solved

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

Posted on 2011-09-09
9
172 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

821 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