FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP File Read, and File Check?





Diablosblizz
Hey, I know there is Fopen and I think Fread, but how do I check if the file contains a certain thing, in this case a IP Address?

Code:
<?php
fopen('text.txt', true);
echo fopen;
?>


I think that opens the file, and displays it, but I want to check if the file contains a IP. I think I know how to write to a file (fwrite).

Thanks!
coreymanshack
Diablosblizz wrote:
Hey, I know there is Fopen and I think Fread, but how do I check if the file contains a certain thing, in this case a IP Address?

Code:
<?php
fopen('text.txt', true);
echo fopen;
?>


I think that opens the file, and displays it, but I want to check if the file contains a IP. I think I know how to write to a file (fwrite).

Thanks!


import file into a variable to be used as a string... then http://us2.php.net/preg_match
Diablosblizz
Okay, this works, for the most part.

Code:
$ip = $_SERVER['REMOTE_ADDR'];
$opens = fopen("pollips.txt", "r");
if(preg_match("$ip", "$opens")) {
   // sorry user, your IP has voted!   
} else {
   // put the voting stuff here
}


I used this, and I got this:

Web Server wrote:
Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in C:\xampp\htdocs\Hotel Mario\poll.php on line 89


So... I am guessing the variables in the preg_match are causing it to error, but they need to be there, to check if their IP is in the txt file. Or it is the IP's screwing up the preg_match (because it says alphanumeric).

So... I am stumped, what can I do?

EDIT: I googled the error, and I noticed when people used variables they put / in their quotes, so I did the same.

Code:
$ip = $_SERVER['REMOTE_ADDR'];
$opens = fopen("pollips.txt", "r");
if(preg_match("/$ip/", "/$opens/")) {
   echo "You've voted.";   
} else {
   echo "$ip Vote away!";
}


I echoed the IP, and because I'm on localhost it displays 127.0.0.1, and the pollips.txt has 127.0.0.1 in it, and it shows Vote Away still.

Still stumped though, thought I would update you.
coreymanshack
Diablosblizz wrote:
Okay, this works, for the most part.

Code:
$ip = $_SERVER['REMOTE_ADDR'];
$opens = fopen("pollips.txt", "r");
if(preg_match("$ip", "$opens")) {
   // sorry user, your IP has voted!   
} else {
   // put the voting stuff here
}


I used this, and I got this:

Web Server wrote:
Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in C:\xampp\htdocs\Hotel Mario\poll.php on line 89


So... I am guessing the variables in the preg_match are causing it to error, but they need to be there, to check if their IP is in the txt file. Or it is the IP's screwing up the preg_match (because it says alphanumeric).

So... I am stumped, what can I do?

EDIT: I googled the error, and I noticed when people used variables they put / in their quotes, so I did the same.

Code:
$ip = $_SERVER['REMOTE_ADDR'];
$opens = fopen("pollips.txt", "r");
if(preg_match("/$ip/", "/$opens/")) {
   echo "You've voted.";   
} else {
   echo "$ip Vote away!";
}


I echoed the IP, and because I'm on localhost it displays 127.0.0.1, and the pollips.txt has 127.0.0.1 in it, and it shows Vote Away still.

Still stumped though, thought I would update you.


yea i dont think you need the backslashes, and i thought preg match would match up numerics, but it seems to be saying it doesn't... let me check for another function....


try this

$ip = $_SERVER['REMOTE_ADDR'];
$opens = fopen("pollips.txt", "r");
//dont forget to lock the file
flock( $opens, LOCK_SH);
if(strstr("$opens", "$ip")){
echo"You've voted.";
} else{
echo"$ip Vote away!";
}
//dont forget to unlock the file
flock( $opens, LOCK_UN);
//don't forget to close the file
fclose( $fp );


also, why are you using a plain txt file instead of a mysql database for this?
Diablosblizz
Corey, good point. I totally forgot about mysql databases. I guess because of my member system I thought I couldn't, for some odd and stupid reason.

Thanks a lot.
MrBlueSky
Diablosblizz wrote:

EDIT: I googled the error, and I noticed when people used variables they put / in their quotes, so I did the same.

Code:
$ip = $_SERVER['REMOTE_ADDR'];
$opens = fopen("pollips.txt", "r");
if(preg_match("/$ip/", "/$opens/")) {
   echo "You've voted.";   
} else {
   echo "$ip Vote away!";
}


I echoed the IP, and because I'm on localhost it displays 127.0.0.1, and the pollips.txt has 127.0.0.1 in it, and it shows Vote Away still.

Still stumped though, thought I would update you.


When a string is to be used by PHP as a regular expression pattern it always needs the /'s. Most programming languages have builtin language support, using the / to denote patterns, like this:

Code:

$regex = /$ip/;
$regex2 = /[a-z]*.?/i;


But PHP doesn't support this kind of regular expression syntax (too bad), so all regex patterns have to be put in strings. This means that a function like preg_match, expects its first parameter (the pattern) to be a string containing a pattern. The string is denoted by the " and the pattern by the /. So the previous code in PHP is:

Code:

$regex = "/$ip/";
$regex2 = "/[a-z]*.?/i";


Especially note the 'i' in the last line has to be IN the string, but OUTSIDE the /.

That's why your new code works, and your old one didn't. Only one thing: the /'s aren't needed in the second argument to preg_match, because it isn't a pattern:

Code:

if(preg_match("/$ip/", "$opens")) {


By coincidence in this case it also works with the //'s in the second parameter (has something to do with the way regexes match stuff), but its better to leave them out because for a lot of cases they will break your code.
Diablosblizz
I have solved this with mysql.
coreymanshack
Diablosblizz wrote:
I have solved this with mysql.


Glad to hear Very Happy
Diablosblizz
Me too! Very Happy Thank you Corey.
Related topics
How To : Secure Your PHP Website
hosting error......help
Cron jobs - running a php file
[PHP doubt]Knowing the directory location of PHP file
is there any way to trigger a .php file without refreshing?
Uploading a .php file! NEED HELP thanks
User Viewing A PHP File
send variable from php tp php file
Ajax or JS - need help ASAP
Limiting php file functions with .htaccess
Remote php file access and reading
including javascript in php file
Hide PHP file extension
[TuT]JOOMLA Installation Guide(Easiest Way)
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.