#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    England
    Posts
    471
    Rep Power
    29
    It wont validate the user name and password from the text file, can anyone help please? I think it is the text file corrupt or something?, I done it in notepad.


    auth.php
    ========
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>

    <?php

    // File Name: auth03.php

    // Check to see if $PHP_AUTH_USER already contains info

    if (!isset($PHP_AUTH_USER)) {

    // If empty, send header causing dialog box to appear

    header('WWW-Authenticate: Basic realm="My Private Stuff"');
    header('HTTP/1.0 401 Unauthorized');
    exit;

    } else if (isset($PHP_AUTH_USER)) {

    // If non-empty, open file containing valid user info

    $filename = "/path/to/file.txt";
    $fp = fopen($filename, "r");
    $file_contents = fread($fp, filesize($filename));
    fclose($fp);

    // Place each line in user info file into an array

    $line = explode("n", $file_contents);

    // For as long as $i is <= the size of the $line array,
    // explode each array element into a username and password pair


    $i = 0;

    while($i <= sizeof($line)) {
    $data_pair = explode(":", $line[$i]);

    if (($data_pair[0] == "$PHP_AUTH_USER") && ($data_pair[1] ==
    "$PHP_AUTH_PW")) {
    $auth = 1;
    break;
    } else {
    $auth = 0;
    }
    $i++;
    }

    if ($auth == "1") {

    echo "<P>You're authorized!</p>";
    exit;

    } else {

    header('WWW-Authenticate: Basic realm="My Private Stuff"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Authorization Required.';
    exit;

    }
    }
    ?>
    [/code]


    file.txt:
    =========

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>

    guest:guest
    [/code]


    ------------------
  2. #2
  3. No Profile Picture
    Robert_J_Sherman
    Guest
    Devshed Newbie (0 - 499 posts)
    First, let me say that you only need to check and see if that authuser and password is not set...

    if (!isset()) {
    header();
    header();
    exit;
    } else {
    They're authorized
    }

    In short, you only need to check once per browser session..

    understand the above isn't perfect, it's a quickly thrown up example.




    ------------------
    SnR Graphics,
    Low Cost Hosting and Web Development.
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    Perth West Australia
    Posts
    757
    Rep Power
    15
    Hi muppet - you will have to lever this into your code - Here we fetch the text line by line (taken straight from the manual) as it is more efficient, we break the loop when a match is found.

    <?php
    $fd = fopen("passtest.txt", "r");
    while (!feof($fd)) {
    $buffer = fgets($fd, 4096);
    $split=explode(",",$buffer);
    if($split[0]==$PHP_AUTH_USER && chop($split[1])==$PHP_AUTH_PW){$gotit=1;break;}
    }
    fclose($fd);
    if($gotit==1){echo " username $user and password $pass are valid";}else {echo " username $user and password $pass are not valid";}
    ?>

    I think the problem is solved by the chop() fuction which clears the whitespace from the password - without the chop() - the code was not working all the time - give it a try - I was using a textfile as below.

    1,mary
    2,john
    3,bill
    4,arthur
    5,elsie

    The rest of your code is fine and would Authenticate once only as it is.


    ------------------
    Simon Wheeler
    FirePages -DHTML/PHP/MySQL
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    England
    Posts
    471
    Rep Power
    29
    Thank you for your reply,

    How will the entire script look like?, also how will I place the users passwords on the file.txt?

    eg.


    ,mary:mypassqordhere
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    Perth West Australia
    Posts
    757
    Rep Power
    15
    you should be able to see where this fits in

    } else if (isset($PHP_AUTH_USER)) { // If non-empty, open file containing valid user info ######## FROM HERE
    $auth=0;
    $fd = fopen("passtest.txt", "r");
    while (!feof($fd)) {
    $buffer = fgets($fd, 4096);
    $split=explode(",",$buffer);
    if($split[0]==$PHP_AUTH_USER && chop($split[1])==$PHP_AUTH_PW){$auth=1;break;}
    }
    fclose($fd);//######## TO HERE ####
    if ($auth == "1") {echo "<P>You're
    authorized!</p>"; exit;

    } else {
    header('WWW-Authenticate: Basic realm="My Private Stuff"');

    Mind you I have not tested this yet but it should work fine.

    This assumes you have a password file called passtest.txt (.inc would be better .php3 even more so) if you use .txt extension - give it an obscure name one that is hard to guess

    the text in the file needs to be:

    username,password
    username,password

    etc no space at the begining , just a comma seperator nothing else at the end.

    As for adding entries - do the first one manually - then check out the PHP mannual under fopen() fputs() fread() etc - if you get really stuck post here again.

    ------------------
    Simon Wheeler
    FirePages -DHTML/PHP/MySQL
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    England
    Posts
    471
    Rep Power
    29
    Hello Simon,

    Thank you for the reply, I will take a look at this when I get some time tonight. have a good day.

    ---------

    Darren
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    England
    Posts
    471
    Rep Power
    29
    Hi, this is my new code. it crashes on on line 59, can anyone help?

    Thanks for the time
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>


    <?php
    // File Name: new.php

    // Check to see if $PHP_AUTH_USER already contains info
    if (!isset($PHP_AUTH_USER)) {
    // If empty, send header causing dialog box to appear
    header('WWW-Authenticate: Basic realm="My Private Stuff"');
    header('HTTP/1.0 401 Unauthorized');
    exit;
    } else if (isset($PHP_AUTH_USER)) {
    } else if (isset($PHP_AUTH_USER)) {


    }

    // start

    $auth=0;
    $fd = fopen("http://localhost/passtest.txt", "r");
    while (!feof($fd)) {
    $buffer = fgets($fd, 4096);
    $split=explode(",",$buffer);
    if($split[0]==$PHP_AUTH_USER && chop($split[1])==$PHP_AUTH_PW){$auth=1;break;}
    }
    fclose($fd);

    // end


    // Place each line in user info file into an array
    $split = explode("n", $file_contents);
    // For as long as $i is <= the size of the $line array,
    // explode each array element into a username and password pair

    $i = 0;
    while($i <= sizeof($line)) {
    $data_pair = explode(":", $split[$i]);
    if (($data_pair[0] == "$PHP_AUTH_USER") && ($data_pair[1] ==
    "$PHP_AUTH_PW")) {
    $auth = 1;
    break;
    } else {
    $auth = 0;
    }
    $i++;
    }
    if ($auth == "1") {
    echo "<P>You're authorized!</p>";
    exit;
    } else {
    header('WWW-Authenticate: Basic realm="My Private Stuff"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Authorization Required.';
    exit;
    }
    }
    ?>

    [/code]
  14. #8
  15. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    Perth West Australia
    Posts
    757
    Rep Power
    15
    <?php
    //File Name: auth03.php
    // Check to see if $PHP_AUTH_USER already contains info
    if (!isset($PHP_AUTH_USER)) {// If empty, send header causing dialog box to appear header('WWW-Authenticate: Basic realm="My Private Stuff"'); header('HTTP/1.0 401 Unauthorized'); exit; }
    else if (isset($PHP_AUTH_USER)) {// If non-empty, open file containing valid user info$auth=0;
    $fd = fopen("passtest.txt", "r");
    while (!feof($fd)) {
    $buffer = fgets($fd, 4096);
    $split=explode(",",$buffer);
    if($split[0]==$PHP_AUTH_USER && chop($split[1])==$PHP_AUTH_PW){$auth=1;break;}
    }
    fclose($fd);
    if ($auth == "1") { echo "<P>You're authorized!</p>"; exit; } else { header('WWW-Authenticate: Basic realm="My Private Stuff"'); header('HTTP/1.0 401 Unauthorized'); echo 'Authorization Required.'; exit; } }?>


    ------------------
    Simon Wheeler
    FirePages -DHTML/PHP/MySQL

    [This message has been edited by firepages (edited July 11, 2000).]

Similar Threads

  1. Can user A read user B's session?
    By Stupid Khor in forum PHP Development
    Replies: 9
    Last Post: May 11th, 2004, 06:19 AM
  2. Multiple Cookies for user
    By Chris24 in forum HTML Programming
    Replies: 0
    Last Post: January 22nd, 2004, 10:27 AM
  3. php user levels
    By dynasty in forum PHP Development
    Replies: 1
    Last Post: January 20th, 2004, 03:01 PM
  4. How to get multiple sessions for one user working?
    By jkoerber in forum PHP Development
    Replies: 1
    Last Post: January 16th, 2004, 02:23 PM
  5. Is there a drop-in user control module?
    By Kent Wang in forum PHP Development
    Replies: 3
    Last Post: December 20th, 2003, 06:20 AM

IMN logo majestic logo threadwatch logo seochat tools logo