tberger Posted March 15, 2007 Share Posted March 15, 2007 I am really new to PHP and my first projects are working with PHP and txt files - I am not yet using MYSQL. I created a registeredusers.txt file that contains user names, emails, and passwords. The users will then fill in a login in page with their email address and password. This form will them process so that another PHP file will open and be available to them. I am having trouble writing the login script that will check the txt file to confirm they have a email address and password on file. I am being told to write it in a function that will be placed in a pinc file that will be included on my loginprocess page. Are there any tutorials that help with writing login functions that work with txt files rather than databases or can someone help me write a function that will work? I appreciate any help. Quote Link to comment https://forums.phpfreaks.com/topic/42841-login-help/ Share on other sites More sharing options...
Orio Posted March 15, 2007 Share Posted March 15, 2007 Can you show how you are storing the info in the txt file? (Show us the registration script or a couple of lines from the text file) Orio. Quote Link to comment https://forums.phpfreaks.com/topic/42841-login-help/#findComment-207959 Share on other sites More sharing options...
tberger Posted March 15, 2007 Author Share Posted March 15, 2007 Sure - It is being stored like this firstname:lastname:email:password Here is the code that is putting the data into the registeredusers file: <?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); echo 'Done'; // Close the file fclose($fp); ?> Thanks for looking at this! TJ Quote Link to comment https://forums.phpfreaks.com/topic/42841-login-help/#findComment-207963 Share on other sites More sharing options...
Orio Posted March 15, 2007 Share Posted March 15, 2007 Something like this should do it... I think there shouldn't be a problem. <?php if(!isset($_POST['submit'])) { echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">"; echo "Your Email: <input type=\"text\" name=\"email\"><br>"; echo "Your Pass: <input type=\"password\" name=\"pass\"><br>"; echo "<input type=\"submit\" name=\"submit\" value=\"Login!\">"; echo "</form>"; die; } $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!"; ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/42841-login-help/#findComment-207966 Share on other sites More sharing options...
tberger Posted March 15, 2007 Author Share Posted March 15, 2007 Thanks for the help, I am going to try that. Do you know is there a way I could write it in a separate function? Something that leads with function getLogin($username, $password) to get the login information? Quote Link to comment https://forums.phpfreaks.com/topic/42841-login-help/#findComment-208004 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.