4

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

flowfree
  • 16,356
  • 12
  • 52
  • 76
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 1
    Your question isn't very clear to me but it sounds like regular expressions might be the best way to provide that kind of validation. – Jeremy Harris May 27 '12 at 01:46
  • Your question is very unclear. Please rephrase so we can help you. – Mahmoud Al-Qudsi May 27 '12 at 01:53
  • Maybe in that file add Registration numbers with a seperator ex: |||, and then whenever you want to check if the user exists you open the file, and use explode("|||", $file); you'll get an array with those registation numbers, and then try in_array() if it exists it will return true ... – HamZa May 27 '12 at 01:53
  • This is the code that I have been trying to get to work, which might explain itself. – Funk Forty Niner May 27 '12 at 02:04
  • Thing is, that the data is stored in an .html file and will not be in a specific order. The script needs to find an exact occurence in the file. – Funk Forty Niner May 27 '12 at 02:06
  • Here is the Scenario: 1) The person visits the initial login page 2) The person enters the number(s) code that was assigned to him/her (pre-determined), so it can't be a number that does not exist, nor to be created on-the-fly. 3) When person clicks on "submit", script checks to see if number exists in file. 4) If exact number match exists, proceed to the page. 5) If match is not found, show a message. – Funk Forty Niner May 27 '12 at 02:29

1 Answers1

4

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.

Sampson
  • 265,109
  • 74
  • 539
  • 565
  • Thanks Johathan, I've got it working with the $numbers populating. Now, all I have to do is figure out a way how to build a form for it (inside the same script), and use the person's code entered, in order to reflect $uNumber – Funk Forty Niner May 27 '12 at 03:32
  • @FredFletcher Set `$uNum` to `$_POST['uNum']` and you're all set. Granted, you'll need `
    `.
    – Sampson May 27 '12 at 03:35
  • Beautiful! Thanks Jonathan, you've been a great help! I appreciate it very much, cheers! I did an echo `"
    ";` - time for me to get some shuteye. thanks again :)
    – Funk Forty Niner May 27 '12 at 03:51
  • I clicked on the "up arrow", and got the following error message "Vote Up requires 15 reputation". I'm new to this site, might be why? – Funk Forty Niner May 27 '12 at 03:59
  • @FredFletcher Ah, yes. You'll build up more reputation over time. Happy we were able to solve your problem. – Sampson May 27 '12 at 04:05
  • I do have another question that came to mind. Is there a way to incorporate something in the script that will prevent someone from entering the same code again, and showing them a message? – Funk Forty Niner May 27 '12 at 04:11
  • @FredFletcher This entire application would be best designed using a database, to be completely honest. A data-table of registration codes, and corresponding values that indicate if they have been used or not. In your example, using a flat-file, you could just remove the number from the file once it's been used. If their number isn't found, it's either because it was never good to begin with, or it has already been used. You could just say "This is not a valid registration code." – Sampson May 27 '12 at 04:15
  • I understand. Another ball of wax there lol! I have next to no knowledge with SQL or other databases, would've been the way to go for sure. How would I go about removing it once the code has been entered? – Funk Forty Niner May 27 '12 at 04:19
  • @FredFletcher If you decide to go that route, StackOverflow is here to assist ;) – Sampson May 27 '12 at 04:21
  • Ok thanks Jonathan. I will make another post later on, yet I'll see if I can figure out how to do myself first, which is always what I do before I go and ask for help. I only ask when I am stumped. – Funk Forty Niner May 27 '12 at 04:25
  • Jonathan - I didn't know how to contact you, wondering if you could have a look at a post I put up today http://stackoverflow.com/questions/10789573/deleting-exact-match-in-file – Funk Forty Niner May 28 '12 at 23:22