Accepting login number with changing prefix with PHP

235 Views Asked by At

I have a flatfile database file with the following numbers:

1
2
3
4
5
etc.

The numbers correspond to a USER ID where a person would enter any of those numbers above, in order to login.

I do not want to modify my file. My question is, if there's a way to add an accepted "PREFIX".

For example, a user logs in with:

abcd1
abcd2
abcd3
abcd4
abcd5
etc.

But my data file is still, but does not contain the prefix "abcd":

1
2
3
4
5
etc.

It would also have to be a "perfect" match. The tests I have done so far have not been conclusive

I guess using an accepted "array" would be the way to go?

My present login PHP script is this, but only works on an exact match for a number, I would like to add a prefix that I can change later on:

<?php
date_default_timezone_set('America/New_York');
$today = mktime(0, 0, 0, date("m"), date("d"), date("y"));

$currenttime = date('h:i:s:u');
list($hrs,$mins,$secs,$msecs) = split(':',$currenttime);

echo "<center><form action=\"" . $PHP_SELF . "\" method='post'><input onkeypress=\"return isNumberKey(event)\" type='text' name='uNum' /><input type='submit' value='Submit' /></form></center>";

if(!$_POST['uNum']) {
die('<center>Entrer votre numéro d\'inscription pour finaliser votre demande.</center>');
}

$numbers = file_get_contents("datafile.txt");

$uNumber = $_POST['uNum'];

if ( @preg_match( "/([^0-9]{$uNumber}[^0-9])/", $numbers ) ) {

print_r( "<center>The number $uNumber is good. You may proceed.</b><br><br>" );

file_put_contents($_POST['uNum'].".txt",$_POST['uNum'] . "\nUsed on ".date("m/d/Y", $today). (" $hrs:$mins:$secs")."");

include 'validate_process.php';

if (isset($_POST['uNumber']))
{

$uNumber = $_GET['inscripnum1'];
}

} else {
echo "<center>Sorry, this login number does not exist.</center>";
}

?>
3

There are 3 best solutions below

2
On

Maybe:

if ( @preg_match( "/([^0-9]{abcd$uNumber}[^0-9])/", $numbers ) || @preg_match( "/([^0-9]{$uNumber}[^0-9])/", $numbers ) )

Think that'll work? I never use preg_match

0
On

This is one of many ways to do it. Create an array of the file contents and loop through that array:

$numbers = file('datafile.txt');
$result = false;
foreach($numbers as $n) {
  if ('abcd'.$n === $uNumber) {
    $result = true;
    break;
  } 
}

if ($result) {
  // OK!
}

Here's another, where I load the file as an array, add the prefix to each element and then check if the provided value is in that array:

$numbers = file('datafile.txt');
$result = in_array($uNumber,
                   array_map(function($e) { return 'abcd'.$e; },
                             $numbers)
                  );
5
On

Before you do a preg_match, check if the input has the desired prefix and remove it.

$uNumber = $_POST['uNum'];

$prefix = "abcd"; //you can change the prefix anytime
$length = strlen($prefix);
if(substr($uNumber, 0, $length) === $prefix) {
    $uNumber = substr($uNumber,$length);
} else {
    $uNumber = ""; //empty or some value that is not a valid ID stored in your flatfile.
}

//Now use $uNumber in preg_match()

if ( @preg_match( "/([^0-9]{$uNumber}[^0-9])/", $numbers ) ) {
    //do something
}