Welcome to Dream.In.Code
Getting PHP Help is Easy!

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




Disposable Password

 
Reply to this topicStart new topic

Disposable Password

jmanuelh
post 22 Jul, 2007 - 09:46 PM
Post #1


New D.I.C Head

Group Icon
Joined: 18 Nov, 2006
Posts: 23



Thanked 2 times
My Contributions


Hey guys!

I'm working on a script which will validate a password and when used the password would be deleted. I will write the passwords on a .txt file and the script will look for the password in the file, and if the given password is found there, then this pass is gonna be deleted and you will be redirected to a url.

How do i compare every line in my .txt file with the string given by the user?? and then delete the used password??

This is what i have so far:

CODE

<?php
  
      //Inicio de variables | Name variables
      $path = "http://www.pr......com/file.txt";//File path
      $nada = " ";
      $contador = 0;
      //Abrir archivo| Open File
      $fp = fopen($path, "r");
      
      //Se lee linea por linea | Read lines ---> heres my problem i think i have to use something diferent
      while($linea = fgets($fp, 1024))
      {
         //Se compara la linea en gestión con la contraseña dada | Here the pass given is compared with the line
         if ($linea = $cadena)//Si se encuentra la contraseña
         {
            //Se borra la contraseña dada para que no pueda ser utilizada de nuevo | Pass is deleted
            $linea = str_replace($cadena,"$nada",$linea);
            //Se manda a la pagina deseada || Redirects
            header("Location: http://pr.....com/valid.html");
            
         }
               
              
      }
    
        
  
  

?>




*NOTE: I'm a php begginer


Thanks


J. Manuel

This post has been edited by jmanuelh: 23 Jul, 2007 - 07:21 AM
User is offlineProfile CardPM

Go to the top of the page

Styx
post 24 Jul, 2007 - 09:45 AM
Post #2


D.I.C Head

Group Icon
Joined: 4 Mar, 2007
Posts: 192



Dream Kudos: 225
My Contributions


Well first of all, that is a horrible idea, storing all your passwords in a text file that anybody can read. Even if not just anybody can read it, it's still a horrible idea.

But if you're intent on doing it that way, use file to get every line of the file into an array (assuming you put one password per line). You might need to do array_map('trim', $your_array) to trim off new lines. Then, with the password entered which you want to check, use in_array to check if the password is in the array of lines.

To delete, look into more array functions such as array_splice or a combination of array_search and unset to remove the value from the array, and implode to return the array to a big string. Storing it back to the file would use either fopen with PHP 4 or file_put_contents with PHP 5.

I would rather recommend using a database such as MySQL to store your passwords. If you do that, it's easy to just do a query for the password which can easily determine if it exists. Read more at php.net

This post has been edited by Styx: 24 Jul, 2007 - 09:48 AM
User is offlineProfile CardPM

Go to the top of the page

jmanuelh
post 25 Jul, 2007 - 11:20 AM
Post #3


New D.I.C Head

Group Icon
Joined: 18 Nov, 2006
Posts: 23



Thanked 2 times
My Contributions


Thx Styx!!

I'll try it with MySQL, i don't knwon much abuti it, but i thik i'll read to do this...

For the php one, now i can comare and determine if the password is in the file, now i have to delete this line. Here is my code... what would you recomend to delete the line??

CODE

<?php

$findThis = "papas\n";
$handle = @fopen("lista.txt", "r"); // Open file form read.

if ($handle) {
while (!feof($handle)) // Loop til end of file.
{
$buffer = fgets($handle, 4096); // Read a line.
if ($buffer == $findThis) // Check for string.
{
header("Location: http://pr........com/"); // If the string is in the fils, then redirect
$buffer = "";
} else {

}
}
echo "Cannot Continue\n"; // If the string is not in the file show Cannot COntinue
fclose($handle); // Close the file.
}

?>



Thanks Again


J. Manuel

This post has been edited by jmanuelh: 25 Jul, 2007 - 11:24 AM
User is offlineProfile CardPM

Go to the top of the page

jmanuelh
post 26 Jul, 2007 - 08:34 AM
Post #4


New D.I.C Head

Group Icon
Joined: 18 Nov, 2006
Posts: 23



Thanked 2 times
My Contributions


Hey guys I solved this!!!!

what i did is this...

read every line in the txt and store them in an array, then with a for loop and an if inside the loop the array is compared index by index with the given word. If the word is in the array, then this cell would be modified to x.x.x, and then this array is written to the file.

CODE

<?php


$pass = $_POST["password"]."\n";
$datalines = file ("lista.txt");
$longi= 0;
foreach ($datalines as $zz){//here i get the number of lines in the file
$longi = $longi+1;
}

if($pass != "x.x.x\n"){//if the pass hasn't been used
for ($i = 0; $i < $longi; $i++)

  {
   if($datalines[$i] == $pass)
        {
             $datalines[$i] = "x.x.x\n";//if the pass is in the file delete and change for x.x.x
          
          $aux = "1";
        }
        
    
   }
  
  
  
   if($aux == "1"){
  
      for ($j = 0; $j < $longi; $j++){
             $escribe = $escribe.$datalines[$j];
            
      }
      $myFile = "lista.txt";//If password was ok, then you will be redirected
      $fh = fopen($myFile, 'w') or die("can't open file");
      $stringData = "$escribe";
      fwrite($fh, $stringData);
      fclose($fh);
      header("Location: http://pr........com");
      
   }else{
  
     echo "Password no valido";
   }
  
} else{

   echo "Password no valido";
}  
  


Thx for your comments



J. Manuel
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 05:57AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month