PDA

View Full Version : Plugins and Agents and Scripts, Oh my...


David LeBow
11-15-2008, 05:27 AM
And I thought that I was trying something simple...

I have a little plugin, short and stout:

$Rotatordata = file_get_contents('http://www.MySite.Com/Rotator.asp');

The output of the asp page gets inserted in the header template like this:

$Rotatordata

It's some custom ad rotator logic. Works like a charm. Then I thought I'd get clever and query the HTTP User Agent within the ASP's VBScript, something like this:

sAgent = Request.ServerVariables ("HTTP_USER_AGENT")

I'd like to disregard requests from web crawlers. If I request Rotator.asp from the browser, everything's fine. Once I embed it as shown above into the PHP Scripting and template in vBulletin, sAgent always returns an empty string.

Googling, I stumbled on this, but don't know to what extent it's true:

"file("yoururl"); will read the page and send an empty UA [User Agent] by default, this is sometimes done to filter your page, remove javascript, flash or java."

I expect that file and file_get_contents act in a similar way in this regard (?).

My question: is there any way to get at the user agent within the asp within the plugin called by PHP script within the VBulletin template?

Thanks in advance for any input.

Dismounted
11-15-2008, 09:15 AM
Add a GET variable to the end of the URL string appending the UA, maybe?

David LeBow
11-15-2008, 01:30 PM
Thanks for the prompt reply. I'm afraid I don't see what you're suggesting, though.

Within the ASP the syntax is correct and works fine outside of the scripted PHP VBulletin environment. It seems as if the environment available when processing the ASP doesn't give access to the request elements (?) somehow.

:confused:

Dismounted
11-17-2008, 04:00 AM
Exactly - so pass the variable from within PHP onto the ASP application (e.g. as a GET variable on the end of the URL).

David LeBow
11-17-2008, 06:28 AM
...the light slowly dawns, even on me.

Last bonus question (honest) - do you happen to know the vBulletin / PHP syntax of the user agent from within a header template? I've been coding all manner of things for years, but have had PHP foisted on me to support the forum.

Thanks from the other side of the planet.

Dismounted
11-17-2008, 08:48 AM
$_SERVER['HTTP_USER_AGENT']
Keep this page for reference: http://www.php.net/manual/en/reserved.variables.server.php.

David LeBow
11-17-2008, 11:35 AM
Superb.

It took a bit more tweaking than I thought, but for anyone who cares, the final plugin code looks like this:

$UserAgent = $_SERVER['HTTP_USER_AGENT'];
$UserAgent = str_replace(" ", "%20", $UserAgent);
$Rotatordata = file_get_contents('http://www.MySite.biz/Rotator.asp?UA=' . $UserAgent);

the ASP page can read the user agent like this:

sAgent = Request.QueryString ("UA")

and all is right with the world.

Thanks for your help!

Dismounted
11-18-2008, 03:27 AM
Instead of doing a S&R on the string, use rawurlencode(). I'm sure ASP has a "reverse" function.