Jump to content

Register....


Sarkara

Recommended Posts

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]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
}

?>

 

 

 

 

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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);
                  
                  
         }                 
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

... 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...

Link to comment
Share on other sites

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;

}

 

Link to comment
Share on other sites

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... >.<

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.