Log form details to a text file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Log form details to a text file

Post 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.
Last edited by m0u53m4t on Wed Jun 14, 2006 2:04 am, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post 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?
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post 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... :)
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post 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...";
}
?>
Post Reply