Page 1 of 1

foreach construct problems

Posted: Tue Aug 24, 2004 3:02 am
by kholloi
feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color] 


HI,

I have a file called /tmp/usrlist containing a list of valid users. I am writing an admin interface to the server and one of the functionalities is to change users password. Now information is entered into a form and passed on to script changepasswd.php which checks tah it is not root, compares the username with /tmp/usrlist and changes the password. Well that is what is suposed to happen anyway. Instead it seems like the foreach construct isn't doing it's job. 

I added a var_dump and the array is filled correctly but the comparrison doesn't seem to work:
=====================================================================================

Code: Select all

<?php

include('header.php');

//create short variable names

$user = $HTTP_POST_VARS['user'];
$pw =  $HTTP_POST_VARS['pw'];
$pwcf =  $HTTP_POST_VARS['pwcf'];





//check if the passwords match, user is not root, is a valid system user, and execute that change password script
if ($user == "root")
{
echo("You are not authorised to change Root's password");
}

$users= file("/tmp/usrlist");


echo '<pre>'; 
echo var_dump($users); 
echo '</pre>';


if ($pw == $pwcf)
{
$fp = fopen("/tmp/chpasswd", 'w');
$output = $user.":".$pw;

$match = "no";

foreach ($users AS $validName) {
if($user == $validName) { $match = "yes"; }
}

if ($match == "yes") {

fwrite($fp, $output );
exec(escapeshellcmd("./chpasswd.sh" ));
}


 else {
echo("$user is not a valid user on this system");
}


}
else
{
echo("The passwords do not match, please try again");
}
=====================================================================================

When I run this script, it returns the "$user is not a valid user on this sytem" error.

Here is the output of the script after trying to change my own passwd:

=====================================================================================

Code: Select all

array(3) &#123;
  &#1111;0]=&gt;
  string(7) "nobody
"
  &#1111;1]=&gt;
  string(4) "jls
"
  &#1111;2]=&gt;
  string(8) "juanita
"
&#125;
jls is not a valid user on this system
=====================================================================================


What am I doing wrong???

PLease help this clueless newbie.

Thanks


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Tue Aug 24, 2004 3:11 am
by feyd
if($user == trim($validName))

Posted: Tue Aug 24, 2004 3:30 am
by kholloi
thanks feyd. Sorry bout the code thing. I will have to read up on what trim does.

Posted: Tue Aug 24, 2004 3:35 am
by John Cartwright
[php_man]trim[/php_man]