SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot Osprey's Avatar
    Join Date
    Jan 2006
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Generate .htaccess/.htpasswd

    Hey Everyone,

    I'm trying to generate .htaccess and .htpasswd files to protect directories for users but having problems.

    Heres an example:

    PHP Code:
        $File "../files/username/folder/folder2/.htaccess";
        
    $Handle fopen($File'w');
        
    $Data "AuthName \"Password Protected Directory\"
        AuthType Basic
        AuthUserFile \"../files/username/folder/folder2/.htpasswd\"
        Require valid-user"
    ;
        
    fwrite($Handle$Data);
        
    fclose($Handle);
        
        
    $username $_COOKIE['username'];
        
    $pass leek;
        
    $password crypt($pass'ab');
        
    $File "../files/username/folder/folder2/.htaccess";
        
    $Handle fopen($File'w');
        
    $Data "$username:$password\r\n";
        echo 
    $data;
        
    fwrite($Handle$Data);
        
    fclose($Handle); 
    This creates both files in the correct directory just fine, but I can't login. I'm thinking I've either got something wrong with the encryption or the path to the .htpasswd file? I've been fiddling with this for a while now and no luck. Any help would be much appreciated.

  2. #2
    SitePoint Enthusiast EddM's Avatar
    Join Date
    Oct 2006
    Location
    Reading, UK
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Possibly because the password you're trying to encrypt is being taken as blank.

    You're passing leek, not "leek" if you understand the distinction. The constant leek doesn't exist, so it's just assuming it as blank (unlike most other languages that will cough an error). You need to pass it a string, rather than a reference to a constant that doesn't exist. As you have not enclosed it in quotation marks, it's trying to access a constant called leek rather than just a string "leek".

    It's encrypting "" instead of "leek".

    This is one of things I do not like about PHP. I won't get into it. :P

    ps. Sorry if this is badly explained, I've never been a good teacher.
    Edd Morgan
    Cake Media Studios, Wokingham UK

    Ashes to ashes, zeros and ones.

  3. #3
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  4. #4
    SitePoint Enthusiast EddM's Avatar
    Join Date
    Oct 2006
    Location
    Reading, UK
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by World Wide Weird View Post
    How exactly does this help his problem?
    Edd Morgan
    Cake Media Studios, Wokingham UK

    Ashes to ashes, zeros and ones.

  5. #5
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by EddM View Post
    How exactly does this help his problem?
    About the same way Ubuntu fixes the problem with my WAMP installation?

  6. #6
    SitePoint Enthusiast EddM's Avatar
    Join Date
    Oct 2006
    Location
    Reading, UK
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by World Wide Weird View Post
    About the same way Ubuntu fixes the problem with my WAMP installation?
    WAMP is incompatible with Linux so that sounds about right.
    Edd Morgan
    Cake Media Studios, Wokingham UK

    Ashes to ashes, zeros and ones.

  7. #7
    SitePoint Zealot Osprey's Avatar
    Join Date
    Jan 2006
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Edd but that didn't seem to fix the problem.

    Quote Originally Posted by World Wide Weird View Post
    Thanks also, but I need to do this with PHP so that the users can do this themselves rather than me manually via the admin panel.

  8. #8
    SitePoint Zealot Osprey's Avatar
    Join Date
    Jan 2006
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For future reference (If anyone searches and finds this) you need to use the document root to the .htpasswd file

    Osprey


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •