Hey Everyone, I have a small PHP script that reads a text file for a set of numbers (ie. 74836 or A37484) if the number exists in the text file it goes to page A, if it does not page B. It works great, however if a letter gets entered first (we have letters as well in the text file as above) it automatically goes to Page A even though the combination is not in the text file, can anyone tell me what part of this code to change to make sure it matches (including letters) to go to page A instead of B? The code is entered in a form field POST. Thanks!! <? // Filename $file = "file.txt"; // Correct Number $loc_correct = "http://www.websitename.com/Page_A.php"; // Wrong Number $loc_wrong = "http://www.websitename.com/Page_B.php"; if (isset($_POST['number'])) { // Set Number $number = $_POST['number']; // Open File $file = fopen($file,"r") or exit("Unable to open file!"); // Run Through File $found = false; while(!feof($file)) { if ($number == (int)fgets($file)) { header('Location: '.$loc_correct); exit; } } header('Location: '.$loc_wrong); exit; } ?> Code (markup):
i think u can change this part if ($number == (int)fgets($file)) , u can use regular expression to make the match
Hi, Try this script: <?php // Filename $file = "file.txt"; // Correct Number $loc_correct = "http://www.websitename.com/Page_A.php"; // Wrong Number $loc_wrong = "http://www.websitename.com/Page_B.php"; if (isset($_POST['number'])) { // Set Number $number = $_POST['number']; // Regezx pattern that matches exact word $patt = '/(?:^|[^a-zA-Z])'. preg_quote($number, '/'). '(?:$|[^a-zA-Z])/i'; // Reads File $file = file_get_contents($file); // Check if $number exits in $file if (preg_match($patt, $file)) { header('Location: '.$loc_correct); exit; } header('Location: '.$loc_wrong); exit; } ?> Code (markup):