Missing TXT file in a PHP form. Any idea what it should contain?

65 Views Asked by At

I have this code on a php mail form that used to work until recently.

//Open last-order.txt file for getting the order number
    $readFile = fopen("./order.txt", "r") or die("Unable to open file!");
    $orden= fread($readFile,filesize("./order.txt"));
    fclose($readFile);
    ++$orden;
    $writeFile= fopen("./order.txt", "w") or die("Unable to open file!");
    fwrite($writeFile, $orden);
    fclose($writeFile);
            
    if (!preg_match("~^(?:f|ht)tps?://~i", $website)) $website = "http://" . $website;
    
    $website = filter_var($website, FILTER_VALIDATE_URL);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $participantes = filter_var($_POST["participantes"], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);

It references a TXT file named order.txt that read the file and increases the number by one each time is read. Unfortunately, I lost this file when the hosting provider left me in the cold with the service, with no way to access the server or its backup.

This is the text that I put on the TXT file:

Orden: 8000

I am not really a proficient PHP coder, so my attempts to recreate it or make it work, have been fruitless.

This is the error am getting:

[03-Jan-2023 13:29:22 America/Mexico_City] PHP Warning:  Undefined variable $email_content in /home/guillerm/iefa.com.mx/rsvp/php/reserve.php on line 78
[03-Jan-2023 13:29:22 America/Mexico_City] PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/guillerm/iefa.com.mx/rsvp/php/reserve.php:78) in /home/guillerm/iefa.com.mx/rsvp/php/reserve.php on line 149

Any help pointing me to the right direction, will be greatly appreciated. Thanks for looking at this.

1

There are 1 best solutions below

1
On BEST ANSWER

it's literally just supposed to be a number. replace Orden: 8000 with just 8000.

btw that code is thread-unsafe and the number may collide/duplicate if you run multiple instances of the code at the same time; if thread-safety is desired, you can instead do

$readFile = fopen("./order.txt", "c+b") or die("Unable to open file!");
flock($readFile, LOCK_EX);
$orden = (int)stream_get_contents($readFile);
++$orden;
rewind($readFile);
fwrite($readFile, (string) $orden);
flock($readFile, LOCK_UN);
fclose($readFile);