Hi good day. i am designing a little project for school. This project is to register the MAC address of the laptops of every student in the school.
My problem is, when i put in the student's MAC address in the textbox, i would like the little search algorithm i got to revise the text file to see if the MAC address is already registered.
I am facing problems with the textbox content to do the search.

Contains two files : getdata.php

<?php
$hoy=date("d-m-y");
?>

<html> 
  <head> 
    <meta http-equiv="content-type" content="text/html; charset=windows-1250">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title> Register MAC </title> 
  </head> 
  <body bgcolor="#ffffff"> 
  
    <form action="savedata.php" method="post"> 
    <table class="fd">
      <tr><td>Identification No.: <input type="text" name="ncontrol" MAXLENGTH ="8" size="30"><br></td></tr>
      <tr><td>MAC Address:<input type="text" name="mac1" size = "1" MAXLENGTH="2" class="macs">:
                          <input type="text" name="mac2" size = "1" MAXLENGTH="2" class="macs">:
                          <input type="text" name="mac3" size = "1" MAXLENGTH="2" class="macs">:
                          <input type="text" name="mac4" size = "1" MAXLENGTH="2" class="macs">:
                          <input type="text" name="mac5" size = "1" MAXLENGTH="2" class="macs">:
                          <input type="text" name="mac6" size = "1" MAXLENGTH="2" class="macs"></td>
      </tr>
      <tr><td>Date Registered: <?php echo "<input type='text' name='hoy' id='hoy' value='$hoy'>"; ?></td></tr>
    </table>
      <input type="submit" value="Save>>"> 
    </form> 
  </body> 
</html> 


<?php

?>

and savedata.php

<?php 
$Ncontrol = @$_POST["ncontrol"]; 
$Fdate = @$_POST["hoy"]; 
$dMAC = @$_POST["mac1"];
$dMAC1 = @$_POST["mac2"];
$dMAC2 = @$_POST["mac3"];
$dMAC3 = @$_POST["mac4"];
$dMAC4 = @$_POST["mac5"];
$dMAC5 = @$_POST["mac6"];
// Set the string to be written to the file 
//save numero de control
$values = "$Ncontrol \t\t\t\t\t\t\t\t\t"; 
//save MAC 
$values .= "$dMAC:$dMAC1:$dMAC2:$dMAC3:$dMAC4:$dMAC5 \t\t\t\t";
//save date
$values .= "$Fdate\r\n"; 

// Open the file for reading and writing 
$fp = @fopen("MACS.data", "a+") or die("CANNOT OPEN THE FILE MACS.daTA!"); 
$numBytes = @fwrite($fp, $values) or die("CANNOT WRITE TO THE FILE!");
$people = file("MACS.data");

//THE SEARCH ALGORITHM. TO REVISE IF THE MAC ADDRESS IS ALREADY IN THE TEXTFILE
for ($posn=0; $posn < count($people); $posn++) 
        if (ereg("PHP",$people[$posn])) {
                print ("Person $posn is $people[$posn]<br>");
                }
        }   
@fclose($fp);
?>

.
I am using the MAC.data to save the results (as a txt file)

Recommended Answers

All 5 Replies

Your better off doing this in a database, easier to index and search. But if you want to search through a text file, you'll need to open the text file for reading, read in the content of the file, then use strrpos() to find the position of the MAC address, or it will return FALSE if its not in there.

You could also do a grep for it, using the system() command in php. That assumes your webhost allows the system command.

hope that helps.

hi, i am not using a database because with the MAC address, i will be working with a proxy server(squid) to validate the MAC addresses so that every person can nagvigate automatically once registered. That's the reason why i am using the text file.

Ok. i can use one of the fuctions u told me about but how can i link the text in the textbox to the functions? How can i assign the text given as a variable for use of the function??

before you write the file (your fwrite statement) but after you open the file for readhing and writing try this:

$filecontents = fread($fp, filesize("MACS.data")); 
if(strpos($filecontents,"$dMAC:$dMAC1:$dMAC2:$dMAC3:$dMAC4:$dMAC5") === FALSE){
//store this MAC address, it's unique
}else{
//you already have this one
}

notice the 3 equal signs. that's intentional. strpos() will return 0 (int) if the string your searching for starts at the first position in the string, 2 equal signs like ==false will cause a false positive, because 0==false is true, however 0===false is not.

ok. that worked perfectly. thanks a lot.
I got another question. i am now trying to go back to getdata.php if the MAC address already exists. how can i do that?

<?php 

$Ncontrol = @$_POST["ncontrol"]; 
$Fdate = @$_POST["hoy"]; 
$dMAC = @$_POST["mac1"];
$dMAC1 = @$_POST["mac2"];
$dMAC2 = @$_POST["mac3"];
$dMAC3 = @$_POST["mac4"];
$dMAC4 = @$_POST["mac5"];
$dMAC5 = @$_POST["mac6"];
// Set the string to be written to the file 
//save numero de control
$values = "$Ncontrol \t\t\t\t\t\t\t\t\t"; 
//save MAC 
$values .= "$dMAC:$dMAC1:$dMAC2:$dMAC3:$dMAC4:$dMAC5 \t\t\t\t";
//save date
$values .= "$Fdate\r\n"; 


   /*
 
  if ( (!$Ncontrol)||(!$Fdate)||(!$dMAC) ||(!$dMAC1)||(!$dMAC2)||(!$dMAC3)||(!$dMAC4)||(!$dMAC5)){ 
    echo "Revise lo siguiente por favor:<BR>"; 
    if(!$Ncontrol){ 
        echo "Ingrese el Numero de Control Por favor<BR>"; 
    }
    if(!$Fdate){ 
        echo "Fecha esta vacio!<BR>"; 
    } 
    if(!$dMAC){ 
        echo "MAC 1 vacio<BR><BR>"; 
    } 
    if(!$dMAC1){ 
        echo "MAC 2 vacio<BR><BR>"; 
    } 
    if(!$dMAC2){ 
        echo "MAC 3 vacio<BR><BR>"; 
    } 
    if(!$dMAC3){ 
        echo "MAC 5 vacio<BR><BR>"; 
    } 
    if(!$dMAC4){ 
        echo "MAC 6 vacio<BR><BR>"; 
    } 
    if(!$dMAC5){ 
        echo "MAC 7 vacio<BR><BR>"; 
    } 
  }else{ */
  // Open the file for reading and writing 
  $fp = @fopen("MACS.data", "a+") or die("No puede abrir MAC.data para escribir!");
  $filecontents = fread($fp,filesize("MACS.data")); 
  if(strpos($filecontents,"$dMAC:$dMAC1:$dMAC2:$dMAC3:$dMAC4:$dMAC5")===FALSE){
      $numBytes = @fwrite($fp, $values) or die("No puede escribir al archivo!");
  }else{
       echo("<SCRIPT LANGUAGE='JavaScript'>
  window.alert('THIS MAC ADDRESS IS ALREADY REGISTERED!!')
  </SCRIPT>"); 
  }
/*}  */
@fclose($fp);
?>

use header(). Make sure you don't have any output on your savedata.php.

header("Location: getdata.php");

If you need to pass back a message you could use session variables, or pass information on the query string. like getdata.php?error=duplicate+MAC

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.