Pulling data based on line number from text file, how?
Moderator: General Moderators
Pulling data based on line number from text file, how?
Thanks for taking the time to read my post. I usually am able to find my answer without posting but I cannot find this anywhere. I would consider my self a novice PHP'er.
So here is what I am trying to do, I have these 15 digit liscense keys for software that I am selling through my website, so instead of manually emailing them I would like to setup a script to do it automatically. The trick is, after a key is sold it needs to be marked or removed so it does not get sold again.
If anyone has any ideas on how to do this please just give me a little help. I have been searching google for days and have not found anything.
Thanks a bunch,
Marcel
So here is what I am trying to do, I have these 15 digit liscense keys for software that I am selling through my website, so instead of manually emailing them I would like to setup a script to do it automatically. The trick is, after a key is sold it needs to be marked or removed so it does not get sold again.
If anyone has any ideas on how to do this please just give me a little help. I have been searching google for days and have not found anything.
Thanks a bunch,
Marcel
-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
off the top of my head... store them in a mysql database (make sure it's secure of course). when the buy the key have it display the key on the screen or display something like, "thank you, your key has been e-mailed to you." and then use php mail() function to e-mail them and then immediatly have it delete the key from the database. or perhaps write the key to a different table, perhaps, "used keys" and then delete from "unused keys".. that help?
edited the post, sorry about the incomplete answer;)
edited the post, sorry about the incomplete answer;)
-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Ideally you want these keys in a MySQL database like Charles says. I think the best thing to would be to have something simple like an extra column for "used" or similar and if it's set to "1" then it's unusable anymore. So you'd email them the key and update the record so that used=1.
If you really do want to go with the text file approach something like this would work...
I'll assume the file you contains a list of keys all on new lines like this
If you really do want to go with the text file approach something like this would work...
I'll assume the file you contains a list of keys all on new lines like this
Code: Select all
XXX-YYY-ZZZ
AAA-BBB-CCC
DDD-EEE-FFF
HHH-AAA-BBBCode: Select all
$lines = file($path_to_text_file); //Open the file and collect the lines as an array
$code = trim($lines[0]); //Get the first line (and remove any trailing whitespace)
/*
Something to email or display the code here
mail() ?
*/
array_shift($lines); //Remove the code we just used
$data = implode('', $lines); //Glue it back together as a string
$handle = fopen($path_to_text_file, 'w'); //Open the file again for writing this time
fwrite($data, $handle); //Write back to the file with a missing line
fclose($handle); //Close the fileThe original idea was to use a MySQL database which I use for everything else, but I did not think it was possible. Stupid me ;-p
I bet anything is possible with PHP and MySQL. So yes give me an idea for doing it with MySQL.
I have found out how to do it with a flat file using the following script:
<?php
$handle = fopen("test.txt", "r");
$buffer = fgets($handle, 4096);
echo $buffer;
fclose($handle);
?>
Maybe someone can fill me in on how to post PHP script in the nifty little boxes.
Thanks so much for the overwhelming response!!
Marcel
I bet anything is possible with PHP and MySQL. So yes give me an idea for doing it with MySQL.
I have found out how to do it with a flat file using the following script:
<?php
$handle = fopen("test.txt", "r");
$buffer = fgets($handle, 4096);
echo $buffer;
fclose($handle);
?>
Maybe someone can fill me in on how to post PHP script in the nifty little boxes.
Thanks so much for the overwhelming response!!
Marcel
-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
grrrrr..the idea is for you to go do the coding and tell us if you have an error but...I'm bored so here's the code
NOTE: NOT TESTED
Conventions: Assuming we have a table called keys and two fields, one key and one used.
I'm also assuming you got their e-mail address in the form they submitted too:-D
that should do it..
but that is off the top of my head so ya know..
i had a typo:)
NOTE: NOT TESTED
Conventions: Assuming we have a table called keys and two fields, one key and one used.
I'm also assuming you got their e-mail address in the form they submitted too:-D
Code: Select all
<?php
$email=$_POST['email'];
$sql="SELECT * FROM keys WHERE used == '0' LIMIT 1";
$query=mysql_query($sql);
$result=mysql_fetch_object($query);
echo ("Thank you for purchasing a CD key your key is $result->key . You will also recieve an e-mail with this cd key, thank you again.");
$message="Your cd key is $result->key . Thank you again.";
$headers='From: myself@now.com'."\r\n" . ' Reply-To: stillme@now.com' ."\r\n";
$subject="Thank you for your purchase.";
mail($email,$subject,$message,$headers);
$secondsql="UPDATE keys SET used='1' WHERE key=$result->key";
$secondquery=mysql_query($secondsql);
?>but that is off the top of my head so ya know..
i had a typo:)
Last edited by Charles256 on Thu Sep 22, 2005 10:48 pm, edited 2 times in total.
-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm