Forum Moderators: coopster

Message Too Old, No Replies

Change Password (PHP)

Stuck on Coding

         

geo039

9:17 pm on Feb 28, 2006 (gmt 0)

10+ Year Member



I am trying to write a simple page in PHP for changing a password that writes to a text file and confirms successful change or not. Here is the working script I have for the log in part of my task but I cannot for the life of me figure out how to do a change password. Any help would be greatly appreciated.

<?php

session_start();

$u=$_POST[username_field];
$p=$_POST[password_field];

$_session["u"]=$u;
$_session["p"]=$p;
$array1 = file("data.txt");

$counter=0;
$username_counter=0;
$password_counter=0;

foreach ($array1 as $line) {
if ($counter/2==round($counter/2)) {
$username_array[$username_counter]=chop($line);
$username_counter++;
} else {
$password_array[$password_counter]=chop($line);
$password_counter++;
}
$counter++;
}

$match=0;
for ($x=0; $x<$username_counter;$x++) {
if (($u==$username_array[$x]) && ($p==$password_array[$x])) {
$match=1;
}
}

if ($match==0) {
print "<html>";
print "<head>";
print "<title>Login Page</title>";
print "<body>";
print "<center>";
print "<img src='images/secure.jpg'>";
print "<h1>Enter Username and Password Below</h1>";
print "<form name='form1' action='check_login.php' method='post'>";
print "<p>USERNAME:
<input type='text' size='20' name='username_field'></p>";
print "<p>PASSWORD:
<input type='text' size='10' name='password_field'></p>";
print "<input type='submit' value='LOGIN'>";
print "</form>";
print "</center>";
print "</body>";
print "</html>";
print "<center><b><font color='red'> **INVALID LOGIN** </font></b></center>";
} else {
header("Location:main.php");
}

?>

[edited by: jatar_k at 10:44 pm (utc) on Feb. 28, 2006]
[edit reason] trimmed code [/edit]

StupidScript

11:13 pm on Feb 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome, geo039!

Part of your problem is the use of a text file as your data storage medium. It's not un-doable, but you are just at the beginning of your major headaches.

Can you use a database structure, instead?

coopster

1:20 am on Mar 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Although I would agree that userID/password storage is much easier in a database, it can indeed be done in a text file. The overall process would be ...
  1. User selects 'Change Password' option.
  2. You display the screen which allows the option
  3. Upon submit you verify the attempt; make sure the user is the logged in user, make sure the passwords match, make sure ... you get the picture
  4. Open the text file and read it into memory
  5. Locate the row with the userID that is asking for a password change
  6. Update this value
  7. Write the entire file back out with the updated row
  8. If the write was successful, let the user know.

When working with text files, you will probably want to flock() [php.net] the file before opening/writing to it.

geo039

3:16 pm on Mar 1, 2006 (gmt 0)

10+ Year Member



Yes unfortunately in this case I need to do it with a text file and that's what's painful. The solutions I've found all use a database. I sort of understand what I need to do but to code it out I can't get it to work.

Thanks for the input

coopster

3:52 pm on Mar 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Which line item number do you get stuck on? Can you get through the first three OK?

geo039

3:45 pm on Mar 2, 2006 (gmt 0)

10+ Year Member



Actually the code I displayed was the log in which works fine I just need a script that will work for a change password page using a text file. I need to do the following
1. Get new pw and re-type new password
2. Retrieve global username and password
3. Retrieve username list and pw list
4. Check if np=np2
5. Search username list for current username and password
6. Re-write new info to text file
There is simple html page forms. If the password match then a confirmation page is called saying it was successful, if they don't match, like above you have to try again.

Here is what I have and I keep getting some parse error past lines at the end of the script for expecting $. I know it's probably simple but I am so tired of working on this, it's driving me insane. I don't even know if i'm on the right track.

session_start();

$np=$_POST[np];
$np2=$_POST[np2];

if(($np=="")¦¦($np2=="")){
include('chgpw.html');

print "New Password Field is blank. Please try again".$_SESSION["u"].".";
return false;

}else if (($np==$np2) && ($np!="")){
include('chgpw.html');
print "Passwords Do Not Match";
return false;

}else{

$u=$_SESSION["u"];
$p=$_SESSION["p"];

if (($np==$np2) && ($np=""))
{
for ($x=0; $x<count($u); $x++)
{
if ($u==$u[$x])
{
$p[$x]=$np;
$_SESSION["p"]=$p;
}
}
Header ("Location:confirmation.php");
}
$fp=fopen("data.txt","w+");
for ($x=0; $x<count($u); $x++)
{
fputs($fp,$u[$x]);
fputs($fp,"\n");
fputs($fp,$p["np"][$x]);
fputs($fp,"\n");
}
fclose($fp);

return false;

jatar_k

7:15 pm on Mar 2, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think there a couple logic issues in your script, some suggestions

$np=$_POST[np];
$np2=$_POST[np2];

should be

$np=trim($_POST[np]);
$np2=trim($_POST[np2]);

since you are making your comparisons against empty string then you should trim them just to be sure they didn't enter only spaces

your 3 comparisons
1. if(($np=="") ¦¦ ($np2=="")){
2. } else if (($np==$np2) && ($np!="")) {
3. if (($np==$np2) && ($np="")) {

1. no problem

2. you don't need the second part of this at all because you have already tested to see if they are empty. The first part seems backwards, I think you meant to be testing for not equal, like so

} else if (($np!=$np2)) {

3. this comparison is actually inside your else and is not necessary as you won't reach your else if either of those are empty.

with those changes that gives me this

session_start(); 
$np=trim($_POST[np]);
$np2=trim($_POST[np2]);
if(($np=="") ¦¦ ($np2=="")){
include('chgpw.html');
print "New Password Field is blank. Please try again".$_SESSION["u"].".";
return false;
} else if (($np==$np2) && ($np!="")) {
include('chgpw.html');
print "Passwords Do Not Match";
return false;
} else {
$u=$_SESSION["u"];
$p=$_SESSION["p"];
for ($x=0; $x<count($u); $x++) {
if ($u==$u[$x]) {
$p[$x]=$np;
$_SESSION["p"]=$p;
}
header("Location:confirmation.php");
}
$fp=fopen("data.txt","w+");
for ($x=0; $x<count($u); $x++) {
fputs($fp,$u[$x]);
fputs($fp,"\n");
fputs($fp,$p["np"][$x]);
fputs($fp,"\n");
}
fclose($fp);
return false;

Also Header should be lower case ie. header. There is one more problem, you are missing a brace. it is inside your else, from there down there is a brace missing though I am not totally sure where it should be, my wild guess would be here

for ($x=0; $x<count($u); $x++) {
if ($u==$u[$x]) {
$p[$x]=$np;
$_SESSION["p"]=$p;
}
} <---- added right before your header call

geo039

1:06 am on Mar 6, 2006 (gmt 0)

10+ Year Member



Okay I got this script to work with only one more snag. When I try to change password that needs to be re-wrote back to the original text file, right now is wiping out my text file blank. So once I go through one round of log ins then change passwords the text file is blank. Here are the two files.

# chgpw.php

session_start();

$np=$_POST[np];
$np2=$_POST[np2];

$u = $_SESSION["u"];
$p = $_SESSION["p"];

$u_list = $_SESSION["u_list"];
$p_list = $_SESSION["p_list"];

if (($np == $np2) && ($np!=""))
{
for ($x=0; $x<count($u_list); $x++)
{
if ($u==$u_list[$x])
{
$p_list[$x]=$np;
$_SESSION["p_list"]=$p_list;
}
}
header ("Location:confirmation.php");
}
else
{
print "<html>";
print "<head>";
print "<title>Change Password Page</title>";
print "<body>";
print "<center>";
print "<h1>Change Password</h1>";
print "<form name='form2' action='chgpw.php' method='post'>";
print "<p>NEW PASSWORD: <input type='text' size='20' name='np'></p>";
print "<p>RE-TYPE PASSWORD: <input type='text' size='20' name='np2'></p>";
print "<input type='submit' value='Submit'>";
print "<input type='button' value='Cancel' onClick='javascript:window.location='main.php'>";
print "</form>";
print "</center>";
print "</body>";
print "</html>";
print "<center><b><font color='red'> **PASSWORDS DO NOT MATCH** </font></b></center>";
}

-----------------------------------------------------
# confirmation.php

session_start();

$u_list = $_SESSION["u_list"];
$p_list = $_SESSION["p_list"];

print "<html>";
print "<head>";
print "<title>Confirmation Page</title>";
print "</head>";
print "<body>";
print "<center>";
print "<h1>Password Update - Successful</h1>";
print "<a href=\"main.php\">Main Page</a> ¦ <a

href=\"a4.html\">Log Out</a>";

$fp = fopen("data.txt", "w");
for ($x=0; $x<count($u_list); $x++)
{
fputs($fp,$u_list[$x]);
fputs($fp,"\n");
fputs($fp,$p_list[$x]);
fputs($fp,"\n");
}
fclose($fp);

print "</body>";
print "</html>";

coopster

2:37 pm on Mar 6, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If your file is being overwritten then you need to look at how you are opening and writing to the file. Files can either be appended to or overwritten. Have a closer look at the list of possible modes for fopen() [php.net].