demsd, 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.