PHP or Javascript for adverts?

Ingenious

Well-known member
I currently run a plugin in Xenforo which generates a random banner advert from a pool, and also some custom PHP widgets in Wordpress which do the same.

I wondered what people's views were on whether it is best to generate a random banner on the server using PHP, or, using Javascript to do the same thing in the browser. Does either way have advantages or disadvantages? Or is there no real difference?

One advantage I can see with Javascript is that it will allow me to cache the Wordpress files. If I cache them now would I lose the random functionality of the PHP banners (ie. it becomes static content). A cached PHP file but with Javascript would still run random banners as the script is being run in the browser.

Any thoughts? As Javascript is new to me I may of course be missing something obvious :)
 
I prefer using php as the there is less code for the visitor to download.

I use a cookie to keep track of which banner was last viewed by the visitor, so every time the forum home page is viewed it cycles to the next banner. I have a total of 63 banners.
 
I might have to look into cookies. Am I right in thinking that cookie related stuff has to be done in the page header (before any content is served) rather than in the advert code itself where it appears in the page?
 
I might have to look into cookies. Am I right in thinking that cookie related stuff has to be done in the page header (before any content is served) rather than in the advert code itself where it appears in the page?

You can read and set cookies from your php code.
 
I know. But I understood that cookies need to be set in the header before any HTML is sent? Or has it changed now?
 
I know. But I understood that cookies need to be set in the header before any HTML is sent? Or has it changed now?

Using php you can set a cookie anywhere. You might be thinking of javascript and where that code is placed.
 
Are you sure? Everything I can find about them says they must be set in the header, eg.

http://php.net/manual/en/function.setcookie.php

Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.

This makes their use for me a little more complicated since my banner PHP runs within the page, though I am sure as always there's a way to achieve this in the header (maybe just using a hook there to run the PHP). I don't know if this restriction also applies with Javascript, though using Javascript in the head is much easier as I guess it's a simple template edit.
 
Here's a flow chart of how I created my banners.

1) Create Add-on
2) The Add-on hook is on the following: ad_above_top_breadcrumb
3) Create a php script the Add-on calls

The script in step 3 reads and sets cookies and rotates the banners.
 
Actually I think we're at cross purposes. I'm thinking about just the basic mechanics of setting cookies, which has to be done in the header. I am sure Xenforo has methods to set cookies via a plugin and though it actually sets them before any output is sent we probably don't have to worry about that bit. I can take a look at an existing plugin that uses cookies within Xenforo and see how it all works :) But if you wrote a PHP script from scratch, you would need to set cookies in the header, not in the body where the advert is.
 
Actually I think we're at cross purposes. I'm thinking about just the basic mechanics of setting cookies, which has to be done in the header. I am sure Xenforo has methods to set cookies via a plugin and though it actually sets them before any output is sent we probably don't have to worry about that bit. I can take a look at an existing plugin that uses cookies within Xenforo and see how it all works :) But if you wrote a PHP script from scratch, you would need to set cookies in the header, not in the body where the advert is.
You need to set the cookie before outputting HTML. XF uses output buffering to help avoid issues.

You can change the value of the cookie with JavaScript if needed after the page loads.
 
Thanks guys - since I created this thread I have dropped the desire to use cookies and continue to render adverts randomly. I'm still using PHP to do this but will change over to JS at some point since I want to cache the wordpress site to the max :) (so will move the random banner element to the user's browser).

Edit - Have now changed the advert rotation from PHP to JS in the browser, then cached the Wordpress site using a cache plugin, it runs about 100 times quicker now and the banners still rotate :)
 
Last edited:
Top Bottom