foreach construct problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kholloi
Forum Newbie
Posts: 19
Joined: Tue Mar 30, 2004 1:08 am

foreach construct problems

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if($user == trim($validName))
kholloi
Forum Newbie
Posts: 19
Joined: Tue Mar 30, 2004 1:08 am

Post by kholloi »

thanks feyd. Sorry bout the code thing. I will have to read up on what trim does.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

[php_man]trim[/php_man]
Post Reply