1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP or Javascript for adverts?

Discussion in 'General PHP and MySQL Discussions' started by Ingenious, Apr 25, 2013.

  1. Ingenious

    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 :)
  2. AndyB

    AndyB Well-Known Member

    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.
  3. Ingenious

    Ingenious Well-Known Member

    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?
  4. AndyB

    AndyB Well-Known Member

    You can read and set cookies from your php code.
  5. Ingenious

    Ingenious Well-Known Member

    I know. But I understood that cookies need to be set in the header before any HTML is sent? Or has it changed now?
  6. AndyB

    AndyB Well-Known Member

    Using php you can set a cookie anywhere. You might be thinking of javascript and where that code is placed.
  7. Ingenious

    Ingenious Well-Known Member

    Are you sure? Everything I can find about them says they must be set in the header, eg.


    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.
  8. AndyB

    AndyB Well-Known Member

    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.
  9. Ingenious

    Ingenious Well-Known Member

    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.
  10. Ingenious

    Ingenious Well-Known Member

    On the subject of Javascript I've found http://www.w3schools.com/ which has some excellent tutorials. I'm surprised at the similarity with PHP, so it's not a major shock to the system to learn the basics.
  11. tyteen4a03

    tyteen4a03 Well-Known Member

    ECMAScript and PHP are both influenced by C.

    w3schools are okay for tutorials, however it has its share of critics.
  12. Adam Howard

    Adam Howard Well-Known Member

    PHP... More ad blockers support java script blocking .

    Also it makes the site faster to download php overall
  13. EasyTarget

    EasyTarget Well-Known Member

    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.
  14. Ingenious

    Ingenious Well-Known Member

    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: Jul 11, 2013

Share This Page