Jump to content

Remake a script to use a text file instead of mysql?


ztealmax

Recommended Posts

Hi is it possible to change this code to use text file instead of a sql database? ;)
just an idea, but think it should work?

[b]Login.php[/b]
[code]<?
session_start(); // start session.
?>
<!-- header tags, edit to match your own, or include template header file. -->
<html>
<head>
<title>Login</title>
<head>
<body>
<?
if(!isset($username) | !isset($password)) {
// escape from php mode.
?>
<form action="<?=$PHP_SELF?><?if($QUERY_STRING){ echo"?". $QUERY_STRING;}?>" method="POST">
<p align="center">Members only. Please login to access this document.</p>
<table align="center" border="0">
<tr>
  <th>
Username:
  </th>
  <th>
<input type="text" name="username">
  </th>
</tr>
<tr>
  <th>
Password:
  </th>
  <th>
<input type="password" name="password">
  </th>
</tr>
<tr>
  <th colspan="2" align="right">
<input type="submit" value="Login">
</form>
  </th>
</tr>
</table>
</body>
</html>
<?
exit();
}

// If all is well so far.

session_register("username");
session_register("password"); // register username and password as session variables.

// Here you would check the supplied username and password against your database to see if they exist.
// For example, a MySQL Query, your method may differ.

$sql = mysql_query("SELECT password FROM user_table WHERE username = '$username'");
$fetch_em = mysql_fetch_array($sql);
$numrows = mysql_num_rows($sql);

if($numrows != "0" & $password == $fetch_em["password"]) {
$valid_user = 1;
}
else {
$valid_user = 0;
}

// If the username exists and pass is correct, don't pop up the login code again.
// If info can't be found or verified....

if (!($valid_user))
{
session_unset();   // Unset session variables.
session_destroy(); // End Session we created earlier.
// escape from php mode.
?>
<form action="<?=$PHP_SELF?><?if($QUERY_STRING){ echo"?". $QUERY_STRING;}?>" method="POST">
<p align="center">Incorrect login information, please try again. You must login to access this document.</p>
<table align="center" border="0">
<tr>
  <th>
Username:
  </th>
  <th>
<input type="text" name="username">
  </th>
</tr>
<tr>
  <th>
Password:
  </th>
  <th>
<input type="password" name="password">
  </th>
</tr>
<tr>
  <th colspan="2" align="right">
<input type="submit" value="Login">
</form>
  </th>
</tr>
</table>
</body>
</html>
<?
exit();
}
?>
[/code]


//Cheers
Martin
To be honest... not too difficult.

Here's a mockup of how I would do it, probably with OO, though

[code]
<?php
/**
* @author: keeb
* @description: Parses a text file with "<username>:<password>" format. Password should be stored in md5.
*/

$userlist = "userlist.txt"; //path to the list of users
$password_to_check = md5("abc123"); // mock user input
$user_to_check = "keeb"; //mock user input
$handle = fopen($userlist, "r");
$contents = fread($handle, filesize($userlist));

$contents = explode("\n", $contents); //turn contents in to array seperated by spaces;

foreach($contents as $check) {
if ( returnUser($check) == $user_to_check && returnPassword($check) == $password_to_check ) {
print "user logged in";
}
}

function returnUser($str) {
$data = explode(":", $str);
return $data[0];
}
function returnPassword($str) {
$data = explode(":", $str);
return $data[1];
}

?>
[/code]

My sample userlist.txt looks like this:

[code]
keeb:e99a18c428cb38d5f260853678922e03
[/code]

Good luck

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.