Page 1 of 1

Log form details to a text file

Posted: Mon Jun 12, 2006 2:31 am
by m0u53m4t
I want my code only to store the vairiable $serial to a log file. How do I need to modify my script?

Code: Select all

<?php
$filename = "logfile.txt";
if (isset($_GET["name"])){
 if (!$handle = fopen($filename, 'a')) {
  die ("Error while connecting, please try again.");
 } else {
  if (fwrite($handle, "\r\n" . $_GET["name"]."\r\n" . $_GET["comment"]) === FALSE){
   die("Error while writing.");
  }
 }
echo "Log recorded"
 fclose($handle);
} else {
 echo "Error: no username or password was specified";
}
?>
Edit: I've got it reading from a text file, I just need to know how to delete something from that file.

Posted: Mon Jun 12, 2006 3:02 am
by twigletmac
This bit of code:

Code: Select all

if (fwrite($handle, "\r\n" . $_GET["name"]."\r\n" . $_GET["comment"]) === FALSE){
   die("Error while writing.");
  }
is where data is written to the log file.

You can change it a bit to make it easier to change the data you store:

Code: Select all

// create a variable for the log data
$log_data = "\r\n" . $_GET["name"]."\r\n" . $_GET["comment"];

// change the fwrite() call to use the $log_data variable
if (fwrite($handle, $log_data) === FALSE){
   die("Error while writing.");
  }
Now all you have to do is set $log_data to whatever you want written to the file and voila.

Mac

Posted: Mon Jun 12, 2006 9:41 am
by m0u53m4t
Thanks! This is taking it one step further. If I wanted to have a file with valid serials in (serials.txt), then each line is compared to $serial until there is a match (otherwise 'echo "Invalid";') then delete that from the list. How could I do that?

Posted: Mon Jun 12, 2006 10:52 am
by mattcooper
Use in_array():

Put all of your serial numbers into a textfile and separate them with commas, for example, then create an array from them using code something like this (untested):

Code: Select all

$data = file_get_contents(path/to/your/file);
$elements = array(explode("$data",",")); // creates an array of your serial numbers
Then, look in the array for the string you want to compare...

Code: Select all

if(in_array($serial,$elements))
Hopefully this will help you out... :)

Posted: Mon Jun 12, 2006 4:07 pm
by m0u53m4t
Hmmm... this is my php:

Code: Select all

<?php

$serial = $_GET["serial"];

$data = file_get_contents("keys.php");
$elements = array(explode("$data",",")); // creates an array of your serial numbers 
if(in_array($serial,$elements)){
echo "Well done!";
}else{
echo "You stuffed it...";
}
?>
This is my keys.php file:

Code: Select all

79EU-IDJH-J09F-UAWE-HRF9,79EU-IDJH-J09F-UAWE-HRF4
Its still saying you stuffed it though. What do I do?

Posted: Mon Jun 12, 2006 4:19 pm
by twigletmac
You've got the arguments for explode() slightly muddled and you don't need to put the exploded array into another array, so instead of:

Code: Select all

$elements = array(explode("$data",","));
try

Code: Select all

$elements = explode(',', $data);
To check what's in the $elements array do:

Code: Select all

echo '<pre>';
print_r($elements);
echo '</pre>';
Mac

Posted: Wed Jun 14, 2006 2:07 am
by m0u53m4t
Yup! It worked! How can I get it to delete that code from the text file after its done? This is the code so far:

Code: Select all

<?php

// I gave the paid guy 79EU-IDJH-J09F-UAWE-HRF9 and the other guy 43T7-9H2O-348Y-UVH2-80VN
$serial = $_GET["serial"];

$data = file_get_contents("keys.php");
$elements = explode(',', $data);
if(in_array($serial,$elements)){
echo "Well done!";
}else{
echo "You stuffed it...";
}
?>