-6

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>";
}

?>
4

3 回答 3

1

在进行 preg_match 之前,请检查输入是否具有所需的前缀并将其删除。

$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
}
于 2012-06-04T14:57:04.737 回答
0

也许:

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

认为这会奏效吗?我从不使用 preg_match

于 2012-06-04T14:42:24.113 回答
0

这是许多方法之一。创建一个文件内容数组并循环遍历该数组:

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

if ($result) {
  // OK!
}

这是另一个,我将文件作为数组加载,为每个元素添加前缀,然后检查提供的值是否在该数组中:

$numbers = file('datafile.txt');
$result = in_array($uNumber,
                   array_map(function($e) { return 'abcd'.$e; },
                             $numbers)
                  );
于 2012-06-04T14:43:06.757 回答