How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site. Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask The-PC-Guy Your Own Question
The-PC-Guy
The-PC-Guy, Software Engineer
Category: Programming
Satisfied Customers: 2050
Experience:  Extensive Knowledge in PHP, MYSQL, CSS & Javascript
62934938
Type Your Programming Question Here...
The-PC-Guy is online now

I'm creating a log in system using php assignment.

Resolved Question:

Hello,
I'm creating a log in system using php for my assignment. I can only store my user's data (username and password) in a text file as i'm not allowed to use mysql. I've managed to produce the working code for this, however the problems i'm having with my code is when my user enters invalid login details my code produces an error and when a third user enters valid login details, their login don't get accepted. User 1 and 2 pass get accepted. I wondering if it's possible to have a look at my code and try and help me fix the error....
Many thanks,
Samia
Submitted: 2 years ago.
Category: Programming
Expert:  The-PC-Guy replied 2 years ago.
send your code, thanks
Customer: replied 2 years ago.

if (isset($_POST['login'])){

$username=htmlentities($_POST['username']);
$password=htmlentities($_POST['password']);

function authenticate($username, $password){
$file = explode( PHP_EOL, file_get_contents( "accounts.txt" ));/*reads the file and stores file contents
in variable $file*/

foreach($file as $line ) {
$split = explode(" ", $line);/*splits each line of the file and separates each value
which has a space between the every value*/

//split[3]is the place holder for username in my file and $split[4] for password
if ($username === $split[3] && $password === $split[4])
return true;
}

return false;
}

//this function is linked to the above function used to display and authorize the result
if(authenticate($username, $password)) {
echo "Login successfull!";
} else {
echo "Invalid username or password";
}

}

Expert:  The-PC-Guy replied 2 years ago.
i wouldn't do it this way, since explode() puts the values into an array I would just simply do a concatenation, and forget about splitting it. Customerif (isset($_POST['login'])){$username=htmlentities($_POST['username']);$password=htmlentities($_POST['password']);function authenticate($username, $password){$file = explode( PHP_EOL, file_get_contents( "accounts.txt" ));/*reads the file and stores file contentsin variable $file*///$file is already and arrayif(in_array($username . ' ' . $password)) {return true;} else {return false;} //end if} //end function//this function is linked to the above function used to display and authorize the resultif(authenticate($username, $password)) {echo "Login successfull!";} else {echo "Invalid username or password";} }
Expert:  The-PC-Guy replied 2 years ago.
let me know if that makes sense.
Customer: replied 2 years ago.

Thank you! Unfortunately the code doesn't work but I did understand it.

The following error message alerts now:

Warning: in_array() expects at least 2 parameters, 1 given in /home/seloua01/public_www/p1fma/login.php on line 23 Call Stack: 0.0014 335984 1. {main}() /home/seloua01/public_www/p1fma/login.php:0 0.0023 337344 2. authenticate() /home/seloua01/public_www/p1fma/login.php:31 0.0045 338688 3. in_array() /home/seloua01/public_www/p1fma/login.php:23 Invalid username or passwordRETURNING USERS

Perhaps you may need to take a look at the format of my file as I didn't just store username and password ***** accounts.txt. Also storing fullname and email-username and password ***** the last two strings contained in each line.

The following is what my file accounts.txt looks like:

***** ********@******.*** sam123 password1
samia el-ouahabi***@******.*** seloua01 password2
Bob grif***@******.*** bobo18 password3

Many thanks,

Samia

Expert:  The-PC-Guy replied 2 years ago.
sorry my bad change if(in_array($username . ' ' . $password)) { to if(in_array($username . ' ' . $password,$file)) {
Customer: replied 2 years ago.

Hey,

No worries! The code now doesn't produce any errors but unfortunately I still can't log in any valid users who exist in my file accounts.txt.

My first code I sent you only has one problem now as I managed to log in all my users including user 3. Now the only problem with my original code is only when a user enters invalid log in details the following errors message alters:

Notice: Undefined offset: 3 in /home/seloua01/public_www/p1fma/login.php on line 27 Call Stack: 0.0011 337776 1. {main}() /home/seloua01/public_www/p1fma/login.php:0 0.0029 339136 2. authenticate() /home/seloua01/public_www/p1fma/login.php:36 Notice: Undefined offset: 3 in /home/seloua01/public_www/p1fma/login.php on line 27 Call Stack: 0.0011 337776 1. {main}() /home/seloua01/public_www/p1fma/login.php:0 0.0029 339136 2. authenticate() /home/seloua01/public_www/p1fma/login.php:36 Invalid username or password

Many thanks,

Samia

Expert:  The-PC-Guy replied 2 years ago.
want to send me the complete thing? both the creation of the file, and the login file?
The-PC-Guy and other Programming Specialists are ready to help you
Customer: replied 2 years ago.

Attachment: 2015-07-23_144229_login.zip

That would be fantastic!

Many thanks,

Samia

Expert:  The-PC-Guy replied 2 years ago.
you can, but it may take some time for them to get it to me. if you want you can upload to a site like www.wikisend.com after uploading give me the links that they provide to you
Customer: replied 2 years ago.

In the following link log.php contains the coding for my login system and accounts.txt contains valid user log in details-last two strings on each line is the username and password ***** to each user.

http://wikisend.com/download/893286/login.zip

Many thanks,

Samia

Customer: replied 2 years ago.

Sorry, please discard the previous links and use the following:

http://wikisend.com/download/285604/login.php

http://wikisend.com/download/362986/accounts.txt

Expert:  The-PC-Guy replied 2 years ago.
first you didn't make the changes I told you in the file you sent me
Customer: replied 2 years ago.

Sorry I sent you the wrong file. I will send you the updated version.

Expert:  The-PC-Guy replied 2 years ago.
also, it appears you have more in the text file then you specified in your original request so we will have to further modify my changes. Send me and updated file
Customer: replied 2 years ago.

I want the system to be able to log in multiple users.

http://wikisend.com/download/628968/login.php

Expert:  The-PC-Guy replied 2 years ago.
ok, i modified the way it is working entirely, due to the way your text file was set up. http://wikisend.com/download/111590/login.php I don't think it will give you any more problems now
Customer: replied 2 years ago.

Thanks you so much Smile it works completely!!

I understand the code apart from the following line:

if (strpos($file,$username . ' ' . $password) > 0) {
return true;

Also my lecturer has only shown us how to retrieve a value from a file by putting the file in an array and exploding it. I know you said you wouldn't explode the file but is there any chance you know how to log in users using the explode function?

Many thanks,

Samia

Customer: replied 2 years ago.
It's ok I've just confirmed with my colleagues that it's ok to not use the explode function!
Expert:  The-PC-Guy replied 2 years ago.
it could be done with explode if you only had user and pw in that file since you have other info this is just better way of doing it
Customer: replied 2 years ago.

Hello,

I just found out my users can also log in by entering their email in username field and name in password ***** I know this is happening because in my file I don't just have username and password ***** Also have stored full name and email. Is there any way I could change my code to only accept username and password ***** file??

Many thanks,

Samia

Expert:  The-PC-Guy replied 2 years ago.
no way. You would have to modify the text file. Of course the whole thing is ridiculously insecure. Anyone could just read the whole text file and get everyone's password. I would suggest using a database instead.
Customer: replied 2 years ago.

Unfortunately my assignment does not allow database. I can modify the text file. How do you want it modified?.

Expert:  The-PC-Guy replied 2 years ago.
first I am sending you an offer of additional service to cover my extra time, then I will be happy to help you further.
Customer: replied 2 years ago.

Ok no problem please as my assignment is due on Friday!

Thank you

Expert:  The-PC-Guy replied 2 years ago.
so, i dont have your text file in front of me. But as I remember you had each thing separated by a space. So since the login is looking for username password. I think it was something like $username . " " . $password. if you change the space in the text file between user and pw to some other character comma for example. Change that for each line in the text file. then change the $username . " " . $passwordto something like $username . "," . $password. where the comma used matches whatever caracter you chose for the text file. Let me know if you need me to make modifications to your PHP or if you understand what is needed.
Customer: replied 2 years ago.

Thank for your service Smile it finally properly works!!!

Many thanks,

Samia