...

View Full Version : Checking files contents - little help



Anthony2oo4
07-09-2005, 09:04 PM
Ok, I want to put ads on my site, buy like you all know ads are annoying. Well I thought that if i only show the user the ads one per day then that would be great. I know this can be done with cookies and javascript, and I have managed to do that succesfully but its not really reliable as people may have javascript disabled etc.

So I thought, If I record the IP of the user in a file, and then make a script that says, if the ip already exsist in the file, dont show the ads script. if it doesnt, show the ads script.

I managed to get the users IP address and write it to a file, but the IP dosnt go to a new line :S

How can I make the IP record to a new line every time and read the file to check if the IP alread exsists. Here is my code so far:


//get IP Address
$domain = GetHostByName($REMOTE_ADDR);

//Open File for writeing
$filename = 'ip.txt';
$fp = fopen($filename, "a");

//Write IP to file
$write = fputs($fp, $domain);
fclose($fp);

I know I can read the file like this:


$filename = 'ip.txt';
$fp = fopen($filename, "r");
$contents = fread($fp, filesize($filename));
fclose($fp);

But how do I check the file to see if the IP is there?

thanks for your help ;) :thumbsup:

delinear
07-09-2005, 11:59 PM
You could probably do it this way:

<?php
//get IP Address
$domain = GetHostByName($REMOTE_ADDR);

// now check that the IP doesn't already exists in the file
$filename = 'ip.txt';
$handle = fopen($filename, "r");
while (!feof($handle)) {
$data = fgets($handle, 4096);
if(strpos($data, $domain) !== false) {
$ipexists = 'yes';
}
}
fclose($handle);

if(!isset($ipexists)) {
// the ip doesn't exist in the file, show the advert and write the ip to the file now

//Open File for writing
$fp = fopen($filename, "a");

//Write IP to file
$write = fputs($fp, $domain . "\n"); // this should add the ip with a newline after it
fclose($fp);
}

?>
But I would suggest you also keep the cookie method as a redundancy because using GetHostByName($REMOTE_ADDR) is by no means certain to work every time.

Anthony2oo4
07-10-2005, 12:12 AM
ah, problem :(

Ok, that script worked great and thanks very much for that. But when I uploded it to my server they dont seem to alow fopen :mad:

Is there another method to write to files?

here are the errors I recived:


Warning: fopen(ip.txt): failed to open stream: Permission denied in /home/public_html/ip.php on line 20

Warning: fputs(): supplied argument is not a valid stream resource in /home/public_html/ip.php on line 23

Warning: fclose(): supplied argument is not a valid stream resource in /home/public_html/ip.php on line 24

Or maybe write it to a database, (I have no clue how to use myswl databases with php lol )

delinear
07-10-2005, 12:18 AM
It could be that the wrong permissions are set for the file, that's all. Try and chmod the file to 0755 I think, if not try chmod to 0777 and try again, it should work then.

To be honest a mysql database solution would work better for you, it's much better for storing this kind of data and retieving it plus you can easily add timestamps to your data to check when the user last saw an add, etc. They're not too difficult even though they look daunting to begin with, if you run through a couple of the tutorials online you will soon see how easy and powerful they are.

Anthony2oo4
07-10-2005, 01:41 PM
Ok, I set it as 755 and it didnt work, so set it as 777 and worked fine thanks very much. i took your advice and kept the cookie too so hopefully things should be sorted now.

I know MySql would be better, and I can read, write and connect to a database as I have read tutorials before, But I can't setup the mysql database. I always struggle as there are so many fields to complete for a simple database.

Maybe in the future I can learn, but for now, this should work ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum