Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,666 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,147 people online right now. Registration is fast and FREE... Join Now!




Problem with basic authentication

 
Reply to this topicStart new topic

Problem with basic authentication

stu26
post 20 Jul, 2007 - 01:25 PM
Post #1


New D.I.C Head

*
Joined: 18 Jul, 2007
Posts: 8


My Contributions


I know this isn't very secure but it doesn't need to be for my application, but I am storing usernames, passwords, and the users authorized cameras in a txt file and then when a user logs in searching the txt file to determine if there is a match, retrieving the information if it is found and displaying an error message if it is not.

Each user entry is stored in this format:
user:password
cam0,cam1,cam2,cam3

The problem I am running into is that if I manually edit the txt file it will find the user and authenticate no problem, but if I use the add user code it adds the user information to the txt file correctly, looks just as if I had typed it in manually, but it will not authenticate unless I go back in and manually retype that users information. I'm thinking maybe it's whitespace somewhere but I am using trim to try to eliminate that. When debugging the $entry array contains all the correct information but the $key variable has a null value.

I would greatly appreciate any help anyone could offer. Thanks!

Here is my code:

Authentication Page:
CODE

    session_start();

    //Retrieve user entry
    $userid = trim($_POST['userid']);
    $password = trim($_POST['password']);
    
    $authentication = $userid.':'.$password;
    $filename = "fields.txt";
    $handle    = fopen($filename, "rb");
    $contents = fread($handle, filesize($filename));
    $entry = explode("\n", $contents);
    
    //Authenticate login against fields.txt    
    $key = array_search($authentication, $entry);
        if (is_numeric($key)==false){
            //Debug
                                                echo $entry[0];
                                                echo $entry[1];
                                                echo $entry[2];
                                                echo $entry[3];
            echo $key;
            echo '<br>';
            echo $authentication;
            echo '<br>';
            echo $userid;
            echo '<br>';
            echo $password;    




Add User Page:
CODE

session_start();

    //Define Variables
    $filename = 'fields.txt';
    $fp = fopen($filename, 'a');

    $userid = trim($_POST['newuserid']);
    $password = trim($_POST['newpassword']);
    $cam0 = trim($_POST['cam0']);
    $cam1 = trim($_POST['cam1']);
    $cam2 = trim($_POST['cam2']);
    $cam3 = trim($_POST['cam3']);
    
    $array[0] = "Camera 1";
    $array[1] = "Camera 2";
    $array[2] = "Camera 3";
    $array[3] = "Camera 4";
    $array[4] = "Camera 5";
    $array[5] = "Camera 6";
    $array[6] = "Camera 7";
    $array[7] = "Camera 8";
    $array[8] = "Camera 9";
    $array[9] = "Camera 10";
    $array[10] = "Camera 11";
    $array[11] = "Camera 12";
    $array[12] = "Camera 13";
    $array[13] = "Camera 14";
    $array[14] = "Camera 15";
    $array[15] = "Camera 16";
    
    $cam0 = array_search($cam0, $array);
    $cam1 = array_search($cam1, $array);
    $cam2 = array_search($cam2, $array);
    $cam3 = array_search($cam3, $array);            
    
    //Define data to write to file
    $line0 = "\r\n";
    $line1 = trim($userid);
    $line2 = ":";
    $line3 = trim($password);
    $line4 = "\r\n";
    $line5 = trim($cam0);
    $line6 = ",";
    $line7 = trim($cam1);
    $line8 = ",";
    $line9 = trim($cam2);
    $line10 = ",";
    $line11 = trim($cam3);
    
    //Write user data to file
    if (is_writable($filename)) {
        fwrite ($fp, $line0);
        fwrite ($fp, $line1);
        fwrite ($fp, $line2);
        fwrite ($fp, $line3);
        fwrite ($fp, $line4);
        fwrite ($fp, $line5);
        fwrite ($fp, $line6);
        fwrite ($fp, $line7);
        fwrite ($fp, $line8);
        fwrite ($fp, $line9);
        fwrite ($fp, $line10);
        fwrite ($fp, $line11);
        
    fclose($fp);    
User is offlineProfile CardPM

Go to the top of the page

corliss
post 21 Jul, 2007 - 08:34 AM
Post #2


D.I.C Head

Group Icon
Joined: 25 Oct, 2006
Posts: 119



Thanked 1 times

Dream Kudos: 50
My Contributions


Hey,

I think that you have answerd your own question.
Ditch the .txt file and switch to a free database such as mysql. That way all your doing is adding vaules to a db and adding these so called values you will not have to a. enter them manually and b. not have to worry about with space.
User is offlineProfile CardPM

Go to the top of the page

spullen
post 21 Jul, 2007 - 09:20 AM
Post #3


D.I.C Regular

Group Icon
Joined: 22 Mar, 2007
Posts: 330



Dream Kudos: 50
My Contributions


Yeah use mysql. To make it more secure when you make a new user you can md5 (hash) the password so like $hashed_password = md5($password), and then you would use the mysql INSERT INTO query, look it up for more details.

Then to do authentication I would do something like:
CODE

public function authenticate($username, $password){
     $auth = false;
     //open a db connection, make a db variable
     $query = "SELECT hashed_password FROM users WHERE username = '". $username ."'";
      if!($results = $db->query($query))
          die("ERROR: ".$db->error)
      $expected_password = $results->fetch_assoc();
      if($expected_password['hashed_password'] == md5($password)){
            $auth = true;
      }
      //close db connection
      return $auth;
}

you'll obviously need to fill in the blanks, but this should get you start on your way. And there is plenty of documentation on www.php.net .
User is offlineProfile CardPM

Go to the top of the page

stu26
post 21 Jul, 2007 - 05:47 PM
Post #4


New D.I.C Head

*
Joined: 18 Jul, 2007
Posts: 8


My Contributions


Thanks for the responses.

Normally I would use a database, however, this is for a customer several states away who hosts their own website on a personal PC locally inside their business, and it is unmanaged. Given that I have no control over the server, the customer is very non-technical, and I am doing it for free, I figure the best way to go about it is to simply supply the pages, requiring minimal changes to the server and taking minimal responsibility for it, especially since the application does not require it to be secure, it is more for selecting which camera streams are selected for a particular viewer.

I did however resolve the problem. When I was writing to the file I used "\r\n" to create the new line when I should have only used "\n".

Thanks again!
User is offlineProfile CardPM

Go to the top of the page

Styx
post 24 Jul, 2007 - 10:29 AM
Post #5


D.I.C Head

Group Icon
Joined: 4 Mar, 2007
Posts: 192



Dream Kudos: 225
My Contributions


On another note, you should use the file function since I believe it automatically propagates on the different newline combinations.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 05:53AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month