Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

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


  • Please log in to reply

#1
demsd

demsd

    New Member

  • Member
  • Pip
  • 4 posts
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;
  }
}
?>

  • 0

Advertisements


#2
Raikia

Raikia

    Member

  • Member
  • PipPip
  • 17 posts

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.
  • 0

#3
demsd

demsd

    New Member

  • Topic Starter
  • Member
  • Pip
  • 4 posts
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();
?>

  • 0

#4
Raikia

Raikia

    Member

  • Member
  • PipPip
  • 17 posts
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.

Edited by Raikia, 14 January 2011 - 01:44 PM.

  • 0

#5
demsd

demsd

    New Member

  • Topic Starter
  • Member
  • Pip
  • 4 posts
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?
  • 0

#6
Raikia

Raikia

    Member

  • Member
  • PipPip
  • 17 posts
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).
  • 0

#7
demsd

demsd

    New Member

  • Topic Starter
  • Member
  • Pip
  • 4 posts
Sweet.

Thanks for the help! :D
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP