Get data from a textbox and compare it with a line in a file

Links for php scripts

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Aug 14, 2014 1:13 am

Thu Aug 14, 2014 6:37 am


first create a form with a text box.

In your action php you should get the value of the text box by searching it into the $_POST (or $_REQUEST) array.

Code: Select all

$text_val = $_POST['text_box_name'];
then you'll have to search it in the file; there are few ways to do that.
The ugliest and yet quickest way is

Code: Select all

$file_content = file_get_contents('/absolute/path/to/file');
if (strpos($file_content, $text_val) !== FALSE) {
  // OK 
  echo "You sign in success.";
else {
  // KO
  echo "You don't have an account. Please register a n account.";

User avatar
php-forum Active User
php-forum Active User
Posts: 365
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Sat Oct 18, 2014 4:53 pm

You probably don't want to do it this way. For example, if the string you're matching is 'hello my name is bob', then none of the following will work:

' hello my name is bob' (space at the beginning)
'hello my name is bob!' (character at the end)
'hello my name is Bob' (capitalized letter)

You probably want to do at least one of the following:
  1. Trim the textbox text using trim() to remove spaces at the beginning and end and use stripos to make it case insensitive (easiest option)
  2. Use preg_match to check the entire string for matches. You can also make it case insensitive here if you like (although stripos() can do that too)

Post Reply