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
https://forums.phpfreaks.com/topic/46170-flat-file-login/
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
https://forums.phpfreaks.com/topic/46170-flat-file-login/#findComment-225984
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
https://forums.phpfreaks.com/topic/46170-flat-file-login/#findComment-225994
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
https://forums.phpfreaks.com/topic/46170-flat-file-login/#findComment-226017
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.