Need Help ASAP!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mtsgill
Forum Newbie
Posts: 6
Joined: Sat Mar 31, 2012 12:06 pm

Need Help ASAP!!

Post by mtsgill »

Hi Folks!!

I am trying to create a login script. I do not want to use any DB for Username/Password validation as this is for assignment purposes!!

I have written this so far!! I have no luck in getting this right so far. Please can somebody help me in getting this right!!!

I have usernames and passwords stored in a text file called USERS.TXT in the following format which I need to use for validation during comaprison:

Dave:Dave123
Katie:Katie123
...and so on

PS: I am a brand new beginner in PHP World! :)



<?

// Check if the form has been submitted:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$handle = fopen("users.txt", "r");
// Handle the form:
if ( (!empty($_POST['username'])) && (!empty($_POST['password'])) ) {

if ( (strtolower($_POST['username']) == 'user') && ($_POST['password'] == 'pass') ) { // Correct!

// Do session stuff:
session_start();
$_SESSION['username'] = $_POST['username'];
$_SESSION['loggedin'] = time();

// Redirect the user to the welcome page!
ob_end_clean(); // Destroy the buffer!
header ('Location: welcome.php');
exit();

} else { // Incorrect login details!

print '<p>The login details do not match with those on our system!<br />Please try again.</p>';

}

} else { // Forgot a field.

print '<p>Please make sure you enter both the username and password!<br />Please try again.</p>';

}

} else { // Display the form.
print '<form action="login.php" method="post">
<p>Username: <input type="text" name="username" size="20" /></p>
<p>Password: <input type="password" name="password" size="20" /></p>
<p><input type="submit" name="submit" value="Log In!" /></p>
</form>';

}

?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need Help ASAP!!

Post by Celauran »

You're currently not doing anything with the file handle you open and are instead checking if the username and password entered match the literal strings 'user' and 'pass'. Take a look at fgets() to read in the data from the file and go from there. Also, please use syntax tags (the PHP Code button) when you're posting code.
mtsgill
Forum Newbie
Posts: 6
Joined: Sat Mar 31, 2012 12:06 pm

Re: Need Help ASAP!!

Post by mtsgill »

First of all thanks for your prompt reply!!

~You are a life saver :)

I do apologize for not posting it in the right manner.

However, I would really appreciate if you could guide me how to use the syntax code!!

I forgot to delete include handle function as I am not using it.

I tried doing the following but it did not work!!

<?php

session_start();

$username = htmlspecialchars($_POST['username']);
$password = md5($_POST['password']);
// Theres your 'nice' variables
$file = './users.txt';

// Lets open it up:
if (!$login = file($file))
{
echo 'Unable to open file...';
exit;
}
foreach ($login AS $members)
{
// For this, we are creating an array for each user, with their username as the index
list($user, $pass) = split(' ', trim($members));
$members_array[$user] = array('password' => $pass);
}
if (array_key_exists($username, $members_array))
{
if ($members_array[$username]['password'] == $password)
{
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
echo 'Successfully logged in';
}
else
{
echo 'Incorrect Password.';
}
}
else
{
echo 'Incorrect username entered';
}

I feel so stressed!! :(
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need Help ASAP!!

Post by Celauran »

mtsgill wrote:

Code: Select all

$username = htmlspecialchars($_POST['username']); 
$password = md5($_POST['password']);

Code: Select all

list($user, $pass) = split(' ', trim($members));
Neither of those code blocks match with how you described your text file. Are the passwords stored as plain text, as your original post suggests, or are they md5 hashes? Are username and password separated by a space or a colon?
mtsgill
Forum Newbie
Posts: 6
Joined: Sat Mar 31, 2012 12:06 pm

Re: Need Help ASAP!!

Post by mtsgill »

The second code is just as an alternative, I am still following the first code.

And, you are right, usernames and passwords are stored in a plain text file called "users.txt" and separated by : as follows,

user1:password1
user2:password2
...and so on

I am reading through fopen function and i think i need to include the following in the code too:

$file = fopen("users.txt","r"); //this would open the users.txt file for validation, i believe!! :S

now i am going through fgets, as suggested by you but finding it so difficult to put everything altogether.

I really appreciate your help!! Thanks a lot!
mtsgill
Forum Newbie
Posts: 6
Joined: Sat Mar 31, 2012 12:06 pm

Re: Need Help ASAP!!

Post by mtsgill »

I joined this great community thinking i will get some sort of expert help.

Sadly 18 times people visited it but only one angel stepped in to help me earlier.

Guys!! Please help me, you guys are pro and I am an amateur.

Please help me get this sorted.

Please!!!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Need Help ASAP!!

Post by califdon »

Settle down. You are getting help from one of our top responders. You are just not helping yourself. In forums like this one, when one responder is handling a request, it is very common for others to move on to someone else who hasn't received any help yet. Your time would be better spent by following the suggestions of the person helping you, asking whenever you don't understand. Whining will not help.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: Need Help ASAP!!

Post by litebearer »

Not to mention FREE help on a Saturday (Between Good Friday and Easter Sunday) AND having to wait all of 47 minutes!!!

hint: google php file()
Post Reply