SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Bristol
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Split testing opt-in boxes...preventing same one being displayed

    Hi,

    I wonder if one of you php gurus out there can help me?

    I've got a simple little php code snippet that rotates two different opt-in forms so I can split test them like so:-

    Code:
    
    if (rand(0,1)) {
    
     include("optinboxA.php");
    
    } else {
    
     include("optinboxB.php");
    
    }
    I have an opt-in form on every page of my site and want to ensure the same form is always displayed to each visitor

    I understand this is possible by either setting a cookie or using a session id.

    I'm not experienced in php so I'd really appreciate it if anyone could provide me with the code to do this.

    My preference is for using session id I think (although I'm open to using a cookie instead if you believe that's the better option) . In fact I'd be grateful for an explanation of the pro's and cons of each option too.

    Many thanks for any help you can provide.

    Cheers...Graham
    Graham Cox

    Is this the perfect list building tool for bloggers?
    Grab your FREE 30 day trial of Ultimate Footer Ad NOW

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If you want sessions:
    PHP Code:
    <?php
    session_start
    ();//must be at top of page, before any content has been output!
    $_SESSION['boxA'] = isset($_SESSION['boxA']) ? $_SESSION['boxA'] : rand(0,1);
    //and later on down the page....
    if ($_SESSION['boxA']) {
     include(
    "optinboxA.php");
    } else {
     include(
    "optinboxB.php");
    }
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Bristol
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Jake,

    Thank you very much for your help.

    From your comment I take it the first two lines...

    Code:
    session_start();//must be at top of page, before any content has been output!
    
    $_SESSION['boxA'] = isset($_SESSION['boxA']) ? $_SESSION['boxA'] : rand(0,1);
    have to go in the header, ie after the opening <html>, is that correct? Am I ok to put that code in an include?

    Can you confirm that that session code will display optinboxA and optinboxB 50% of the time each? I'm probably misunderstanding it but the pseudocode to me looks like:

    display optinboxA if the session is set to boxA, else rotate between optinboxA and optinboxB. Wouldn't that mean optinboxA gets shown 75% of the time and optinboxB 25%? Like I say, I'm not hot on these things so feel free to enlighten me!

    Thanks again,

    Graham
    Graham Cox

    Is this the perfect list building tool for bloggers?
    Grab your FREE 30 day trial of Ultimate Footer Ad NOW

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Those two lines should be before anything whatsoever (html, whitespace, etc) has been output - so really it's best to put them at the very top of the file, in PHP tags.

    the rand() function should theoretically give you 1:1 of each, however, in practice it may be slightly biased to A or B, but that's all down to randomness. It's practically heads/tails - in theory it should evenly spread.

    What the session does is makes sure that the user will always see the same box. It's set to a random number, if it's not already set. If it is already set, it's kept.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Bristol
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for clarifying that Jake, really appreciated.

    Cheers...Graham
    Graham Cox

    Is this the perfect list building tool for bloggers?
    Grab your FREE 30 day trial of Ultimate Footer Ad NOW

  6. #6
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Bristol
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually got it working perfect now, thanks.

    Final questions which are really just to improve my knowledge.
    1) Does using the session mean the visitor will see the same optinbox only in his browsing session or will he always see the same box (ie even after closing and reopening the browser).
    2) Is the session id cookie based?

    Thanks again

    Graham
    Graham Cox

    Is this the perfect list building tool for bloggers?
    Grab your FREE 30 day trial of Ultimate Footer Ad NOW

  7. #7
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    To enable the session for after the browser is closed, you can use this before session_start():
    PHP Code:
    session_set_cookie_params (time() + 86400); 
    This will enable the session for a day after it's last regenerated - i.e. every time the page is refreshed, it can stay closed for upto a day, upon loading the session will kick in again.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Bristol
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, thanks for the explanation and all your help Jake.

    Best wishes,

    Graham
    Graham Cox

    Is this the perfect list building tool for bloggers?
    Grab your FREE 30 day trial of Ultimate Footer Ad NOW


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •