Hi Everyone, I am having a small issue with this code and can't figure out how to fix it. I have a form field (just 1) that checks a text file to see if there is a number in it. If the number is in it, it goes to page A, if not page B. All of the codes are 6 or 7 digit numbers, however I am just realizing in you put in a 3 digit code and the 3 digits make up part or one of the codes ex: put in 456 and one of the codes is 234567, it validates. I need to find a way to make the code only work if there are 6 or 7 alphanumeric characters in the form field. below is my php code - <?php // Filename $file = "file.txt"; // Correct Number $loc_correct = "http://www.website.com/success.php"; // Wrong Number $loc_wrong = "http://www.website.com/notfound.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): Any ideas to validate 6 or 7 characters before sending would be great! Thanks!!
Is there anything else in the file or just the number? If its just the number, or if the number is consistently on it's own on a specific line then I'd do something like <?php // Filename $file = "file.txt"; // Correct Number $loc_correct = "http://www.website.com/success.php"; // Wrong Number, the default destination $goto = "http://www.website.com/notfound.php"; if (isset($_POST['number'])) { // Set Number $number = intval($_POST['number']); $filecontents = file_get_contents($file); $fileval = intval($filecontents); // Check if $number exits in $file if ($fileval & $fileval === $number) { $goto = $loc_correct; } } header('Location: '.$goto); exit; ?> PHP:
Sorry I didn't see this and didn't think anyone had answered, the file.txt is a return separated list of 6 digit numbers (i.e 387465) and 7 digit numbers with a letter in front (i.e A938475), I tried your code above and all it does is automatically re-direct to the error page. Any more help would be appreciated!
Then you need to put some var_dump() commands in there and see what it is actually capturing. There may be some hidden characters at the end that need to be stripped out. the most important one is dumping out whatever gets into $goto Do you need to drop the letter at the beginning? just use substr($goto, 1) to get that.
Hey Sarahk, thanks for the info but Im not very good at coding PHP so I can't really go at it myself but to clarify the text files are in a format like so: 847563 298475 A938475 A938475 They are all randomly generated, the problem I am facing is using the example above if a user puts in 847 it returns true (which is true 847 is a number in the file) but it is not checking to see if only FULL numbers are in there, I want it to only match if the WHOLE thing from start to finish is listed on a row. Does that make sense? Thanks again for your help!!
The triple equals === should have prevented that. Two ways to double check intval($fileval) == intval($number) // but that will mess up when there is a letter in the string check the length of the string and make sure they match, that way 837 can't be part of a larger string.
the === is what I thought as well but it isint working like that. Is there an easy line of code that I can add to make it check for the proper length of the number?