tberger Posted March 18, 2007 Share Posted March 18, 2007 Currently I have a form that collects basic user information - first and last name, email and password. This is being stored in a txt file. Here is the code that is processing the information into the txt file. <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Create Process</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> </head> <body> <?php //Escapes and enquotes variables function clean($var){ return trim(addslashes($var)); } //Variable declaration $fname = clean($_POST['first_name']); $lname = clean($_POST['last_name']); $email = clean($_POST['email_address']); $pw = clean($_POST['password']); //Declare the file $file = "/staff/seebetra/homepage/ciss225/registered_users.txt"; //Open File in append mode $fp = fopen ($file, "a+"); //Append Data to file fwrite($fp, $email); fwrite($fp, ":"); fwrite($fp, $pw); fwrite($fp, ":"); fwrite($fp, $fname); fwrite($fp, ":"); fwrite($fp, $lname); fwrite($fp, ":"); echo 'Done<br />'; echo $fname; echo ', your account has been created'; // Close the file fclose($fp); ?> </body> </html> Once you are registered, you will be able to log in. Right now once I confirm login data - they will go to a wlecome page as this is a work in progress. The login is process in a login_process.php that will call in the function from a file called library.pinc. The problem that I am having is that it never finds my data. My guess is that it has something to do with how it is being set up in the txt file but I am not sure. I know a database is a better option there but this is what I have to work with at this time. Here is the code for the login_process: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Login Process</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> </head> <body> <?php include('/staff/seebetra/homepage/ciss225/library.pinc'); if(!isset($_POST['submit'])) { getLogin($username, $password); } ?> </body> </html> Here is the function in library.pinc <?php function getLogin($username, $password) { $email = trim($_POST['email']); $pass = trim($_POST['pass']); $file = "/staff/seebetra/homepage/ciss225/registered_users.txt"; $lines = file($file); $logged = FALSE; foreach($lines as $line) { if(preg_match("/^.+?:.+?:".preg_quote($email).":".preg_quote($pass)."$/s", trim($line))) { list($first, $last, $f_email, $f_pass) = explode(":", trim($line)); echo "Hello ".$first." ".$last."!"; $logged = TRUE; break; } } if(!$logged) echo "Sorry, you didn't match!"; } ?> Can anyone help me with this?? Link to comment https://forums.phpfreaks.com/topic/43272-help-withfunction/ Share on other sites More sharing options...
Barand Posted March 18, 2007 Share Posted March 18, 2007 As you expect to read the data as separate lines you need to write it as separate lines. fwrite($fp, $email); fwrite($fp, ":"); fwrite($fp, $pw); fwrite($fp, ":"); fwrite($fp, $fname); fwrite($fp, ":"); fwrite($fp, $lname); fwrite($fp, "\n"); // write a newline at end of each new record or even fwrite ($fp, "$email:$pw:$fname:$lname\n"); Link to comment https://forums.phpfreaks.com/topic/43272-help-withfunction/#findComment-210189 Share on other sites More sharing options...
tberger Posted March 19, 2007 Author Share Posted March 19, 2007 Thanks for your help. I did change the code to write to file to be fwrite ($fp, "$email:$pw:$fname:$lname\n"); but it still is telling me I have not matches when I know I do. I really think the problem is in the function - <?php function getLogin($username, $password) { $email = trim($_POST['email']); $pass = trim($_POST['pass']); $file = "/staff/seebetra/homepage/ciss225/registered_users.txt"; $lines = file($file); $logged = FALSE; foreach($lines as $line) { if(preg_match("/^.+?:.+?:".preg_quote($email).":".preg_quote($pass)."$/s", trim($line))) { list($first, $last, $f_email, $f_pass) = explode(":", trim($line)); echo "Hello ".$first." ".$last."!"; $logged = TRUE; break; } } if(!$logged) echo "Sorry, you didn't match!"; } ?> Any other suggestions? Link to comment https://forums.phpfreaks.com/topic/43272-help-withfunction/#findComment-210474 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.