Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post

    Checking files contents - little help

    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:

    PHP Code:
    //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:

    PHP Code:
    $filename 'ip.txt';
    $fp fopen($filename"r");
    $contents fread($fpfilesize($filename));
    fclose($fp); 
    But how do I check the file to see if the IP is there?

    thanks for your help

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could probably do it this way:
    PHP Code:
    <?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($handle4096);
        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.
    Last edited by delinear; 07-10-2005 at 12:01 AM.

  • #3
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    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

    Is there another method to write to files?

    here are the errors I recived:

    PHP Code:
    Warningfopen(ip.txt): failed to open streamPermission 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 )

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •