Age Verification Script and Tutorial

Elysian Brewing Age Verification Page

Elysian Brewing Age Verification Page

*7/27/2013 UPDATE – I have received so many inquiries and questions on this topic that I’ve decided to turn this in to a service that I offer.  Please go to http://age-verify.com for complete details.

Currently I am in the midst of a fair amount of work for Elysian Brewing. The most recent piece in place is an age verification script that is a simple yes / no question to the user which sets a session cookie and then directs them accordingly. Sounds simple right? Well, not as simple as you might think.

Considerations and Requirements

  1. This is a pretty big site and changing the site root index.html page to be the actual verification page would mean the undoing of established search engine results and crawl data.
  2. Even if I did use the site root page for age verification, every page on the site (not to mention external links), would need to have the link back to root updated with a new location if the page extension were to change.
  3. I want to prompt for the age verification regardless of what page the site visitor first gets to.  They may not necessarily come in at the site root, but rather from a blog posting, searching for a brewery location, beer profile page, etc.
  4. I only want users to be prompted once per site visit, but not every time they navigate to a different page within the site.
  5. To accomplish #3 and #4, I’ll need to set a session cookie with a server side language such as PHP.
  6. I don’t want to change any of my page extensions from .html to .php for the same reason listed above in item #2.

Check out the Elysian Brewing website to see this live and in action. Elysian Brewing

Gregg

7 thoughts on “Age Verification Script and Tutorial

  1. Thomas Garrison

    I’ve used a combination of your above instructions and the code found in the verify.php script in Elysian.

    I have 90% of it working, but there’s a problem.
    I get it to provide the pop-up screen, looking just like yours, but no matter what date of birth I enter I’m redirected to the video.
    I’m using your css and js, although I’ll change them as necessary once I have the script fully functional.

    Somewhere I’m missing the uri for the actual site.
    Can you please provide the code I’m missing?
    I’m working on a localhost first. I don’t think that should make a difference, however.

    Age Verification

    31 && (charCode 57))
    return false;

    return true;
    }
    //-->

    function jumpField(elmnt,content)
    {
    if (content.length==elmnt.maxLength)
    {
    next=elmnt.tabIndex
    if (next<document.forms[0].elements.length)
    {
    document.forms[0].elements[next].focus()
    }
    }
    }

    Welcome! Please enter your date of birth.

    <!--
    function formCheck(formobj){
    // Enter name of mandatory fields
    var fieldRequired = Array("month", "day", "year");
    // Enter field description to appear in the dialog box
    var fieldDescription = Array("month", "day", "year");
    // dialog message
    var alertMsg = "Please complete the following fields:\n";

    var l_Msg = alertMsg.length;

    for (var i = 0; i < fieldRequired.length; i++){
    var obj = formobj.elements[fieldRequired[i]];
    if (obj){
    switch(obj.type){
    case "select-one":
    if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
    alertMsg += " - " + fieldDescription[i] + "\n";
    }
    break;
    case "select-multiple":
    if (obj.selectedIndex == -1){
    alertMsg += " - " + fieldDescription[i] + "\n";
    }
    break;
    case "text":
    case "textarea":
    if (obj.value == "" || obj.value == null){
    alertMsg += " - " + fieldDescription[i] + "\n";
    }
    break;
    default:
    }
    if (obj.type == undefined){
    var blnchecked = false;
    for (var j = 0; j

    Age Verification Script Credit - Gregg Richter

    Here’s the code that I inserted into the index.php since I’m using a Joomla foundation:

    Thanks for any assistance
    Thomas

  2. Thomas Garrison

    Hmmm…php didn’t come through…

    session_start();
    if($_SERVER['REQUEST_METHOD']=='POST')
    {
    if(isset($_POST['YES']))
    {
    $redirect=isset($_GET['return'])?urldecode($_GET['return']):'./';
    $expire=isset($_GET['x']) && is_numeric($_GET['x'])?intval($_GET['x']):-1;
    if($expire==-1)
    {
    $_SESSION['verified']="yes";
    header("location: ".$redirect);
    exit(0);
    }
    if($expire==0)
    {
    setcookie("verified", "yes",mktime(0,0,0,01,01,date("Y")+30));
    $_SESSION['verified']="yes";
    header("location: ".$redirect);
    exit(0);
    }
    setcookie("verified", "yes",(time()+$expire));
    $_SESSION['verified']="yes";
    header("location: ".$redirect);
    exit(0);
    }else{
    header("location: http://www.youtube.com/watch?v=gppbrYIcR80");
    exit(0);
    }
    }

    and in the head….

    function verified(){
    $redirect_url='http://localhost/J2.5/ageverificationone.php';
    $expires=-1;
    session_start();
    $validated=false;
    if(!empty($_COOKIE["verified"])) { $validated=true; }
    if(!$validated && isset($_SESSION['verified'])) { $validated=true; }
    if(is_numeric($expires) && $expires==-1 && !isset($_SESSION['verified'])) { $validated=false; }
    if($validated) { return; }
    else {
    $redirect_url=$redirect_url."?return=".$_SERVER['REQUEST_URI']."&x=".$expires;
    Header('Location: '.$redirect_url);
    exit(0);
    }
    }
    verified();
    ?>

    Let’s see if you get that.
    Thomas

  3. gregg Post author

    Hey Thomas,

    Unless you have a MAMP or LAMP environment installed on your machine, I believe PHP scripts will not work on local host. My recommendation is to either install MAMP / LAMP or even better, build everything in a test domain. Also, FYI the tutorial above is no longer used on the Elysian site. I recently changed the Elysian site over to a D.O.B. picker as opposed to yes / no verification. The code sets (PHP and JavaScript) used in each are completely different.

    Thanks

    1. Thomas Garrison

      I use WAMP.
      I’m well aware of the changes in your script. I’m only asking that you provide, if you can, a bit more detail of what you used to make your “current” script work in Elysian.
      As I mentioned, I have it all working except the “why” it redirects to the video no matter the input.
      Is there another script of which I’m unaware? Or is the php in the index page different than what I’ve found?

      Please take another look at what I’ve written. I think there is something simple where the redirect lies.
      To confirm, I’m using the script that you have in the current Elysian site now, but the php in “my” index page is what is found in this page. I think that’s where the difference lies.

      Thanks.
      Thomas

  4. Jeppe

    Hi.. Nice Tut.. Just need to know one thing… How will it react to SEO? Think google and the others are a bit touchy when it comes to redirects..? Any ideas?

    Cheers
    J

  5. Katie

    Hi Gregg!

    I have tried to enter your php into the site, and all I’m getting is a “verify you’re 18 yes – enter no – leave” script. In fact, the html is rejecting most of the script, with the exception of:

    PLEASE VERIFY THAT YOU ARE OVER AGE 21 BEFORE ENTERING THIS SITE

    I am using a prebuilt (Web Site Tonight), and I’ve found some html that partially work. By that i mean I’m not sure what I’m missing (or rather where). I want exactly what you describe above – that before they go off the Home Page, age verification is obtained, and only obtained once throughout thier search. If i could have a cookie that is placed in there so upon a rejection (i.e. they are under 18) it prevents them from going back and re-entering a false birthday. And as a sub-note, i’d prefer it to be in MONTH DATE YEAR format, but when i cut and paste the lines, it just omits it. This is the html i have (that only half works):

    Please enter your birth date to proceed.

    Day :

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    Month :

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    Year :

    2012

    2011

    2010

    2009

    2008

    2007

    2006

    2005

    2004

    2003

    2002

    2001

    2000

    1999

    1998

    1997

    1996

    1995

    1994

    1993

    1992

    1991

    1990

    1989

    1988

    1987

    1986

    1985

    1984

    1983

    1982

    1981

    1980

    1979

    1978

    1977

    1976

    1975

    1974

    1973

    1972

    1971

    1970

    1969

    1968

    1967

    1966

    1965

    1964

    1963

    1962

    1961

    1960

    1959

    1958

    1957

    1956

    1955

    1954

    1953

    1952

    1951

    1950

    1949

    1948

    1947

    1946

    1945

    1944

    1943

    1942

    1941

    1940

    1939

    1938

    1937

    1936

    1935

    1934

    1933

    1932

    1931

    1930

    1929

    1928

    1927

    1926

    1925

    1924

    1923

    1922

    1921

    1920

    1919

    1918

    1917

    1916

    1915

    1914

    1913

    1912

    1911

    1910

    1909

    1908

    1907

    1906

    1905

    1904

    1903

    1902

    1901

    1900

    Right now, I have a Coming Soon page as i can’t have people on the site under 18. Thanks so much for your help!

    Katie

  6. sean

    This is a good neat bit of solid code that works & is easy to implement & importantly, will work with javascript turned off too.
    Much thanks!

Comments are closed.