In testbed.php:
Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
$coupons = file('my_file.txt', FILE_SKIP_EMPTY_LINES);
$coupons = array_map('trim', $coupons);
$myCoupon = $_POST['cc_0001'];
$sdiscount = $myCoupon;
// if found return the position in the array as $key
if(($key = array_search($myCoupon, $coupons)) !== false) {
echo $myCoupon . ' is valid'.'<br>';
$hasdiscount = 1;
// delete the coupon from the array using $key
echo "Deleted coupon code $myCoupon!"."<br>";
unset($coupons[$key]);
}
else {
$hasdiscount = 0;
echo 'Invalid coupon code: "' . $myCoupon . '"<br>';
}
// join the array elements into a string and write back to the file
file_put_contents(implode("\n", $coupons), 'my_file.txt');
?>Code: Select all
Invalid coupon code: "a"
Warning: file_put_contents(4wjtg84wtjw4tjw48ujtw84jt83wujr8wj4t8rujw3t8ujhw48t8w4twj48t w4t8uw4tj3w8ru8w3ur8q3ujr83w) [function.file-put-contents]: failed to open stream: Invalid argument in \htdocs\testbed.php on line 27