Sarkara Posted September 11, 2009 Share Posted September 11, 2009 Ok guys i have a problem, i'm working on a php excersise at school and i'm having a hard time figuring it out. I'm writing a program that when a user comes to register to your site it takes the name, runs through a series of checks and then compares it to the names in my txt file. after if all goes smoothly it goes to the second php which will add the user to the txt file. But it doesn't seem to be entering. i've attached the 2 php files and txt file [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
TheJoey Posted September 11, 2009 Share Posted September 11, 2009 should just add them to code tags "[" code ][" /code]" Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 11, 2009 Share Posted September 11, 2009 Try the following code: function checkFile($name){ $file = file("users.txt"); foreach($file as $line){ $line = trim($line); list($username,$passwords) = explode('#',$line); if(strcasecmp($name,$username)==0){return true;} } return false; } Quote Link to comment Share on other sites More sharing options...
Sarkara Posted September 11, 2009 Author Share Posted September 11, 2009 Ok first code is my adduser.php second is the register.php <?php function checkRegister($newName, $newPassword) { $new_login_pattern = "/^[\da-zA-Z]{4,16}$/"; $new_pass_pattern = "/^[\da-zA-Z_]{8,12}$/"; if(empty($newName) && empty($newPassword)) { print "please enter a user name and password"; return false; } else { if(!preg_match($new_login_pattern,$newName) || !preg_match($new_pass_pattern,$newPassword)) { print "Your user name must be 4-16 characters long <br />"; print "Your password must be 8-12 characters long"; return false; } else { return true; } } } $match=true; function matchCheck($exsists) { $match=true; $user_check = fopen('users.txt','r'); while(!feof($user_check) && $match==true) { $allready_user = fgets($user_check); $allready_user = explode('#', $allready_user); if($exsists == $allready_user[0]) { print "sorry but this user name all ready exsists"; return false; } } if($match == false) { return true; } else { print "sorry we cannot add you at this time"; } fclose($user_check); } ?> <?php if(isset($_POST['fuser']) && isset($_POST['fpassword'])) { include("adduser.php"); $new_login = $_POST['fuser']; $new_pass = $_POST['fpassword']; $new_login = trim($new_login); $new_pass = trim($new_pass); $match=true; $match_call=""; $check_call = checkRegister($new_login, $new_pass); if($check_call == true) { $match_call = matchCheck($new_login); } if($match_call == true) { $add_user = fopen('users.txt','a'); fwrite($add_user,"\n". $new_login . '#' . $new_pass); print "thank you for signing up"; fclose($add_user); } } ?> I hope this helps a bit more Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 11, 2009 Share Posted September 11, 2009 Change this: $check_call = checkRegister($new_login, $new_pass); if($check_call == true) { $match_call = matchCheck($new_login); } if($match_call == true) { $add_user = fopen('users.txt','a'); fwrite($add_user,"\n". $new_login . '#' . $new_pass); print "thank you for signing up"; fclose($add_user); } To: if(checkRegister($new_login, $new_pass) && checkFile($new_login)) { $add_user = fopen('users.txt','a'); fwrite($add_user,"\n". $new_login . '#' . $new_pass); print "thank you for signing up"; fclose($add_user); } It's much shorter and uses the function I provided earlier for checking file. Quote Link to comment Share on other sites More sharing options...
Sarkara Posted September 11, 2009 Author Share Posted September 11, 2009 ok i've done what you said but i'm getting a Notice: Undefined offset: 1 in /var/www/web13/patrick/cgi/lab5/adduser.php on line 10 again code 1 is register.php and code 2 adduser.php <?php if(isset($_POST['fuser']) && isset($_POST['fpassword'])) { include("adduser.php"); $new_login = $_POST['fuser']; $new_pass = $_POST['fpassword']; $new_login = trim($new_login); $new_pass = trim($new_pass); $match=true; $match_call=""; $check_call = checkRegister($new_login, $new_pass); /*if($check_call == true) { $match_call = matchCheck($new_login); } if($match_call == true) { $add_user = fopen('users.txt','a'); fwrite($add_user,"\n". $new_login . '#' . $new_pass); print "thank you for signing up"; fclose($add_user); }*/ } if(checkRegister($new_login, $new_pass) && checkFile($new_login)) { $add_user = fopen('users.txt','a'); fwrite($add_user,"\n". $new_login . '#' . $new_pass); print "thank you for signing up"; fclose($add_user); } ?> <?php function checkFile($name){ $file = file("users.txt"); foreach($file as $line){ $line = trim($line); list($username,$passwords) = explode('#',$line); if(strcasecmp($name,$username)==0){return true;} } return false; } function checkRegister($newName, $newPassword) { $new_login_pattern = "/^[\da-zA-Z]{4,16}$/"; $new_pass_pattern = "/^[\da-zA-Z_]{8,12}$/"; if(empty($newName) && empty($newPassword)) { print "please enter a user name and password"; return false; } else { if(!preg_match($new_login_pattern,$newName) || !preg_match($new_pass_pattern,$newPassword)) { print "Your user name must be 4-16 characters long <br />"; print "Your password must be 8-12 characters long"; return false; } else { return true; } } } $match=true; function matchCheck($exsists) { $match=true; $user_check = fopen('users.txt','r'); while(!feof($user_check) && $match==true) { $allready_user = fgets($user_check); $allready_user = explode('#', $allready_user); if($exsists == $allready_user[0]) { print "sorry but this user name all ready exsists"; return false; } } if($match == false) { return true; } else { print "sorry we cannot add you at this time"; } fclose($user_check); } ?> Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 11, 2009 Share Posted September 11, 2009 Undefined offset is what happens when the line is null... You can either make sure there are no null lines in users.txt, and change the function like so: function checkFile($name){ $file = file("users.txt"); foreach($file as $line){ $line = trim($line); if(!empty($line)){ list($username,$passwords) = explode('#',$line); if(strcasecmp($name,$username)==0){return true;} } } return false; } Quote Link to comment Share on other sites More sharing options...
Sarkara Posted September 11, 2009 Author Share Posted September 11, 2009 Ok that takes care of the error. but i'm still having the same problem. its seems to be skipping in the adduser.php if($match == false) { return true; } else { print "sorry we cannot add you at this time"; } fclose($user_check); and in the register.php its skipping if(checkRegister($new_login, $new_pass) && checkFile($new_login)) { $add_user = fopen('users.txt','a'); fwrite($add_user,"\n". $new_login . '#' . $new_pass); print "thank you for signing up"; fclose($add_user); } } Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 11, 2009 Share Posted September 11, 2009 What do you mean by skipping? matchCheck shouldn't be there anymore if all it does is check the file... that's what checkFile does. Quote Link to comment Share on other sites More sharing options...
Sarkara Posted September 11, 2009 Author Share Posted September 11, 2009 by skipping i mean its not going into either if in the register.php or adduser.php. the matchCheck i commented out to test out the code you gave me but there one thing i don't understand in teh check file function you gave me function checkFile($name){ $file = file("users.txt"); foreach($file as $line){ $line = trim($line); if(!empty($line)){ list($username,$passwords) = explode('#',$line);<-- what are $username,$passwords if(strcasecmp($name,$username)==0){return true;} } } return false; } and the matchCheck function is also checking to see if the username already exsists in the txt file Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 11, 2009 Share Posted September 11, 2009 The users.txt is split into (username)#(password) for each line.. we're just splitting this up using the list definition. Quote Link to comment Share on other sites More sharing options...
Sarkara Posted September 11, 2009 Author Share Posted September 11, 2009 ok but should i add some code to check to see if the user name already exsists? and it still isn't seeing the if to add the user to the txt file. Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 11, 2009 Share Posted September 11, 2009 What do you mean if to add? Check to see if they can register, check to see if username doesn't already exist, then write to the file. Quote Link to comment Share on other sites More sharing options...
Sarkara Posted September 11, 2009 Author Share Posted September 11, 2009 well its simple the function is supposed to check to see if the exsist and if they don't it will add the username and password to the user.txt file which is then used by my login php. so i added this <?php function checkFile($name){ $file = file("users.txt"); foreach($file as $line){ $line = trim($line); if(!empty($line)){ list($username,$passwords) = explode('#',$line); if(strcasecmp($name,$username)==0){return true;} } if($match == true;) { $user_check = fopen('users.txt','r'); while(!feof($user_check) && $match==true) { $allready_user = fgets($user_check); $allready_user = explode('#', $allready_user); if($exsists == $allready_user[0]) { print "sorry but this user name all ready exsists"; return false; } } } } return false; } Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 11, 2009 Share Posted September 11, 2009 ... the checkFile($username) will return true or false depending on whether the username exists in the file.. or not. So that's where if(checkRegister($new_login, $new_pass) && checkFile($new_login)) { $add_user = fopen('users.txt','a'); fwrite($add_user,"\n". $new_login . '#' . $new_pass); print "thank you for signing up"; fclose($add_user); } } Adds the user in... Quote Link to comment Share on other sites More sharing options...
Sarkara Posted September 11, 2009 Author Share Posted September 11, 2009 ok so i added this then Code: [select] <?php function checkFile($name){ $file = file("users.txt"); foreach($file as $line){ $line = trim($line); if(!empty($line)){ list($username,$passwords) = explode('#',$line); if(strcasecmp($name,$username)==0){return true;} } if($match == true;) { $user_check = fopen('users.txt','r'); while(!feof($user_check) && $match==true) { $allready_user = fgets($user_check); $allready_user = explode('#', $allready_user); if($exsists == $allready_user[0]) { print "sorry but this user name all ready exsists"; return false; } } } } return false; } Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 11, 2009 Share Posted September 11, 2009 You don't need this: if($match == true;) { $user_check = fopen('users.txt','r'); while(!feof($user_check) && $match==true) { $allready_user = fgets($user_check); $allready_user = explode('#', $allready_user); if($exsists == $allready_user[0]) { print "sorry but this user name all ready exsists"; return false; } That's covered by checkFile... >.< Quote Link to comment Share on other sites More sharing options...
Sarkara Posted September 11, 2009 Author Share Posted September 11, 2009 ahh perfect thank you very much now it is wroking. it is now working i'm only now having a small error with a error repeating 1 time to many but i can fix that. Cheers 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.