Question

this is a PHP related question.

I have searched far and wide for a solution to the following but did not find something that worked for me. If someone could help me, would be very much appreciated.

The idea that I have is to create a login page where, if a "specific" pre-determined registration number is given/sent to the user, the visits the login page and the number(s) exists in the file, to execute / login rights allowed.

For example, if a user's registration number is 12345 while another is 123, would like to first search to see if the user's number exists, then do something().

Therefore, searching for "12345" would not be the same if "123" exists already as would be the same for "234", so it has to find a specific number or numbers.

If it makes a difference, there stands to be a "space" before and/or after the number(s).

This would be in an existing file.

Code:

<?php 
$filename = 'datafile.txt'; 

// Data file may contain the following number(s) or any other 
// 123 (no) 
// 1234 (match) 
// 12345 (no) 
// 123456 (no) 
// exact match needed to proceed to page fopen($filename); 
$searchfor = '1234'; 

// this needs to be a variable 
// I don't know what the number will be used. 
$file = file_get_contents($filename); 

if (preg_match($file, $searchfor)) {
  echo "Match found. Proceed to the page."; 
} else { 
  echo "Match not found. Try again."; 
} 

fclose($filename); 
?>

Thank you. Any help is appreciated, cheers.

Fred

Was it helpful?

Solution

Storing these in a flat-file is probably not the way I would do this, but if I were to use a flat-file, the following example should work just fine:

$nums = "12345 34 45 12345 23"; # use file_get_contents to load from file
$uNum = 34;

if ( preg_match( "/[^0-9]{$uNum}[^0-9]/", $nums ) ) {
  echo "Access";
} else {
  echo "Denied";
}

As for populating the $nums string, you could use file_get_contents() to pull in the contents of your file containing all of the numbers.

/         Denotes start of pattern
[^0-9]    Space in pattern cannot be occupied by any number
{$uNum}   User number - 34 in the example above
[^0-9]    Space in pattern cannot be occupied by any number
/         Denotes end of pattern

Essentially, our number will not be found if it has a number on either side of it rather than a space, or nothing at all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top