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: 1948
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: 1 year ago.
Category: Programming
Expert:  The-PC-Guy replied 1 year ago.
send your code, thanks
Customer: replied 1 year 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 1 year 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 1 year ago.
let me know if that makes sense.
Customer: replied 1 year 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 1 year ago.
sorry my bad change if(in_array($username . ' ' . $password)) { to if(in_array($username . ' ' . $password,$file)) {
Customer: replied 1 year 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 1 year ago.
want to send me the complete thing? both the creation of the file, and the login file?
The-PC-Guy, Software Engineer
Category: Programming
Satisfied Customers: 1948
Experience: Extensive Knowledge in PHP, MYSQL, CSS & Javascript
The-PC-Guy and other Programming Specialists are ready to help you
Customer: replied 1 year ago.

Attachment: 2015-07-23_144229_login.zip

That would be fantastic!

Many thanks,

Samia

Expert:  The-PC-Guy replied 1 year 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 1 year 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 1 year 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 1 year ago.
first you didn't make the changes I told you in the file you sent me
Customer: replied 1 year ago.

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

Expert:  The-PC-Guy replied 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year ago.

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

Thank you

Expert:  The-PC-Guy replied 1 year 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 1 year ago.

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

Many thanks,

Samia

What Customers are Saying:

 
 
 
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer
< Previous | Next >
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C.
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C.
 
 
 

Meet The Experts:

 
 
 
  • ATLPROG

    ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7260
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
< Previous | Next >
  • http://ww2.justanswer.co.uk/uploads/SP/spatlanta2010/2011-6-23_12450_photo.64x64.gif ATLPROG's Avatar

    ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7260
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
  • http://ww2.justanswer.co.uk/uploads/ComputersGuru/2010-02-13_051118_Photo41.JPG LogicPro's Avatar

    LogicPro

    Computer Software Engineer

    Satisfied Customers:

    5514
    Expert in C, C++, Java, DOT NET, Python, HTML, Javascript, Design.
  • http://ww2.justanswer.co.uk/uploads/lifesaver333/2010-10-17_191349_ls.jpeg lifesaver's Avatar

    lifesaver

    Computer Software Engineer

    Satisfied Customers:

    936
    Several years of intensive programming and application development experience in various platforms.
  • http://ww2.justanswer.co.uk/uploads/EH/ehabtutor/2012-8-2_202016_1.64x64.jpg ehabtutor's Avatar

    ehabtutor

    Computer Software Engineer

    Satisfied Customers:

    766
    Bachelor of computer science, 5+ years experience in software development, software company owner
  • http://ww2.justanswer.co.uk/uploads/eljonis/2010-01-06_130406_eljon2.jpg Eljon's Avatar

    Eljon

    Consultant

    Satisfied Customers:

    558
    Founder of StockCanvas.com
  • http://ww2.justanswer.co.uk/uploads/FA/fael/2011-1-15_104041_profile500x500.64x64.jpg Rafael Martins's Avatar

    Rafael Martins

    Computer Software Engineer

    Satisfied Customers:

    446
    Desktop, Mobile and Web Developer. 7+ years of experience. Creative solutions provider.
  • http://ww2.justanswer.co.uk/uploads/CH/chutz747/2011-11-6_14231_me.64x64.jpg The-PC-Guy's Avatar

    The-PC-Guy

    Software Engineer

    Satisfied Customers:

    325
    Extensive Knowledge in PHP, MYSQL, CSS & Javascript
 
 
 

Related Programming Questions