Parse error: syntax error, unexpected T_IF, expecting '{' in - Geeks to Go Forums

Jump to content

Log in Register Register Malware removal guide How it works

Parse error: syntax error, unexpected T_IF, expecting '{' in

#1 demsd

  • Group: Member
  • Posts: 4
  • Joined: 12-January 11

Posted 12 January 2011 - 11:46 AM

Not a PHP guru. Receiving a parse error on line 3.

Parse error: syntax error, unexpected T_IF, expecting '{' in

<?
function check_search_engine()
  if ($a == 1) {
    header("Location: http://www.affiliate.com/link.php");
    exit;
  }
{
	IF(!isset($_SERVER['HTTP_USER_AGENT']))
	{
		$user_agent = '';
	}
	ELSE
	{
		$user_agent = $_SERVER['HTTP_USER_AGENT'];
	}

	$search_engines[] = 'Fast';
	$search_engines[] = 'Slurp';
	$search_engines[] = 'Ink';
	$search_engines[] = 'Atomz';
	$search_engines[] = 'Scooter';
	$search_engines[] = 'Crawler';
	$search_engines[] = 'bot';
	$search_engines[] = 'Genius';
	$search_engines[] = 'AbachoBOT';
	$search_engines[] = 'AESOP_com_SpiderMan';
	$search_engines[] = 'ia_archiver';
	$search_engines[] = 'Googlebot';
	$search_engines[] = 'UltraSeek';
	$search_engines[] = 'Google';

	foreach ($search_engines as $key => $value) 
	{
		IF($user_agent != '')
		{
			if(strstr($user_agent, $value)) 
			{
				$is_search_engine = 1;
			}
		}
	}

 	IF(isset($is_search_engine)) 
	{
		return TRUE;
 	}
 	else
 	{
		return FALSE;
 	}
}
?>
<?
if (check_search_engine() == TRUE) {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta name="robots" content="noindex, nofollow">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
</body>
</html>
<?
} else {
  $a = $_REQUEST['a'];
  if ($a == 1) {
    header("Location: http://www.affiliate.com/link1.php");
    exit;
  }
  if ($a == 2) {
    header("Location: http://www.affiliate.com/link2.php");
    exit;
  }
  if ($a == 3) {
    header("Location: http://www.affiliate.com/link3.php");
    exit;
  }
}
?>


#2 Raikia

  • Group: Member
  • Posts: 17
  • Joined: 10-January 11

Posted 12 January 2011 - 01:14 PM

View Postdemsd, on 12 January 2011 - 11:46 AM, said:

Not a PHP guru. Receiving a parse error on line 3.

Parse error: syntax error, unexpected T_IF, expecting '{' in

<?
function check_search_engine()
  if ($a == 1) {
    header("Location: http://www.affiliate.com/link.php");
    exit;
  }
{
	IF(!isset($_SERVER['HTTP_USER_AGENT']))
	{
		$user_agent = '';
	}
	ELSE
	{
		$user_agent = $_SERVER['HTTP_USER_AGENT'];
	}

	$search_engines[] = 'Fast';
	$search_engines[] = 'Slurp';
	$search_engines[] = 'Ink';
	$search_engines[] = 'Atomz';
	$search_engines[] = 'Scooter';
	$search_engines[] = 'Crawler';
	$search_engines[] = 'bot';
	$search_engines[] = 'Genius';
	$search_engines[] = 'AbachoBOT';
	$search_engines[] = 'AESOP_com_SpiderMan';
	$search_engines[] = 'ia_archiver';
	$search_engines[] = 'Googlebot';
	$search_engines[] = 'UltraSeek';
	$search_engines[] = 'Google';

	foreach ($search_engines as $key => $value) 
	{
		IF($user_agent != '')
		{
			if(strstr($user_agent, $value)) 
			{
				$is_search_engine = 1;
			}
		}
	}

 	IF(isset($is_search_engine)) 
	{
		return TRUE;
 	}
 	else
 	{
		return FALSE;
 	}
}
?>
<?
if (check_search_engine() == TRUE) {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta name="robots" content="noindex, nofollow">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
</body>
</html>
<?
} else {
  $a = $_REQUEST['a'];
  if ($a == 1) {
    header("Location: http://www.affiliate.com/link1.php");
    exit;
  }
  if ($a == 2) {
    header("Location: http://www.affiliate.com/link2.php");
    exit;
  }
  if ($a == 3) {
    header("Location: http://www.affiliate.com/link3.php");
    exit;
  }
}
?>



This is quite a simple error. The first IF statement of yours (on line 3) isn't within the function declaration. You must move that part into the curly brackets (that start on line 7). The fixed code would look like:

<?
function check_search_engine()
{
        if ($a == 1) {
                header("Location: http://www.affiliate.com/link.php");
                exit;
        }
        IF(!isset($_SERVER['HTTP_USER_AGENT']))
        {
                $user_agent = '';
        }
        ELSE
        {
                $user_agent = $_SERVER['HTTP_USER_AGENT'];
        }

        $search_engines[] = 'Fast';
        $search_engines[] = 'Slurp';
        $search_engines[] = 'Ink';
        $search_engines[] = 'Atomz';
        $search_engines[] = 'Scooter';
        $search_engines[] = 'Crawler';
        $search_engines[] = 'bot';
        $search_engines[] = 'Genius';
        $search_engines[] = 'AbachoBOT';
        $search_engines[] = 'AESOP_com_SpiderMan';
        $search_engines[] = 'ia_archiver';
        $search_engines[] = 'Googlebot';
        $search_engines[] = 'UltraSeek';
        $search_engines[] = 'Google';

        foreach ($search_engines as $key => $value) 
        {
                IF($user_agent != '')
                {
                        if(strstr($user_agent, $value)) 
                        {
                                $is_search_engine = 1;
                        }
                }
        }

        IF(isset($is_search_engine)) 
        {
                return TRUE;
        }
        else
        {
                return FALSE;
        }
}
?>
<?
if (check_search_engine() == TRUE) {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta name="robots" content="noindex, nofollow">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
</body>
</html>
<?
} else {
  $a = $_REQUEST['a'];
  if ($a == 1) {
    header("Location: http://www.affiliate.com/link1.php");
    exit;
  }
  if ($a == 2) {
    header("Location: http://www.affiliate.com/link2.php");
    exit;
  }
  if ($a == 3) {
    header("Location: http://www.affiliate.com/link3.php");
    exit;
  }
}
?>


Let me point out, however, you have a couple logic errors in this code snippet. The variable $a at the beginning hasn't been declared yet (and isn't declared as global). You also have a significant problem with not declaring variables. While PHP does not require static binding, it is always a good idea to initialize a variable outside of any conditional statements. This is to ensure that the logic works correctly and even for the security (especially if register globals are on). In addition, in the "foreach" statement, you don't need "foreach ($search_engines as $key => $value)". Instead, use "foreach ($search_engines as $value)" because you never actually use "$keys" (nor do you make them for the array).


Actually, now that I look at this code, I have no idea what you are trying to do. Are you trying to detect if the user is a bot and then redirecting them? I don't think this function will succeed in the way that you want it to. If you give me more specifics on what you are trying to do, I may be able to code it for you.

I hope this helps some.

#3 demsd

  • Group: Member
  • Posts: 4
  • Joined: 12-January 11

Posted 13 January 2011 - 09:55 PM

I didn't code it, and I don't think it will do what it was supposed to. It needed to mask affiliate links.

Although I have not tried this yet, this is what I have as a way to mask links.

<?php

if ($p == "1") {$link = "http://www.partner.com";}
if ($p == "2") {$link = "http://www.partner1.com";}
if ($p == "3") {$link = "http://www.partner2.com";}
if ($p == "4") {$link = "http://www.partner3.com";}

header("Location: $link");
exit();
?>


#4 Raikia

  • Group: Member
  • Posts: 17
  • Joined: 10-January 11

Posted 14 January 2011 - 01:43 PM

Yes, that would work (assuming you initialize the variable $p). Also, you will want to make $link have a value before the conditionals. For example, what if $p does not equal 1, 2, 3, or 4? The script would fail because $link is not assigned anything.

There is an easier way to do this that is easier to maintain, also.

$links = array('http://www.partner.com', 'http://www.partner1.com', 'http://www.partner2.com', 'http://www.partner3.com');
if ($p <= count($links) && $p > 0) // This makes sure $p is within range of the array
    header('Location:'.$links[$p-1]);
exit;


With that code, all you have to do to add partner links is keep adding to the array. It has the same triggers as your code, but without the copy and pasting of code. If you have 100 partner links, it would be nightmare code to have it copy and pasted 100 times. All you would have to do with my code is make the $links array have 100 values.

The code above still makes: when $p == 1, it goes to http://www.partner.com, when $p == 2, it goes to http://www.partner1.com, etc.


You have to be sure that $p is initialized though (either through a REQUEST or something else). It is a very bad idea to rely on "register globals" to initialize variables. If you don't know what register globals are, read: http://php.net/manua...ity.globals.php


I hope this helps.

#5 demsd

  • Group: Member
  • Posts: 4
  • Joined: 12-January 11

Posted 14 January 2011 - 08:43 PM

That does seem simpler.

I did forget to mention that what was intended with my version was to link to a php file [e.g. partners.php] using a link structure such as mydomain.com/partners.php?p=1, where p=1 is the affiliate link to partner 1, etc.

Does that make sense?

#6 Raikia

  • Group: Member
  • Posts: 17
  • Joined: 10-January 11

Posted 15 January 2011 - 12:40 AM

Yes, that does make sense. To do that, you just add 1 more line to the code I already wrote.

$p = $_GET['p'];
$links = array('http://www.partner.com', 'http://www.partner1.com', 'http://www.partner2.com', 'http://www.partner3.com');
if ($p <= count($links) && $p > 0)
    header('Location:'.$links[$p-1]);
exit;


If you save that code as a file "partners.php", it will work just how you want it to (obviously with changing the array though).

#7 demsd

  • Group: Member
  • Posts: 4
  • Joined: 12-January 11

Posted 16 January 2011 - 03:38 PM

Sweet.

Thanks for the help! :D

Share this topic: