Solved

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

Posted on 2011-09-09
9
170 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:RQuadling
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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:RQuadling
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:RQuadling
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP/Magento not logging 2 35
Echo images using file system 2 31
Load google maps api into wordpress 5 37
using php variable inside javascript 5 14
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
The viewer will learn how to count occurrences of each item in an array.
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 …

910 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

16 Experts available now in Live!

Get 1:1 Help Now