More .htaccess goodness

Johnathan,

Thank you for your help with the .htaccess and Perl question I had. What I ended up doing was downloading a script that allowed users to input their own usernames and passwords.

I have another question. The server requires a .htgroup file in order to work, and the script I downloaded does not put usernames into the .htgroup file. I wrote some code to do this, but unfortunately it inserts a space at the end of the .htgroup file and will not allow users to log in unless I delete that space. I tried just appending to the end of the file, but it appends on a new line, and of course I need it to append to the end of the existing line in .htgroup.

Could someone please look at the code I have and let me know if they have any suggestions? Thank you very much!

[code]
#!/usr/bin/perl

$gcounter=1;

#First read the file .htgroup in as a Perl hash
open(group, "<.htgroup") or die "Can't open group file: $!");
@groupusers=<;group>;
close(group);

#Next, remove the spaces between the usernames and store in a new hash
foreach $groupusers(@groupusers) {
@groupsplit=split(" ", $groupusers);
}

#Add the new username to the end of the hash and increase the length by one
$grouplength=@groupsplit;
$groupsplit[$grouplength]=$input{'username'};
$grouplength=$grouplength+1;

#Open the .htgroup file and write "readers:" to it
open(groupwrite, ">.htgroup") or die "Can't write to group file: $!";
print groupwrite $groupsplit[0];

#For each element, print a space then the username in the .htgroup file
#Also increment the counter
while($gcounter<$grouplength) {
print groupwrite " ", $groupsplit[$gcounter];
$gcounter=$gcounter+1;
}
close(groupwrite);
[/code]

Comments

  • Hi,

    : Thank you for your help with the .htaccess and Perl question I had.
    You're welcome.

    : What I ended up doing was downloading a script that allowed users to
    : input their own usernames and passwords.
    :
    : I have another question. The server requires a .htgroup file in
    : order to work, and the script I downloaded does not put usernames
    : into the .htgroup file. I wrote some code to do this, but
    : unfortunately it inserts a space at the end of the .htgroup file and
    : will not allow users to log in unless I delete that space. I tried
    : just appending to the end of the file, but it appends on a new line,
    : and of course I need it to append to the end of the existing line
    : in .htgroup.
    :
    : Could someone please look at the code I have and let me know if they
    : have any suggestions? Thank you very much!
    Hmmm....the code seems to be doing a few weird things, like reading in a whole file and then in the first loop only splitting the last line into @groupsplit. If you want the last line you can just write $groupsplit[@groupsplit - 1].

    Please can you post an example of a .htgroup file and show what you want to do exactly, then I can work out the most efficient way of doing it.

    Thanks,

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • : Hmmm....the code seems to be doing a few weird things, like reading in a whole file and then in the first loop only splitting the last line into @groupsplit. If you want the last line you can just write $groupsplit[@groupsplit - 1].

    Yes, it is supposed to read in the file (which is a single line), break up the file into a hash, add an element to the end, then print the hash back into the .htgroup file with spaces between the elements. I could not just append to the end of the file because it would place the username on a new line. I need all elements to be on the same line after adding a username.

    : Please can you post an example of a .htgroup file and show what you want to do exactly, then I can work out the most efficient way of doing it.

    Here is an example of the .htgroup file:

    [code]
    groupname: user1 user2 user3
    [/code]

    Simple. :^)
  • : Yes, it is supposed to read in the file (which is a single line),
    : break up the file into a hash, add an element to the end, then print
    : the hash back into the .htgroup file with spaces between the
    : elements. I could not just append to the end of the file because it
    : would place the username on a new line. I need all elements to be
    : on the same line after adding a username.
    @array = ; grabs the entire file, and your for loop would iterate through each element in the array, which is essentially each line in the file. That's what confused me - it seemed a bit pointless! If you know there will only be one line why not just read it into a scalar?
    $line = ;

    : Here is an example of the .htgroup file:
    :
    [code]
    : groupname: user1 user2 user3
    [/code]
    :
    : Simple. :^)
    Indeed. If you know there will only ever be one line in the file, they you could do this:-

    [code]#Read the line from the file. I'm assuming only one line.
    open(GROUP, "<.htgroup") or die "Can't open group file: $!");
    my $groupusers = <GROUP>;
    close(GROUP);

    #Add username onto the end (with a space before it).
    $groupusers .= " $input{'username'}";

    #We should have no
    on the end. You may find your problem is that
    #$input{'username'} had a
    on the end! You can always safely do this:-
    chomp $groupusers;

    #Write the file back.
    open(GROUP, ">.htgroup") or die "Can't open group file: $!");
    print GROUP $groupusers;
    close(GROUP);
    [/code]

    In case you have a file with many groups, it might look something like this:-

    [code]#Read the whole file.
    open(GROUP, "<.htgroup") or die "Can't open group file: $!");
    my @groups = <GROUP>;
    close(GROUP);

    #Add username onto the end (with a space before it).
    my $group;
    foreach $group (@groups) {
    if ($group =~ /^$input{'groupname'}: /) {
    $group .= " $input{'username'}";
    }
    }

    #Write the file back.
    open(GROUP, ">.htgroup") or die "Can't open group file: $!");
    print GROUP $_ for (@groups);
    close(GROUP);
    [/code]

    Untested and may not work perfect without tweaks, but hopefully you get the gist. And if you don't, at least it isn't as arcane as my sig. :-)

    Hope this is what you want,

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories