Jump to content

Recommended Posts

Well my knowledge of php is even poorer than first imagined! Im trying to do something simple but jsut cant get it to work and would really appreiciate some help.

Im trying to detect whether the user agent is a robot and if it is to send it to one page, and if its not to send it to another.

[code]<html>
<head>
<title>Test</title>
</head>
<body>

<?php

$botlist = array("alexa", "appie", "Ask Jeeves", "crawler", "FAST",
"froogle", "Firefly", "girafabot", "Googlebot", "InfoSeek", "inktomi",
"looksmart", "NationalDirectory", "rabaz", "Scooter", "Slurp", "Spade",
"TECNOSEEK", "Teoma", "WebBug", "WebFindBot", "URL_Spider_SQL",
"ZyBorg");

                function detectBrowser($agent) {
                                    if (eregi("botlist", $agent)) {
                                        $browser = "Bot";
                      } else {
                                        $browser = "Browser";
                      }
                                return $browser;
                }
?>

<?php

$user_agent = $HTTP_SERVER_VARS["HTTP_USER_AGENT"];
$isBrowser = detectBrowser($user_agent);

if ($isBrowser=="Bot") {
echo "<a href='http://www.sitea.com">";}

else {
echo "<a href='http://www.siteb.com">";


}
?>


</body>
</html>[/code]

But im not quite sure what im doing wrong? I dont want to redirect the user i just want the hyperlink to change

Any ideas would be most appreciated.

Thanks
Alex
Link to comment
https://forums.phpfreaks.com/topic/15510-simple-if-statement-to-add-hyperlink/
Share on other sites

Thankyou wildteen88 school boy error!!!  ::)

I have changed the code to the following, it is writing the urls but is not changing when seen by a bot?

Any ideas most welcome

Thanks

[code]<html>
<head>
<title>Test</title>
</head>
<body>

<?php

$botlist = array("alexa", "appie", "Ask Jeeves", "crawler", "FAST",
"froogle", "Firefly", "girafabot", "Googlebot", "InfoSeek", "inktomi",
"looksmart", "NationalDirectory", "rabaz", "Scooter", "Slurp", "Spade",
"TECNOSEEK", "Teoma", "WebBug", "WebFindBot", "URL_Spider_SQL",
"ZyBorg");

                function detectBrowser($agent) {
                                    if (eregi("botlist", $agent)) {
                                        $browser = "Bot";
                      } else {
                                        $browser = "Browser";
                      }
                                return $browser;
                }
?>

<?php

$user_agent = $HTTP_SERVER_VARS["HTTP_USER_AGENT"];
$isBrowser = detectBrowser($user_agent);
if ($isBrowser=="Bot") {

echo '<a href="http://www.sitea.com">site a</a>';


} else {

echo '<a href="http://www.siteb.com">site b</a>';


}
?>


</body>
</html>[/code]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.