Zen Cart Logo
Forums / Templates, Stylesheets, Page Layout / Age verification with cookie in header

Age verification with cookie in header

Views: 2,332

Results 1 to 9 of 9
8 Mar 2015, 5:49 PM
#1
castlepyro avatar

castlepyro

New Zenner

Join Date:
Apr 2011
Posts:
6
Plugin Contributions:
0

Age verification with cookie in header

First, thank you all that have posted help on this site. It has assisted me in numerous zencart installs for myself and friends.

I am having a small problem with a age verification pop-up that I am trying to implement on a new install of zencart v 1.51
the website is inaprops.com
and I am trying to create a popup that will prompt the user to acknowledge that they are 18 or older before proceeding to the website. The popup is working correctly, but the cookie is not being created (at least when I look at the the site with firebug). Does anything pop out to anyone else with this code:

<script type="text/javascript"> function setCookie(cname,cvalue,exdays) { var d = new Date(); d.setTime(d.getTime() + (exdays*24*60*60*1000)); var expires = "expires=" + d.toGMTString(); document.cookie = cname+"="+cvalue+"; "+expires; } function getCookie(cname) { var name = cname + "="; var ca = document.cookie.split(';'); for(var i=0; i<ca.length; i++) { var c = ca\[i]; while (c.charAt(0)==' ') c = c.substring(1); if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ""; } var ofAge = getCookie("ofAge"); if (ofAge != "true") { var answer = confirm ("Are you 18 or older?"); if (answer = ok) { setCookie("ofAge","true",30); // document.location="http://www.inaprops.com"; // This line is not needed and will redirect the user back to the main page } else { alert('Sorry, we can not let you in!'); document.location="http://www.google.com"; } } </script>

Thank you all in advance for any insight that you can give on this.

9 Mar 2015, 10:12 PM
#2
castlepyro avatar

castlepyro

New Zenner

Join Date:
Apr 2011
Posts:
6
Plugin Contributions:
0

Re: Age verification with cookie in header

Okay, I think I have the answer. I changed the lines int he bottom from:

var ofAge = getCookie("ofAge");
if (ofAge != "true") {
var answer = confirm ("Are you 18 or older?");
if (answer = ok) {
setCookie("ofAge","true",30);
// document.location="http://www.inaprops.com"; // This line is not needed and will redirect the user back to the main page
} else {
alert('Sorry, we can not let you in!');
document.location="http://www.google.com";
}
}

to:
var ofAge = getCookie("ofAge");
if (ofAge != "true") {
var answer = confirm ("Are you 18 or older?");
if (answer == true) {
setCookie("ofAge","true",1);
// document.location="http://www.inaprops.com"; // This line is not needed and will redirect the user back to the main page
} else {
alert('Sorry, we can not let you in!');
document.location="http://www.google.com";
}
}

</script>
1 Aug 2015, 7:48 AM
#3
mysh avatar

mysh

New Zenner

Join Date:
Nov 2011
Posts:
54
Plugin Contributions:
0

Re: Age verification with cookie in header

thanks for posting this. IT seems to be working for me with the change in your second post.

For anyone who knows about this type of script. Where is the best place to put this? I currently have it in index.php but not at the top. When I put these types of scripts at the very top of indiex.php it changes my font size and spacing on the site.
I put it towards the bottom of index.php and it seems to be working but I worry it might be causing other issues that aren't as obvious. Also worry if this can affect browser indexing etc. since the pop up gets in the way.
Is there an ideal spot to put this script without causing any side effects? I want it to pop up when a person first comes to the site but just once.

1 Aug 2015, 2:29 PM
#4
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Age verification with cookie in header

mysh:

thanks for posting this. IT seems to be working for me with the change in your second post.

For anyone who knows about this type of script. Where is the best place to put this? I currently have it in index.php but not at the top. When I put these types of scripts at the very top of indiex.php it changes my font size and spacing on the site.
I put it towards the bottom of index.php and it seems to be working but I worry it might be causing other issues that aren't as obvious. Also worry if this can affect browser indexing etc. since the pop up gets in the way.
Is there an ideal spot to put this script without causing any side effects? I want it to pop up when a person first comes to the site but just once.

No, no, no...

First of all, edits of index.php should be minimized to those absolutely necessary... Further some time and review of the way zen cart operates should be taken.

The above script does the following: if a cookie hasn't been set validating the person is of age, then ask the person. If their answer is yes, then set/extend the cookie so that when the validation is attempted that it simply gets extended provided the cookie hasn't expired.
If the person says no not of age, then they are redirected to google, no cookie is set. Next arrival at the site will cause the question to be asked again.

As for how/where to place the above code. My suggeston would be to put it in a jscript_PICK_A_NAME.php file located in the includes/templates/YOUR_TEMPLATE/jscript directory. This way every page load will refresh the cookie.

28 Aug 2015, 7:35 AM
#5
freddiec2385 avatar

freddiec2385

New Zenner

Join Date:
May 2015
Posts:
21
Plugin Contributions:
0

Re: Age verification with cookie in header

Not sure if this is the correct location for this or I should open a new thread, but I am encountering the following issue with a PHP based age verification: I am placing the following code in index.php

session_start();
    if(! isset( $_SESSION['age_verification'] ) or $_SESSION['age_verification'] != true ) die( header("Location: checkage.php?

url=http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]") );

    //session_destroy(); //

    echo 'hello over 18 person';

Then I am placing a file called checkage.php in the root folder of my cart with the following code:

<?php

        // checkage.php
    session_start();

    // checkage.php
    if( isset( $_POST['yes'] ) )
    {
        $_SESSION['age_verification'] = true;

        if( isset( $_GET['url'] ) )
        {
            die( header('Location: ' . $_GET['url'] ) ); 
        } 
        else
        {
            die( header('Location: index.php') );
        }
    }
    elseif( isset( $_POST['no'] ) )
    {
        $_SESSION['age_verification'] = false;
    }

    // The below line will check if someone has already said no so you can show them a page which tells them to they are too young.
    if( isset( $_SESSION['age_verification'] ) and $_SESSION['age_verification'] != true ) die( header('Location: [url]http://www.google.com[/url]' 

);

    ?>

    <form method="POST" >
    <fieldset>
    <legend>Are you over 18?</legend>
    <input type="submit" name="yes" value="Yes" /> <input type="submit" name="no" value="No" />
    </fieldset>
    </form>

It gives me a 500 internal error. Any suggestions as to what I'm doing wrong?

28 Aug 2015, 8:50 AM
#6
design75 avatar

design75

Totally Zenned

Join Date:
Dec 2009
Location:
Amersfoort, The Netherlands
Posts:
2,862
Plugin Contributions:
5

Re: Age verification with cookie in header

if( isset( $_SESSION['age_verification'] ) and $_SESSION['age_verification'] != true ) die( header('Location: http://www.google.com'   );

should be:

if( isset( $_SESSION['age_verification'] ) and $_SESSION['age_verification'] != true ) die( header('Location: http://www.google.com'   ));

You missed a closing )

28 Aug 2015, 3:45 PM
#7
freddiec2385 avatar

freddiec2385

New Zenner

Join Date:
May 2015
Posts:
21
Plugin Contributions:
0

Re: Age verification with cookie in header

Design75:

if( isset( $_SESSION['age_verification'] ) and $_SESSION['age_verification'] != true ) die( header('Location: http://www.google.com' );

> should be:
> ```php
if( isset( $_SESSION['age_verification'] ) and $_SESSION['age_verification'] != true ) die( header('Location: http://www.google.com'   ));

You missed a closing )

Thank you for the response Design75. I updated the code and I still receive a 500 internal error after the first instance of this code. I wonder if where I place the code on the index.php file matters. The template I am running is Winchester responsive. Currently I am placing the following at the top of index.php:

    <?php
session_start();
    if(! isset( $_SESSION['age_verification'] ) or $_SESSION['age_verification'] != true ) die( header("Location: checkage.php?url=http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]") );

    // session_destroy(); // uncomment to reset for testing and debugging.

?>

Here is the checkage.php file:

<?php

   
    // checkage.php
    session_start();

    // checkage.php
    if( isset( $_POST['yes'] ) )
    {
        $_SESSION['age_verification'] = true;

        if( isset( $_GET['url'] ) )
        {
            die( header('Location: ' . $_GET['url'] ) ); 
        } 
        else
        {
            die( header('Location: index.php') );
        }
    }
    elseif( isset( $_POST['no'] ) )
    {
        $_SESSION['age_verification'] = false;
    }

    // The below line will check if someone has already said no so you can show them a page which tells them to they are too young.
    if( isset( $_SESSION['age_verification'] ) and $_SESSION['age_verification'] != true ) die( header('Location: http://www.google.com'));

    ?>

    <form method="POST" >
    <fieldset>
    <legend>Are you over 18?</legend>
    <input type="submit" name="yes" value="Yes" /> <input type="submit" name="no" value="No" />
    </fieldset>
    </form>
28 Aug 2015, 3:49 PM
#8
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Age verification with cookie in header

On a side note, with all the code placed where it is, the form is not presented on a properly setup html page... There's no html, header, or body tags applied from what it looks like...

Also guessing that you don't want your webpage indexed? As how will an SE navigate the page under the current setup?

28 Aug 2015, 4:55 PM
#9
freddiec2385 avatar

freddiec2385

New Zenner

Join Date:
May 2015
Posts:
21
Plugin Contributions:
0

Re: Age verification with cookie in header

mc12345678:

On a side note, with all the code placed where it is, the form is not presented on a properly setup html page... There's no html, header, or body tags applied from what it looks like...

Also guessing that you don't want your webpage indexed? As how will an SE navigate the page under the current setup?

Hello mc12345678,

Attached is a copy of my index.php . I am not too proficient in PHP. I would like to have SE navigate the page if possible.

<?php
   
session_start(); 
    if(! isset( $_SESSION['age_verification'] ) or $_SESSION['age_verification'] != true ) die( header("Location: checkage.php?

url=http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]") ); 

    // session_destroy(); // uncomment to reset for testing and debugging.


/**
 * index.php represents the hub of the Zen Cart MVC system
 * 
 * Overview of flow
 * <ul>
 * <li>Load application_top.php - see {@tutorial initsystem}</li>
 * <li>Set main language directory based on $_SESSION['language']</li>
 * <li>Load all *header_php.php files from includes/modules/pages/PAGE_NAME/</li>
 * <li>Load html_header.php (this is a common template file)</li>
 * <li>Load main_template_vars.php (this is a common template file)</li>
 * <li>Load on_load scripts (page based and site wide)</li>
 * <li>Load tpl_main_page.php (this is a common template file)</li>
 * <li>Load application_bottom.php</li>
 * </ul>
 *
 * @package general
 * @copyright Copyright 2003-2005 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: index.php 2942 2006-02-02 04:41:23Z drbyte $
 */
/**
 * Load common library stuff 
 */
  require('includes/application_top.php');

  $language_page_directory = DIR_WS_LANGUAGES . $_SESSION['language'] . '/';
  $directory_array = $template->get_template_part($code_page_directory, '/^header_php/');
  foreach ($directory_array as $value) { 
/**
 * We now load header code for a given page. 
 * Page code is stored in includes/modules/pages/PAGE_NAME/directory 
 * 'header_php.php' files in that directory are loaded now.
 */
    require($code_page_directory . '/' . $value);
  }




/**
 * We now load the html_header.php file. This file contains code that would appear within the HTML <head></head> code 
 * it is overridable on a template and page basis. 
 * In that a custom template can define its own common/html_header.php file 
 */
  require($template->get_template_dir('html_header.php',DIR_WS_TEMPLATE, $current_page_base,'common'). '/html_header.php');
/**
 * Define Template Variables picked up from includes/main_template_vars.php unless a file exists in the
 * includes/pages/{page_name}/directory to overide. Allowing different pages to have different overall
 * templates.
 */



  require($template->get_template_dir('main_template_vars.php',DIR_WS_TEMPLATE, $current_page_base,'common'). '/main_template_vars.php');
/**
 * Read the "on_load" scripts for the individual page, and from the site-wide template settings
 * NOTE: on_load_*.js files must contain just the raw code to be inserted in the <body> tag in the on_load="" parameter.
 * Looking in "/includes/modules/pages" for files named "on_load_*.js"
 */
  $directory_array = $template->get_template_part(DIR_WS_MODULES . 'pages/' . $current_page_base, '/^on_load_/', '.js');
  foreach ($directory_array as $value) { 
    $onload_file = DIR_WS_MODULES . 'pages/' . $current_page_base . '/' . $value;
    $read_contents='';
    $lines = @file($onload_file);
    foreach($lines as $line) {
      $read_contents .= $line;
    }
  $za_onload_array[] = $read_contents;
  }
/**
 * now read "includes/templates/TEMPLATE/jscript/on_load/on_load_*.js", which would be site-wide settings
 */
  $directory_array=array();
  $tpl_dir=$template->get_template_dir('.js', DIR_WS_TEMPLATE, 'jscript/on_load', 'jscript/on_load_');
  $directory_array = $template->get_template_part($tpl_dir ,'/^on_load_/', '.js');
  foreach ($directory_array as $value) { 
    $onload_file = $tpl_dir . '/' . $value;
    $read_contents='';
    $lines = @file($onload_file);
    foreach($lines as $line) {
      $read_contents .= $line;
    }
    $za_onload_array[] = $read_contents;
  }

  // set $zc_first_field for backwards compatibility with previous version usage of this var
  if (isset($zc_first_field) && $zc_first_field !='') $za_onload_array[] = $zc_first_field;

  $zv_onload = "";
  if (isset($za_onload_array) && count($za_onload_array)>0) $zv_onload=implode(';',$za_onload_array);

  //ensure we have just one ';' between each, and at the end
  $zv_onload = str_replace(';;',';',$zv_onload.';');

  // ensure that a blank list is truly blank and thus ignored.
  if (trim($zv_onload) == ';') $zv_onload='';
/**
 * Define the template that will govern the overall page layout, can be done on a page by page basis
 * or using a default template. The default template installed will be a standard 3 column layout. This
 * template also loads the page body code based on the variable $body_code.
 */
  require($template->get_template_dir('tpl_main_page.php',DIR_WS_TEMPLATE, $current_page_base,'common'). '/tpl_main_page.php');


?>
</html>
<?php




/**
 * Load general code run before page closes
 */
?>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>