/    Sign up×
Community /Pin to ProfileBookmark

.htaccess password file issue

Hi Guys,

I’m trying to write to a .htpass file. I find sometimes certain users are missing sometimes even though they were not removed. Anybody know why?
Is there a limit on the array? My code is below.

[code=php]
case ‘ADD_USER’:
$userArray = trim_array(getUserArray($pass_file ));

$userArray[$username] = crypt($password);
writeToFile($pass_file, $userArray );
break;

case ‘DELETE_USER’:
$userArray = trim_array(getUserArray($pass_file ));

unset($userArray[$username]);
writeToFile($pass_file, $userArray );
break;[/code]

[code=php]function getUserArray( $ht_file )
{
$userArray = array();
foreach( file($ht_file) as $line ) {
list($username, $password) = explode( ‘:’, $line, 2 );
$userArray[$username] = $password;
}
return $userArray;
}

function writeToFile( $ht_file, $userArray ) {
$f = fopen($ht_file, “w”) or die( “ERROR: unable to open ” );
flock( $f, LOCK_EX ) or die( “ERROR: unable to lock file ” );
foreach( $userArray as $username => $password ) {
fwrite( $f, “$username:$passwordn”) or die( “ERROR: unable to write” );
}
flock( $f, LOCK_UN ) or die( “ERROR: unable to release file” );
fclose($f);
}[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@Blizzard84authorMar 15.2009 — Anybody have any idea what could cause this issue?
Copy linkTweet thisAlerts:
@GUIRMar 15.2009 — I may be wrong anyway, but here s my suggestion,

unset[key] just clears its value, the element remains there even after unset but with a null value.

So writeTofile may have written some blank lines for those null elements.

If that is the the case try following after unset,

[code=php]$userArray = array_values($userArray);[/code]

Not sure what your function trim_array does ? may be same as above, not sure...
Copy linkTweet thisAlerts:
@Blizzard84authorMar 15.2009 — Hi Guir,

Thanks for the reply. Here's my trim_array.

[code=php]function trim_array($totrim) {
if (is_array($totrim)) {
$totrim = array_map("trim_array", $totrim);
} else {
$totrim = trim($totrim);
}
return $totrim;
}[/code]


I believe using foreach instead of looping through should fix the null issue within writeToFile
Copy linkTweet thisAlerts:
@Blizzard84authorMar 17.2009 — Hi Guys,

Here is the log of the .htpass file when the script is called. There is something going on with the unset. You can tell on the number of rows in the array. I use sizeof($userArray) for the rows after unset.

Function-Username-Password-Rows-File

ADD-p1xqj-grxph-2269-htpass

ADD-8f03-kr9va-2270-htpass

ADD-35rn-0haj-2271-htpass

ADD-ktjp6b-wx6k-2272-htpass

ADD-28yk0-k3eqjr-2273-htpass

ADD-uefj-bbstg-2274-htpass

ADD-ff2vs-em33a-2275-htpass

ADD-7yr8h-beha6-2276-htpass

ADD-3t64-b9a2b7-2277-htpass

ADD-w4h67-m71tn-2278-htpass

ADD-fdempty-2hxu-2279-htpass

ADD-1jq81k-eyv1v-2280-htpass

ADD-2jfesy-ee6ar-2281-htpass

DELETE-s813-fryq7-2279-htpass

DELETE-1521-h761-2279-htpass

DELETE-xputm-cszc-2279-htpass

DELETE-ny90-bhyyr-2279-htpass

DELETE-yrjvvc-ckxat-2279-htpass

DELETE-tbzy1-5yz4-2279-htpass

DELETE-96eq2-ux2e-2279-htpass

DELETE-rxkvh-0uhz-2279-htpass

DELETE-z37zf-fkcb-2277-htpass

DELETE-jt7gw-4v94-2277-htpass

I really need to know what's going on.

This works on my home pc but not on my unix system.

Could php be corrupted?
Copy linkTweet thisAlerts:
@Blizzard84authorMar 24.2009 — Hi guys,

Still having the problems. Works fine with a small amount of users.

When it gets to close to 1000 lines in the .htpass file, weird things happen.

I even change the delete code to get rid of the unset function.

case 'DELETE_USER':

$userArray = trim_array(getUserArray($pass_file ));

//unset($userArray[$username]);
$userArray[$username] = 'terminated';
writeToFile($pass_file, $userArray );
break;[/QUOTE]


It still removes users. I don't get it.... it uses the same code as "ADD" but delete has the problems.

One last thing to mention is that I use curl to call the script to add or delete.

Could that cause a problem?

Thanks
×

Success!

Help @Blizzard84 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...