You could use file_get_contents
to pull the data in the file, str_replace
to remove the line and file_put_contents
to put the data back. As suggested in this answer:
How to delete a line from the file with php?
Although this might not be the most efficient way of doing that, you could end up reading from and writing from the disk a lot depending on your application. Ideally you'd use a database like MySQL or SQLite.
Edit 9/25/2016
Here's a simple implementation of a database that might work for you. As of PHP 5.3 SQLite3 is enabled by default. This method generates a new code when you need instead of pre generating a list. Of course you could pre generate a list using this method as well if you wanted to.
PHP Docs:
http://php.net/manual/en/book.sqlite3.php
You only need a few concepts to get going:
- SQLite3::open [Create or Open a database file]
- SQLite3::query [For Selects]
- SQLite3::querySingle [For Selects]
- SQLite3::exec [For Insert, create, delete, update]
Note: My method for generating the "Code" is not cryptographically secure. If security is a big concern See: PHP: How to generate a random, unique, alphanumeric string?
Setup.php
Run this file once to create your database, you can delete it after if you like
<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('mysqlitedb.db');
}
}
$db = new MyDB();
$db->exec('CREATE TABLE codes (code_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,code varchar(50) NOT NULL)');
$db->close();
CodeManager.php
Include this file anywhere you want to generate or delete a code
<?php
class CodeManager extends SQLite3
{
function __construct()
{
// You can change this to the file location of the code list.
$this->open('mysqlitedb.db');
}
function getRandomCode()
{
do {
// If users have a unique identifier, you can use it instead of rand()
// This should guarrantee a unique value that doesn't have to be checked first.
// You can also replace md5(...) with any method you prefer to generate the code
$code = md5(uniqid(rand(), true));
$code_check = $this->querySingle('SELECT code FROM codes WHERE code="'.$code.'"');
} while(!is_null($code_check)); // Need this loop as long as we are using rand()
// Try to add the code to the list of used codes
if($this->exec('INSERT INTO codes (code) VALUES ("'.$code.'")'))
return $code;
return false;
}
function deleteCode($to_delete)
{
// Try to delete the record
if($this->exec('DELETE FROM codes WHERE code="'.$to_delete.'"'))
return true;
// If we couldn't delete the record
return false;
}
function getAllCodes()
{
$all_codes = array();
$results = $this->query("SELECT * FROM codes");
while ($row = $results->fetchArray()) {
$all_codes[] = $row['code'];
}
return $all_codes;
}
}
App.php
Example of how to use CodeManager.php
<?php
include('CodeManager.php');
$cm = new CodeManager();
// Get a single new code
$new_code = $cm->getRandomCode();
echo $new_code."\r\n";
// Get a list of all codes generated so far
$all = $cm->getAllCodes();
// Display them one by one
foreach($all as $single){
echo $single . "\r\n";
}
?>