uhplifted Posted December 3, 2014 Share Posted December 3, 2014 I have a user registration page that requires the user to input a Username, Password, Confirm Password, Email. If the user passes all the validation requirements for the new account, I then need to have the username, password, email fields saved to a file called 'login.dat' Here is my code that I have so far, it runs perfectly. <?php include 'helpfulfunctions.inc'; include 'productsdata.inc'; $user_login_file = 'login.dat'; //var_dump($_POST); // product data for photo, name, and price. $alluserinfo = load_users_info($user_login_file); //validate users info $errors = array(); if (array_key_exists('register_submit', $_POST)) { //check to see if username is taken $username_entered = $_POST['username']; //check to see if username already exists if (array_key_exists($username_entered, $alluserinfo)) { $errors['username']['username_exists'] = "Username already exists."; } //validate username is 4-11 characters long using only a-z A-Z 0-9 if(preg_match("/^[0-9a-zA-z]{4,11}$/",$_POST['username']) ===0){ $errors['username']['invalid_username']= "Invalid username. Username must be 4-11 characters long and use only letters and numbers."; } //validate password "." means any character //.* allows numbers 0-9 to be inserted anywhere //?= positive lookahead: next text must be like this and follow these rules // must be at least 6 characters, contain 0-9, a-z, A-Z $pw_entered=$_POST['password']; if(preg_match("/^.*(?=.{6,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $_POST["password"]) === 0){ $errors['password']['invalid_password']="Password must be at least 6 characters and must contain at least one lower case letter, one upper case letter and one digit."; } //validate that "confirm password" matches password above $pw_repeat=$_POST['confirmpassword']; if($pw_repeat != $pw_entered){ $errors['confirmpassword']['pw_no_match']="Passwords do not match. Try again."; } //validate email format $email_entered=($_POST['email']); if(!filter_var($email_entered, FILTER_VALIDATE_EMAIL)){ $errors['email']['invalid_email']="Not a valid email. Please try again."; } //no validation errors=>print invoice if (empty($errors)) { include 'invoice.inc'; exit; } } //reprint if invalid entry. if no errors print invoice ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <h3>Please register to continue.</h3> <table> <tr> <td> *Username: </td> <td> <input type="text" name="username"/> <?php if (isset($errors['username'])) { print implode('<br>', $errors['username']); } ?> </td> </tr> <tr> <td> *Password: </td> <td> <input type="password" name="password"/> <?php if (isset($errors['password'])) { print implode('<br>', $errors['password']); } ?> </td> </tr> <tr> <td> *Confirm Password: </td> <td> <input type="password" name="confirmpassword"/> <?php if (isset($errors['confirmpassword'])) { print implode('<br>', $errors['confirmpassword']); } ?> </td> </tr> <tr> <td> *Email: </td> <td> <input type="text" name="email"/> <?php if (isset($errors['email'])) { print implode('<br>', $errors['email']); } ?> </td> </tr> <tr> <td> * required info <br> <input type="submit" value="Register" name="register_submit"> <?php //print out hiddens with quantities save_hidden_qty($_POST['quantity']); ?> </form> </td> </tr> </table> In case it's needed, this is the 'helpfulfunctions.inc' file and the included functions: <?php if (!function_exists('load_users_info')) { function load_users_info($users_data_file) { $fp = fopen($users_data_file, 'r'); //read all lines of login.dat file and create user info arrays while (!feof($fp)) { $users_info_line = fgets($fp); $user_info_parts = explode(',', $users_info_line); $user_info_array = array('username' => $user_info_parts[0], 'password' => $user_info_parts[1], 'email' => $user_info_parts[2]); $complete_user_info_array[$user_info_array['username']] = $user_info_array; } fclose($fp); return $complete_user_info_array; } } // function to display products if (!function_exists('display_products')) { function display_products($products_to_display, $quantities = array()) { global $errors; ?> <table border="1"> <tbody> <tr> <td style="text-align: center;"><b><big>Product</big></b></td> <td style="text-align: center;"><b><big>Brand</big></b></td> <td style="text-align: center;"><b><big>Price(each)</big></b></td> <td style="text-align: center;"><b><big>Quantity Desired</big></b></td> </tr> <?php // quantities are 0 unless already inputted, if quantities previously were inputted, return the values. // input boxes for ($i = 0; $i < count($products_to_display); $i++) { if (empty($quantities)) { $qty = isset($_POST['quantity'][$i]) ? $_POST['quantity'][$i] : 0; $qty_str = "<input type=text size=3 maxlength=3 name='quantity[$i]' value='$qty'>"; if (isset($errors['quantity'][$i])) { $qty_str .= "<span style='font-style:italic;font-size:8px;color:red;'>{$errors['quantity'][$i]}</span>"; } } else { $qty_str = $quantities[$i]; } // loop to print out table of photo of board, name of the brand, price, and quantity selected printf(' <tr> <td><img alt="Small" id="lightboxImage" style="width: 119px; height: 88px; bgcolor="#cccccc;" src="http://imgur.com/%s" height="300" width="300"></td> <td style="text-align: center;">%s</td> <td style="text-align: center;">$%.2f</td> <td style="text-align: center;">' . $qty_str . '</td> </tr> ', $products_to_display[$i]['item'], $products_to_display[$i]['board'], $products_to_display[$i]['price']); } ?> <tr><td colspan="4" style="text-align: right; border: none"> <input type="submit" value="Purchase"></td></tr> </tbody> </table> <?php } } if (!function_exists('save_hidden_qty')) { function save_hidden_qty($the_quantities){ foreach ($the_quantities as $key=>$value){ print "<input type='hidden' name='quantity[$key]' value='$value'>\n"; } } } ?> Can anyone help me out? Quote Link to comment Share on other sites More sharing options...
mogosselin Posted December 3, 2014 Share Posted December 3, 2014 If it works perfectly, where is the problem then? What do you have trouble with? You don't know where to start? With just one line of code you can append a line in a file. Checkout: http://php.net/manual/en/function.file-put-contents.php You need to use the FILE_APPEND flag. Like: file_put_contents ("file.dat" , $linetoadd , FILE_APPEND); Quote Link to comment Share on other sites More sharing options...
uhplifted Posted December 3, 2014 Author Share Posted December 3, 2014 If it works perfectly, where is the problem then? What do you have trouble with? You don't know where to start? With just one line of code you can append a line in a file. Checkout: http://php.net/manual/en/function.file-put-contents.php You need to use the FILE_APPEND flag. Like: file_put_contents ("file.dat" , $linetoadd , FILE_APPEND); My problem is writing the user inputted data from the username, password, email form to a .dat file. I was finally able to get code that writes to the file, the only issue is it keeps repeating itself. I'm using this code below to write to my login.dat file $blank=$_POST['register_submit']; if(isset($_POST['register_submit'])){ $un=$_POST['username']; $pwd=$_POST['password']; $email=$_POST['email']; $blank=array($un,$pwd,$email); $text= "\n" . $un ."," . $pwd . "," . $email; $fp=fopen('login.dat','a'); if(fwrite($fp,$text)) fclose($fp); Quote Link to comment Share on other sites More sharing options...
CroNiX Posted December 3, 2014 Share Posted December 3, 2014 Why not use a database for this? Quote Link to comment Share on other sites More sharing options...
uhplifted Posted December 3, 2014 Author Share Posted December 3, 2014 Why not use a database for this? It's for a school assignment, we haven't covered data bases yet. Also, before I get chastised for just looking for answers, I've spent numerous hours on this, and we are allowed to get outside help. I have figured out how to write my data however to a file. Quote Link to comment Share on other sites More sharing options...
Alex_ Posted December 4, 2014 Share Posted December 4, 2014 What do you mean "keep repeating itself", is the fwrite function called more than once? Also not sure why you have the fwrite function in an if-statement prior to closing the file. The file pointer should always be closed, regardless of successfully writing or not. Quote Link to comment 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.