Jim Raymond
Jim Raymond

Reputation: 103

PHP die stops everything. What else can I use?

I hope this is the last time I have to bother you good people. I'm a newbie hack who is working on a unique hit counter for each page of a web site. I seem to have it working properly but after the first time when it hits and adds the IP to the file it stops the whole page from loading on refresh or coming back. I know the problem is with the 'die' statement which ends the loop of checking for the IP. I have also tried 'break' and 'exit' but the same thing happens. I have searched for anything else but I can't find anything. Is there a way of getting out of the php code without stopping everything else from loading? Thanks in advance.

<?php

//  Declare string names
        $ip_file = "ip_index.txt"; 

//  get ip address of user      
    $user_ip = $_SERVER['REMOTE_ADDR'];
    $ip_handle = fopen($ip_file, "r");

    while (!feof($ip_handle) ) {

    $line_of_text = fgets($ip_handle);
    $ip = trim($line_of_text);

    if ($user_ip==$ip){

        die();
        }   
    }
    $count_file = 'count_index.txt';

//  read contents of count.txt
    $count_file = "count_index.txt";

    ini_set('display_errors', 'On');
        error_reporting(E_ALL);

    $handle = fopen($count_file, "r");
    $old_count=fgets($handle);
    fclose($handle);

//  write contents of count.txt

    $fp = fopen($count_file, 'ab');
    if (false === $fp) {
    throw new RuntimeException('Unable to open log file for writing');
    }

    $handle = fopen($count_file, "w");  
    $new_count = $old_count +1;
    fwrite($handle, $new_count);
    fclose($handle);

//  write new IP to ip.txt file

    $fp = fopen($ip_file, 'r');
    if (false === $fp) {
    throw new RuntimeException('Unable to open log file for writing');
    }

    $handle = fopen($ip_file, 'a+');    
    $w_user_ip=$user_ip . "\n";

    fwrite($handle, $w_user_ip);
    fclose($handle);

?>

Upvotes: 0

Views: 138

Answers (5)

Barmar
Barmar

Reputation: 781058

Don't exit the whole script when you find a match, just exit the loop. Set a variable that allows you to skip over the code that increments the unique hit counter.

$ip_exists = false;
while (!feof($ip_handle) ) {

    $line_of_text = fgets($ip_handle);
    $ip = trim($line_of_text);

    if ($user_ip==$ip){
        $ip_exists = true;
        break;
    }   
}

if (!$ip_exists) {
    // Update all the files
    ...
}

Upvotes: 1

Jim
Jim

Reputation: 22656

If this is included by other code you can use return:

if ($user_ip==$ip){
    return;
}  

This will stop further execution of your script but code which is including wont die.

If on the other hand you want to terminate the while loop only then break is what you need (what issue did you have when using it?)

if ($user_ip==$ip){
    break;
}  

Upvotes: 0

Leonardo
Leonardo

Reputation: 736

Do this:

....

if ($user_ip != $ip) {
    $count_file = 'count_index.txt';

//  read contents of count.txt
    $count_file = "count_index.txt";

    ini_set('display_errors', 'On');
    error_reporting(E_ALL);

    $handle = fopen($count_file, "r");
    $old_count=fgets($handle);
    fclose($handle);

//  write contents of count.txt

    $fp = fopen($count_file, 'ab');
    if (false === $fp) {
        throw new RuntimeException('Unable to open log file for writing');
    }

    $handle = fopen($count_file, "w");  
    $new_count = $old_count +1;
    fwrite($handle, $new_count);
    fclose($handle);

//  write new IP to ip.txt file

    $fp = fopen($ip_file, 'r');
    if (false === $fp) {
        throw new RuntimeException('Unable to open log file for writing');
    }

    $handle = fopen($ip_file, 'a+');    
    $w_user_ip=$user_ip . "\n";

    fwrite($handle, $w_user_ip);
    fclose($handle);
}

Upvotes: 0

scrblnrd3
scrblnrd3

Reputation: 7416

When you have:

if ($user_ip==$ip){
    die();
}

Put everything you don't want to run if the IPs are the same in an else{} block

Upvotes: 0

kinghfb
kinghfb

Reputation: 1021

Not sure what you mean. You can reverse the condition of the if block and then just wrap the remaining code in the braces.

eg

if ($user_ip != $ip) {
    $count_file = 'count_index.txt';

    // read contents of count.txt
    $count_file = "count_index.txt";

    // ... etc

    }   
}

Upvotes: 1

Related Questions