开发者

HTML form that searches text (php?)

I have a file with usernames and ip addresses (about 10MB). I want to be able to have people come to my website and enter an ip address or username and it search the database and return matches.

Case 1: User enters "Billy" in <form>. Output is IP address and all users who have used this IP address Case 2: User enters ip address in <form>. Output is all usernames which have used this ip address.

Unfortunately I'm a true noob here, I've looked up w3schools, but haven't really found what I wanted or even how I should do this.

Probably I'll have an HTML <form> 开发者_运维问答where user enters either IP or username and radio buttons where user specifies if what they just entered was an IP or name. After this I'm stumped, in the <form> should I have an action="blah.php" or can I create a local variable to take in this value?

EDIT: I would like to do this all in one html file by injecting right into the html. Probably using $desc=$_POST['ipaddress']; or similar? What kind of action="" should I use in the form tags?


I would just make a single search field:

<form action="search.php">
<input name="q">

Then in the search.php script just receive the string:

$q = $_REQUEST["q"];

And run through the text file:

$f = fopen("10MB.txt", "r");
while (($line = fgets($f)) !== FALSE) {
   if (strstr($line, $q)) {
       print "<li>found: $line"; 
   }   // outputs name+ip if either one is found in the line
}

This assumes that the text file contains one IP and username per line. You can fine-tune the output of course. Split it up if you want. But for searching itself its irrelevant if the form sends an ip address or a username to search for. It would match either in the text file.


How are you storing the usernames and ip addresses? if you are using a sql database like mysql then you can use it's search functions or a more dedicated search tool like lucene or sphinx.

Here are some references that you can follow.

http://www.w3schools.com/PHP/php_mysql_select.asp

http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html

http://lucene.apache.org/java/docs/index.html

http://sphinxsearch.com/docs/current.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜