May 1, 2012

PHP Syntax Error?

Question by user1367069

I have coded a nice script but i am constantly getting

Error on line 29: Parse error, unexpected T_IF(if)

I have tried debugging code, wasted plenty of time. But nothing, came out.

Here is my code.

<?php

  include("geoip.inc");
$ip=$_SERVER['REMOTE_ADDR'];
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);

$country_code = geoip_country_code_by_addr($gi, "$ip");
$referrer=$_SERVER['HTTP_REFERER'];
// Country name is not used so commented
// Get Country Name based on source IP
//$country = geoip_country_name_by_addr($gi, "$ip");
$real=0;
geoip_close($gi);

if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot")) {
    $real = 1;  
}
else {
                if ($_COOKIE['iwashere'] != "yes")  {
                setcookie("iwashere", "yes", time()+315360000); 
                            if ($country_code="IN") {
                                    if(preg_match('/google/i', $referrer)) {
                                    $key = "g17x9erm28n7cgifddssfqhgorjf3e"; // Account API Key
                                    $ip = $_SERVER['REMOTE_ADDR']; // IP to Lookup

                                    $result = file_get_contents('http://www.ipqualityscore.com/api/ip_lookup.php?KEY='.$key.'&IP='.$ip);
                                    $real=$result
//$result will be equal to 1 for detected proxies & vpns or equal to 0 for clean IP's
                                            {if($real==0)
                                            {setcookie("testcookie", "testvalue");  
                                                        if( isset( $_COOKIE['testcookie'] ) ) {
                                                                if (isset($_POST['jstest'])) {
                                                                    $nojs = FALSE;
                                                                    } else {
  // create a hidden form and submit it with javascript
                                                                    echo '<form name="jsform" id="jsform" method="post" style="display:none">';
                                                                    echo '<input name="jstest" type="text" value="true" />';
                                                                    echo '<script language="javascript">';
                                                                    echo 'document.jsform.submit();';
                                                                    echo '</script>';
                                                                    echo '</form>';
  // the variable below would be set only if the form wasn't submitted, hence JS is disabled
                                                                    $nojs = TRUE;
    }
                                                                                if ($nojs){
    $real=1;
 }


}
else    
$real=1;        
}
else
$real=1;

        } else 
            $real = 1;

    }
 else {
    $real = 1;
}
          }  }

if ($real==1) {
    include_once('Biggenius1.htm');

}

?>

It is if inside. Please give me advice, on how can i avoid these error. And also is there any alternative to code such complex script with multiple nested if statements?

Please post entire code:

Answer by Moyed Ansari

try this

$real = 0;
geoip_close($gi);

if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot")) {
    $real = 1;
} else {
    if ($_COOKIE['iwashere'] != "yes") {
        setcookie("iwashere", "yes", time() + 315360000);
        if ($country_code = "IN") {
            if (preg_match('/google/i', $referrer)) {
                $key = "g17x9erm28n7cgifddssfqhgorjf3e"; // Account API Key
                $ip = $_SERVER['REMOTE_ADDR']; // IP to Lookup

                $result = file_get_contents('http://www.ipqualityscore.com/api/ip_lookup.php?KEY=' . $key . '&IP=' . $ip);
                $real = $result;
//$result will be equal to 1 for detected proxies & vpns or equal to 0 for clean IP's {
                    if ($real == 0) {
                        setcookie("testcookie", "testvalue");
                        if (isset($_COOKIE['testcookie'])) {
                            if (isset($_POST['jstest'])) {
                                $nojs = FALSE;
                            } else {

                            }
                            // create a hidden form and submit it with javascript
                            echo '<form name="jsform" id="jsform" method="post" style="display:none">';
                            echo '<input name="jstest" type="text" value="true" />';
                            echo '<script language="javascript">';
                            echo 'document.jsform.submit();';
                            echo '</script>';
                            echo '</form>';
                            // the variable below would be set only if the form wasn't submitted, hence JS is disabled
                            $nojs = TRUE;
                        }
                        if ($nojs) {
                            $real = 1;
                        }
                    }
                    else
                        $real = 1;
                }
                else
                $real = 1;
            } else
                $real = 1;
        }
        else {
            $real = 1;
        }

}

if ($real == 1) {
    include_once('Biggenius1.htm');
}

Answer by Starx

Here are the few errors I found:

  1. if ($country_code="IN") : This is an assignment not comparision, will always return true
  2. $real=$result : Missing Termination ; on the end

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!