php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38970 weird skip so that $array[0] is missed
Submitted: 2006-09-27 03:00 UTC Modified: 2006-09-27 09:32 UTC
From: jlindenbaum at gawsolutions dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.4.4 OS: Cent OS 4.3
Private report: No CVE-ID: None
 [2006-09-27 03:00 UTC] jlindenbaum at gawsolutions dot com
Description:
------------
Reading files from a directory, stripping "." and "..". Sorting array by values (using sort()). foreach() is used to output a checkbox for each file (cPanel user file).

When user is checked chosen action (from the radio buttons) is to be applied (with system command) by executing the command in action with the appropriate username trailing.

foreach() is used to go through every $_POST value with an if() the $keys "action" and "submit" are excluded from execution. A bash command is put together and finally executed.

Everything works as it should, except that the array[0] is totally ignored.

Reproduce code:
---------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>User actions</title>
</head>

<body>
<form name="userlist" action="test.php" method="post"> 
	<input type="radio" name="action" value="/scripts/pkgacct" checked="checked" />Backup
	<input type="radio" name="action" value="/scripts/suspendacct" />Suspend
	<input type="radio" name="action" value="/scripts/unsuspendacct" />Unsuspend
	<input type="radio" name="action" value="/scripts/killacct" />Delete
	<br />
<?php 
$handle     = opendir("/var/cpanel/users/");

$usrarr     = array(); // Array full of users
while( FALSE !== ($users = readdir($handle)) ) {
	if( $users != "." && $users != ".." ) { 
		$usrarr[]     = $users; 
	}
}

sort($usrarr); // sort alphabetically

foreach($usrarr as $key => $value) {
	echo	"\t<input type=\"checkbox\" name=\"".$key."\" value=\"".$value."\" /> ".$value."<br />\n";
}

closedir($handle);
?>
<input name="submit" type="submit" id="submit" value="GO">
</form>
<?php
if( isset($_POST['submit']) ) {
echo "<hr noshade=\"noshade\" />"; // output a horizontal rule for clearance
	echo "<pre>";
	foreach( $_POST as $key => $value ) {
		if( $key != "submit" && $key != "action" ) {
			$bash	= $_POST['action']." ".$value; // put together /path/command username
			echo "Processing: ".$value."<br />\n";
			system($bash);
			echo "Done<br />\n";
		}
	}
	echo "<pre>";
}
?>
</body>
</html>


Expected result:
----------------
Expected result is that every $_POST key (0 through n) should be put together with the $_POST['action'] command and a username, executed and on to the next.

Actual result:
--------------
It works for every user, BUT the $usrarr[0] key and value.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-09-27 03:05 UTC] jlindenbaum at gawsolutions dot com
Sorry entered wrong OS version. Changed from 4.2 to 4.3
 [2006-09-27 03:07 UTC] jlindenbaum at gawsolutions dot com
Sorry, another update. We just found the weirdest work around.

the echo the checkbox:
echo     "\t<input type=\"checkbox\" name=\"1".$key."\" value=\"".$value."\" /> ".$value

if we add the one before the $key in nam="" so that the $_POST array starts at 10 it works fine.
 [2006-09-27 07:07 UTC] derick@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.
 [2006-09-27 09:32 UTC] mgf@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

This sounds like an auto-type-conversion issue -- you need to use identity tests instead of equality.  (Further Hint: by definition, "submit"==0, but "submit"!==0).
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 19 00:00:03 2025 UTC