Jump to content

Flat File Login


tarun

Recommended Posts

<?php
$user  =  $_POST["u"];
$pass  =  $_POST["p"];
$db = file("db.txt");

foreach($db as $key => $val) {
$data[$key] = explode("|-|", $val);
}

for($k = 0; $k < sizeof($db); $k++) { 
$u = $data[$k][0];
$p = $data[$k][1];

if ( $u == "$user" ) {
if ( $p == "$pass" ) {
echo 'You Are Now Logged In As: '.$u.'';
}
else {
echo 'Username And Password Do Not Match';
}

}

}
?>

 

Theres Still More To Add When Your Logged In Such As Sessions

But At The Moment I Can't Even Log In

I Get My Error "Username And Password Do Not Match"

Link to comment
Share on other sites

You're "if" is incorrect, try:

<?php
if ( $u == $user && $p == $pass)
    echo 'You Are Now Logged In As: '.$u;
else
    echo 'Username And Password Do Not Match';
?>

 

BTW, what is the format of your input file? There may be an easier way of doing this.

 

Ken

Link to comment
Share on other sites

By Input File Do You Mean The db.txt File

It Looks Similar To This

user1|-|pass1
user2|-|pass2

 

 

And With This "NEW" Code I Get:

Username And Password Do Not MatchUsername And Password Do Not Match

 

It Comes Out Twice Because There Are Two Columns Of Data (eg. Col1:user1 Col2:pass1)

Link to comment
Share on other sites

Ok. Go back to your original "if" statement. I believe the problem is that you're not trimming off the newline character from the end of the input lines before you do the compare. Change:

<?php
foreach($db as $key => $val) {
$data[$key] = explode("|-|", $val);
}?>

to

<?php
for ($i=0;$i<count($db);$i++)
$data[] = explode("|-|", trim($db[$i]);
?>

 

Here is my version of your code that I tested to make sure it worked:

<?php
$user  =  $_POST["u"];
$pass  =  $_POST["p"];
$data = array();
$db = file("db.txt");

for ($i=0;$i<count($db);$i++)
$data[] = explode("|-|", trim($db[$i]));

for($k = 0; $k < count($data); $k++) { 
if ( $data[$k][0] == $user )
	if ( $data[$k][1] == $pass )
		echo 'You Are Now Logged In As: '.$user;
	else
		echo 'Username And Password Do Not Match';
}
?>

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.