1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

unexpected T_ELSE

Discussion in 'PHP' started by dotson83, Jun 15, 2007.

  1. #1
    I'm pretty new to php and I'm having problems with this little script. Every time I run it I get the "Parse error: syntax error, unexpected T_ELSE in /var/www/html/ip_log.php on line 19" eror.
    Could someone please let me know what I'm doing wrong?

    
    <?php
    
    $ip =$_SERVER[REMOTE_ADDR]; 
    
    $ips=file("ip-log.txt");
    
    if(in_array($ip,$ips));
    
    {
    
        $fp= fopen("ip-log.txt","a")or die('can\'t open file ip-log');
    
        fwrite($fp,"$ip \n")or die('can\'t write to file ip-log');
    
        fclose($fp)or die('can\'t close file ip-log');
    
        echo "your ip is not unique";
    
    }
    
    
    else
    
    
    
    {   
        $uh=fopen("uip-log.txt","a")or die('can\'t open file uip-log');
        fwrite($uh,"$ip \n")or die('can\'t write to file uip-log');
        fclose($uh)or die('can\'t close file uip-log');
    
        echo  "your ip is unique";
    
    
    
    }
    
    
    
    
    
    
    
    ?> 
    
    
    Code (markup):
    BTW... I'm using Bluefish for the editor and the highlighting messes up sometimes. Does anyone have any other recommendations that will work on a "nix" box?

    Thanks for the help
     
    dotson83, Jun 15, 2007 IP
  2. lemaitre

    lemaitre Peon

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Change these lines:

    $ip =$_SERVER[REMOTE_ADDR]; 
    $ips=file("ip-log.txt");
    if(in_array($ip,$ips));
    PHP:
    to this:

    $ip =$_SERVER["REMOTE_ADDR"]; 
    $ips=file("ip-log.txt");
    if(in_array($ip,$ips))
    PHP:
    This fixes two problems I saw:

    1. You should quote "REMOTE_ADDR" for it to work properly.

    2. Extra semicolon after the if statement. This one was causing the syntax error you got. Semicolons separate statements, they don't end lines. If you use a semicolon at the end of every line, PHP will not be able to parse your program properly.
     
    lemaitre, Jun 15, 2007 IP
  3. dotson83

    dotson83 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks. That fixed the error but now I have another problem... The script ALWAYS runs the else statement. Why? I'm really thankful for the help.

    P.S I thought I figured it out but it still does the same thing. This is the new code.
    <?php
    
    $ip =$_SERVER["REMOTE_ADDR"]; 
    
    $ips=file("ip-log.txt");
    
    if(in_array($ip,$ips))
    
    {
    
        $fp= fopen("ip-log.txt","a")or die('can\'t open file ip-log');
    
        fwrite($fp,"$ip \n")or die('can\'t write to file ip-log');
    
        fclose($fp)or die('can\'t close file ip-log');
    
        echo "your ip is not unique";
    
    }
    
    
    else
    
    
    
    {   
        $uh=fopen("uip-log.txt","a")or die('can\'t open file uip-log');
        fwrite($uh,"$ip \n")or die('can\'t write to file uip-log');
        fclose($uh)or die('can\'t close file uip-log');
    
        echo  "your ip is unique";
        $fp= fopen("ip-log.txt","a")or die('can\'t open file ip-log');
    
        fwrite($fp,"$ip \n")or die('can\'t write to file ip-log');
    
        fclose($fp)or die('can\'t close file ip-log');
    
    
    
    }
    
    
    
    
    
    
    
    ?> 
    
    
    Code (markup):
     
    dotson83, Jun 15, 2007 IP
  4. lemaitre

    lemaitre Peon

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I see a couple possible problems here but the main thing is that you're not getting a match because the entries in the file contain white space. You can remove that with trim():

    <?php
    
    $ip =$_SERVER["REMOTE_ADDR"]; 
    $ips0=file("ip-log.txt");
    $ips = array_map("trim", $ips0);
    if(in_array($ip,$ips))
    {
        // by the way, why are you writing the ip to the file again if
        // it's already there???
        $fp= fopen("ip-log.txt","a")or die('can\'t open file ip-log');
        fwrite($fp,"$ip \n")or die('can\'t write to file ip-log');
        fclose($fp)or die('can\'t close file ip-log');
        echo "your ip is not unique";
    }
    else
    {   
        $uh=fopen("uip-log.txt","a")or die('can\'t open file uip-log');
        fwrite($uh,"$ip \n")or die('can\'t write to file uip-log');
        fclose($uh)or die('can\'t close file uip-log');
    
        echo  "your ip is unique";
        $fp= fopen("ip-log.txt","a")or die('can\'t open file ip-log');
        fwrite($fp,"$ip \n")or die('can\'t write to file ip-log');
        fclose($fp)or die('can\'t close file ip-log');
    }
    
    ?>
    PHP:
    I think that will get your code working, but the technique of reading the whole file into an array won't be scalable if you ever get a large number of visitors, so don't use this on a high traffic site.
     
    lemaitre, Jun 15, 2007 IP