i am trying to use a flatfile database just for a basic webpage. i have no mySQL and need a login file to keep certaian things private from guests, and to also have members view a personal page each time they login. i have been trying to use this code

$name = $_POST['name'];
$email = $_POST['email'];

$fp = fopen("data.txt","a");

if(!$fp) {
    echo 'Error: Cannot open file.';
    exit;
}

fwrite($fp, $name."||".$email."||");

fclose($fp);

to register the users.
But when i open the data file it will not put each user on seperate lines, user||user@user.com||user2||user2@user2.com||user3||user3@user3.com i end up with this.
after all this i have tried to use

<?php
$userinfo = file("data.txt");

   echo '<table>';

foreach($userinfo as $key => $val) 
{
   //explode that data into a new array:  
   $data[$key] = explode("||", $val);
}

for($k = 0; $k < sizeof($userinfo); $k++) 
{ 
   echo '<tr><td>Name:</td><td>'.$data[$k][0].'</td></tr>';
   echo '<tr><td>Email:</td><td>'.$data[$k][1].'</td></tr>';
   echo '<tr><td colspan=2> </td></tr>';
}

   echo '</table>';
?>
<?php
if ($_POST['username'] != $data || $_POST['useremail'] != $data) { ?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" /> <br/><br/>
Enter Name
<input type="text" name="username" />
<br/>
Enter Email
<input type="text" name="useremail"  />
<br/>
<br/>
<input type="submit"  />
<?php } else { ?>
<?php 

echo "Hello $data"; ?>




<?php } ?>

i have tried to use that as a basic login, showing the users at the top and the login box at the bottem.
I am really struggling with this and cannot find anything to help me with it.

Thank you in advance for helping

Recommended Answers

All 19 Replies

Member Avatar for diafol

Sorry to sound thick, but any reason why you can't use a DB? Files are such a pain.

i signed into a 4 year deal that will not allow Databases,

i suggest you that you use database because in feature there are getting more trouble for finding any of user information in this file and also with database login is so simple and more secure then file. so use database for login information submission.

it does not need to be secure. all the info it holds is a last name.

use DB even if security is not problem

Member Avatar for diafol

i signed into a 4 year deal that will not allow Databases,

I assume it's a very low cost deal if it doesn't allow DBs. I'd suggest upgrading if possible. Sorry, this isn't really answering your question is it?

From what I can see, and I may be wrong, you have (or want to have) a list of all members usernames with their emails and then a login form. Surely with this setup, anyone can log in using anybody else's info?

Also

... $_POST['username'] != $data || $_POST['useremail'] != $data ...
//AND
echo "Hello $data";

don't seem to make much sense if $data is an array. There's something very wrong with this.

that was just a basic test to se if it worked.
all i need to know is when the data is stored in the file
e.g
user||user@user.com
user2||user2@user2.com

how to get php to read this and when user logs on it says "hello user" or if user 2 logs on it says "hello user2"

one record per line

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$fp = fopen("data.txt","a");
if(!$fp) { echo 'Error: Cannot open file.';
 exit; }
fwrite($fp, $name."|".$email."|" \n);
fclose($fp);

perhaps something to locate usernames

<?php
function nameexists($name){
if (($handle = fopen("data.txt", "r")) !== FALSE) {
 while (($data = fgetcsv($handle, 0, "|")) !== FALSE) {
  if($name = $data[0] { fclose($handle);
   return true;}
 }
fclose($handle);
return false; } } ?> 
// unsure:: but ::something like
if(!nameexists($_post["name"]) echo 'guest'; else echo $_post['name'];

no guaranttee, HTH

double post opps

sorry im new to php
you will have to explain do both of these go in the same page or diffrent?
and
i create the user register script

<?php
$name = $_POST['name']
;$email = $_POST['email'];
$fp = fopen("data.txt","a");
if(!$fp) { 
echo 'Error: Cannot open file.'; 
exit;
 }
fwrite($fp, $name."|".$email."|" \n);
fclose($fp);
?>
<form method="post" action="what goes here?" >
enter name:
<input type="text" name="name" />
<br/>
<br/>
enter email:
<input type="text" name="email" />
<br/>
<br/>
<input type="submit" value="login" />
</form>

Thats the register box,
Then the login box on a new page.

<form method="post" action="what goes here?" >
enter name:
<input type="text" name="name" />
<br/>
<br/>
enter email:
<input type="text" name="email" />
<br/>
<br/>
<input type="submit" value="login" />
</form>

<?php
function nameexists($name){
if (($handle = fopen("data.txt", "r")) !== FALSE) {
 while (($data = fgetcsv($handle, 0, "|")) !== FALSE) {
  if($name = $data[0] { fclose($handle);
   return true;}
 }
fclose($handle);
return false; } } ?> 
// unsure:: but ::something like
if(!nameexists($_post["name"]) echo 'guest'; else echo $_post['name'];

Hay nats01282,

I Have created a Registration page and Login page for You. That is Insert Data in data.txt file and also login with this data.txt file. the below script also check the data that is already inserted or not in data.txt file. use the below script:

reg.php file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Registration Test Page</title>
</head>

<body>

<form method="post" action="reg.php" >
enter name:
<input type="text" name="name" />
<br/>
<br/>
enter email:
<input type="text" name="email" />
<br/>
<br/>
<input type="submit" value="Register" name="submit" />
</form>

</body>
</html>

<?PHP

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

$name = $_POST['name'];
$email = $_POST['email'];

$file = file_get_contents("data.txt");
$string = "$name||$email";
if(!strstr($file, "$string"))
{
$myFile = "data.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "$name||$email\n";
fwrite($fh, $stringData);
echo "your Registration Information Successfully Inserted.";
fclose($fh);
}
else
{
echo "Sorry the name: $name and email: $email is already in database. please use defrent name & email.";
}
}

?>

Data Ex: user||user@user.com
user1||user1@user1.com
Database file for Demo: http://www.kuchamancity.com/test/data.txt

login.php file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login Page Test Script.</title>
</head>

<body>
<form method="post" action="login.php" >
enter name:
<input type="text" name="name" />
<br/>
<br/>
enter email:
<input type="text" name="email" />
<br/>
<br/>
<input type="submit" value="login" name="submit"/>
</form>
</body>
</html>
<?PHP

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

$name = $_POST['name'];
$email = $_POST['email'];

$file = file_get_contents("data.txt");
if(!strstr($file, "$name||$email"))
{
echo "Sorry! You Insert an Invalid Name & Email. Please Use a Valid name & email and Try Again.";
}
else
{
echo "Welcome $name.<br/>You Have Successfully Logging.";
}
}
?>

the above script is tested and work fine.
Demo:
Registration: http://www.kuchamancity.com/test/reg.php
Login Page: http://www.kuchamancity.com/test/login.php
Data File: http://www.kuchamancity.com/test/data.txt
the above script is example based only please modify it as your own requirement. if you find any bug in above script then post your problem here.

commented: Excelent work on login script without mySQL database. solved my question thanks you +1

Thank you hemgoyal_1990 you have solved my problem :)

next help question lol.

now i have my login and register script how do i show diffrent pages to diffrent people?
e.g
if john logs in he gets a page showing all johns information and stuff
but if mary logs in she see`s all marys details?

You're beginning to push the usual capabilities of a flat file database
but a redirect to more than a few users makes a very convoluted system because the search of a flat file takes time
you can store the information in the file, and populate the fields in user.php with values pulled from the file

You're beginning to push the usual capabilities of a flat file database
but a redirect to more than a few users makes a very convoluted system because the search of a flat file takes time
you can store the information in the file, and populate the fields in user.php with values pulled from the file

how do you mean?

fields in the flat file database are currently $name and $email
there is room in a flat file .csv(or any other separator, see the php .csv file handling functions at w3cschools or php.net ) for a large number of data columns, as long as you remember what they are for eg.
$name, $email, $nickname, $address, $phone, preferences1, preferences2, preferences3, preferences4, preferences5
In a flat file I put the first record as fieldnames as a memory aid
As long as you terminate each record with a newline \n its easy to pull the line that matches the username from the file, and populate the fields in a welcome script, an update your details script, or just color scheme from the file.
BUT a flat file is not efficient to access, as the number of users get larger the delay in processing is exponentially bigger
newer users tend to access more often than older users, newer users are always at the end of the file so every prior line is read before their details are reached
1-1000 users, easypeasy
10000 users delays
20000 users php timeouts,
examples only not a definite number where errors occur as it depends on the complexity of the data file (number of columns and size of data)

I prefer fputcsv() fgetcsv() to fwrite() filegetcontents() for datafiles
they read write single lines and do not need the entire file in memory, and ensure that the data is formatted in the manner that my scripts expect

If you use .csv comma separators, you can import the file into a spreadsheet to examine it

With the script above posted by hemgoyal_1990.
Is they a way to keep the users logged in as they view around on diffrent pages? ????

read up on sessions, you establish the session on login kill it on logout or timeout no activity

you may set the session for logged into you site and destroy the session when your logged out or set the timeout if not any activity done in given time. you may also use the below script for set the session and destroying the session:


login.php:

<?php
session_start();
// This part sets up the connection to the 
// database (so you don't need to reopen the connection
// again on the same page).
if ($_SESSION["valid_user"])
{
	// User not logged in, redirect to login page
	Header("Location: welcome.php");
}
// is selected.
if (isset($_POST['submit']))
{ 
	$name = $_POST['name'];
	$email = $_POST['email'];

	$file = file_get_contents("data.txt");
	if(strstr($file, "$name||$email"))
	{

		$_SESSION["valid_user"] = $_POST["name"];
		$_SESSION["valid_time"] = time();

		// Redirect to member page
		Header("Location: welcome.php");
	}
	else
	{
		// Login not successful
		print '<script> alert ("Sorry! You have entered a Invalid Login Id or Password."); window.location="login.php"; </script>';
	}
}
else
{
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login Page Test Script.</title>
</head>

<body>
<form method="post" action="login.php" >
enter name:
<input type="text" name="name" />
<br/>
<br/>
enter email:
<input type="text" name="email" />
<br/>
<br/>
<input type="submit" value="login" name="submit"/>
</form>
</body>
</html>';
}
?>

welcome.php:

<?PHP
session_start();

if (!$_SESSION["valid_user"])
{
// User not logged in, redirect to login page
Header("Location: login.php");
}

$name = $_SESSION["valid_user"];

echo "Welcome $name.<br/>You Have Successfully Logging.";

?>

logout.php

<?php
session_start();
session_unset();

session_destroy();
// Logged out, return home.
print '<script> alert ("You Have Successfully Logged Out."); 
window.top.location.href = "login.php";; </script>';
?>

The above script is fully tested and work fine.
if you find any problem in this script then post your problem here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.