Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a PHP Expert!

Join 244,283 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,041 people online right now. Registration is fast and FREE... Join Now!




php script to verify user data

 
Reply to this topicStart new topic

php script to verify user data

tiff88
1 Dec, 2008 - 06:12 PM
Post #1

D.I.C Head
**

Joined: 9 Jul, 2008
Posts: 54



Thanked: 1 times
My Contributions
Hi all,
I have the following problem. I wrote some code including a php script that is supposed to compare user input with the content of a file. Specifically, the user enters an user id and submits it, the php script opens a provided file and compares the id submitted by the user to all ids contained in the file. If the script finds a match, it will grant access to the user. Below is the code I wrote. For some reason it won't work for me. Any help or advice is highly appreciated.
Thanks,
Tiff

CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns = "http://www.w3.org/1999/xhtml">
   <head>
      <?php
         extract( $_POST );

         if ( !$inputbox ) {
            fieldsBlank();
            die();
         }

             $file = fopen( "idnumbers.txt", "r" ) or die("Could not open file");
          
          

            $wVerified = 0;

            while ( !feof( $file ) && !$wVerified ) {

               // read line from file
               $line = fgets( $file, 255 );
                 
  

                  if ( checkID( $inputbox, $line )
                     == true )
                     accessGranted( $inputbox );
                  else  
                     invalidWNumber();
               }
            }
  
            // close text file
            fclose( $file );
         }

          // verify user password and return a boolean
         function checkID( $inputbox, $filedata )
         {
            if ( $inputbox == $filedata[ 1 ] )
               return true;
            else
               return false;
         }






          // print a message indicating access has been denied
         function accessDenied()
         {
            print( "<title>Access Denied</title></head>
               <body style = \"font-family: arial;
               font-size: 1em; color: red\">
               <strong>
               You were denied access to this server.
               <br /></strong>" );
         }

          // print a message indicating that fields
          // have been left blank
         function fieldsBlank()
         {
            print( "<title>Access Denied</title></head>
               <body style = \"font-family: arial;
               font-size: 1em; color: red\">
               <strong>
               Please fill in all form fields.
               <br /></strong>" );
         }
      ?>
   </body>
</html>


CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Verify ID</title>

</head>

<body>

    <form name="enterID" action="IDnumber.php" method="post">
    Please enter your ID-number below: <BR />
    <input type="text" name="inputbox" id="inputbox" maxlength="8"/>
    <input type="submit" name="submit" id="submit" value="Submit"/>
    </form>

</body>
</html>




User is offlineProfile CardPM
+Quote Post


CTphpnwb
RE: Php Script To Verify User Data
1 Dec, 2008 - 07:51 PM
Post #2

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 1,570



Thanked: 94 times
Dream Kudos: 100
Expert In: PHP

My Contributions
That seems like a lot of effort just to go through a file. I prefer to load the whole file and loop through it:
CODE
<?php
$mynames = readmyfile("names.txt");
$myarray = explode ("\n",$mynames);

$thename = "jim"; // I didn't feel like recreating the user input!
$letin = false;
foreach ($myarray as $n)
    {
    if ($n == $thename)
        {
        $letin = true;
        }
    }
if ($letin)
    {
    echo "You are a valid user.";
    } else
    {
    echo  "Sorry.";
    }

function readmyfile($thefile)
        {    
        $myfile=fopen($thefile,"r");
        $x = fread($myfile, filesize($thefile));
        fclose($myfile);
        return $x;
        }
?>




User is offlineProfile CardPM
+Quote Post

tiff88
RE: Php Script To Verify User Data
2 Dec, 2008 - 02:43 PM
Post #3

D.I.C Head
**

Joined: 9 Jul, 2008
Posts: 54



Thanked: 1 times
My Contributions
Hi,

I made some changes to the code and it seems to be working fine. However, now I ran into another problem. I want to include yet another form in my php file. I have done some research on including html elements in php files and it said you just have to close the php script and open it back up after the html element. I have done that below, but I am getting an error when I am trying to run it. My question is, did I close the php script at the wrong point or why isn't it working Any advice is highly appreciated.

Thanks

CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns = "http://www.w3.org/1999/xhtml">
   <head>
      <?php
         extract( $_POST );

         if ( !$inputbox ) {
            fieldsBlank();
            die("yeeeeeeeehhaaaaaa");
         }

             $file = fopen( "idnumbers.txt", "r" ) or die("Could not open file");
          
          

            $wVerified = 0;

            while ( !feof( $file ) && !$idVerified ) {

               // read line from file
               $line = fgets( $file, 255 );
            echo $line;
            echo $inputbox;
                $line = chop( $line );
  

                  if ($inputbox==$line){
             echo "ACCESS GRANTED";
             $idVerified = 1;
        ?>


                    <form action="register.php" method="POST">
                        First Name
                        <input type="text" />
                        </form>
                  


    <?php                
                  } else{  
              print "HUIUBABABABA";
                    echo "ID-number is invalid";
               }
           }
  
            // close text file
            fclose( $file );


          // print a message indicating that fields
          // have been left blank
         function fieldsBlank()
         {
            print( "<title>Access Denied</title></head>
               <body style = \"font-family: arial;
               font-size: 1em; color: red\">
               <strong>
               Please fill in all form fields.
               <br /></strong>" );
         }

    
      
      ?>

    
   </body>
</html>



CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>title</title>
<script type="text/javascript">
function verifyID(){
  if(document.enterW.inputbox.value.length<9){
     window.alert("Please enter your complete IDnumber.");
     document.enterID.inputbox.value.length.focus();
     return;
     }
     return;
     }
</script>
</head>

<body>

    <form name="enterID" action="IDnumber.php" method="post">
    Please enter your IDnumber below: <BR />
    <input type="text" name="inputbox" id="inputbox" maxlength="9"/>
    <input type="submit" name="submit" id="submit" value="Submit"/>
    </form>

</body>
</html>


User is offlineProfile CardPM
+Quote Post

ericr2427
RE: Php Script To Verify User Data
2 Dec, 2008 - 04:04 PM
Post #4

D.I.C Regular
Group Icon

Joined: 1 Dec, 2008
Posts: 255



Thanked: 17 times
Dream Kudos: 125
My Contributions
You said you get an error. Can you post the error message here?
User is offlineProfile CardPM
+Quote Post

tiff88
RE: Php Script To Verify User Data
2 Dec, 2008 - 04:27 PM
Post #5

D.I.C Head
**

Joined: 9 Jul, 2008
Posts: 54



Thanked: 1 times
My Contributions
QUOTE(ericr2427 @ 2 Dec, 2008 - 04:04 PM) *

You said you get an error. Can you post the error message here?

Parse error: syntax error, unexpected '<' in idnumber.php on line 35

User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: Php Script To Verify User Data
2 Dec, 2008 - 04:52 PM
Post #6

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 1,570



Thanked: 94 times
Dream Kudos: 100
Expert In: PHP

My Contributions
That appears to be around your form, but I don't see where the problem is, so I'd try echoing it instead of breaking out of php:
CODE
echo '<form action="register.php" method="POST">First Name<input type="text" /></form>';


User is offlineProfile CardPM
+Quote Post

midasxl
RE: Php Script To Verify User Data
31 Dec, 2008 - 08:59 AM
Post #7

D.I.C Head
**

Joined: 3 Dec, 2008
Posts: 105



Thanked: 1 times
My Contributions
Hi, thanks for the cool loop script. I have tried to implement it into my project and when I do I get the following error:

Fatal error: Call to undefined function readmyfile() in check.php on line 27

Line 27 is $mynames = readmyfile("data.txt");

Thanks for any help you may be able to provide!

QUOTE(CTphpnwb @ 1 Dec, 2008 - 07:51 PM) *

That seems like a lot of effort just to go through a file. I prefer to load the whole file and loop through it:
CODE
<?php
$mynames = readmyfile("names.txt");
$myarray = explode ("\n",$mynames);

$thename = "jim"; // I didn't feel like recreating the user input!
$letin = false;
foreach ($myarray as $n)
    {
    if ($n == $thename)
        {
        $letin = true;
        }
    }
if ($letin)
    {
    echo "You are a valid user.";
    } else
    {
    echo  "Sorry.";
    }

function readmyfile($thefile)
        {    
        $myfile=fopen($thefile,"r");
        $x = fread($myfile, filesize($thefile));
        fclose($myfile);
        return $x;
        }
?>



User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 02:48PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month