Hello.
I hope you can help me please.
We are currently using this login.php, which works to protect a staff area by using an include on each php we want to protect:
include_once('/home/foo/htdocs/staff/protect.php');
There is also present a logout.php which obviously logs the user out, the included protect.php to ensure users are logged in to view a page, as well as the following login.php. Our users and passwords are not stored in a database, they are stored in a flatfile (as we cannot use SQL yet...):
<?php
session_start();
function output_error($text='')
{
include_once('/home/foo/htdocs/header.php');
echo "<p>Errors:</p>\n<ul>\n" . $text . "\n</ul>\n";
include_once('/home/foo/htdocs/footer.php');
exit();
}
if (!isset($_SESSION['correctcode'])) {
output_error('<li>enable cookies.</li>');
}
$correctcode = $_SESSION['correctcode'];
$securitycode = $_POST['securitycode'];
if ($securitycode != $correctcode) {
output_error('<li>human validation check failed.</li>');
}
$user_data = file("/home/foo/hidden/users.txt");
foreach($user_data as $val)
{
list($user, $pwd) = explode(",", trim($val));
$users[$user] = $pwd;
}
$username = $_POST['username'];
$password = md5($_POST['password']);
if (array_key_exists($username, $users))
{
if ($password == $users[$username])
{
$_SESSION['logged'] = true;
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
setcookie('logindate', date('d/m/Y'));
setcookie('logintime', date('H:i:s'));
header ("Location: [url]http://www.domain.com/staff/home.php[/url]");
exit();
}
else
{
output_error('<li>password has been entered incorrectly.</li>');
}
}
else
{
output_error('<li>username has not been recognised.</li>');
}
?>
Basically, what we want to accomplish is a 'pass.php' which will do the following:
a) Confirm that the old password is correct before proceeding.
b) Confirm a new and confirmed password field match before continuing.
c) Make an MD5 hash of the password and update our users.txt.
The syntax of our users.txt is:
name,md5hashpass
Obviously to achieve this... I realise I would need to use a standard form which would post to a pass.php, but can you advise what field names we'd need?
So far, for the actual pass.php I have the following, can you suggest improvements to actually get it to achieve the above list?
Any help is MUCH appreciated, thank you!
pass.php:
<?php
include_once('/home/foo/htdocs/staff/protect.php');
function output_error($text='')
{
include_once('/home/foo/htdocs/header.php');
echo "<ul>\n" . $text . "\n</ul>\n";
include_once('/home/foo/htdocs/footer.php');
exit();
}
if( md5($oldpass) == $nowpass )
{
if( $newpass == $newpass2 )
{
$file = file( "/path/to/users.txt" );
// Scan the file for the right line, and replace it, at the same time, build the new file to be written
while( list($key,$val) = each($file) )
{
if( ereg( $user, $val) )
$val = $newpass;
$newfile .= "$key,$val\n";
}
// Now we have a variable $newfile, which contains the contents of the new file to be written
// Write the file
$fp = fopen( "/path/to/users.txt", "w" );
fwrite( $fp, $newfile, strlen($newfile) );
fclose($fp);
}
else
{
output_error('<li>the new and confirmed passwords did not match.</li>');
}
}
else
{
output_error('<ul>the password you entered is not your current password.</li>');
}
?>