Jump to content

[SOLVED] Minimum Age verification


JayVee

Recommended Posts

I'm trying to put an alcohol related website together and want to include a minimum age verification script on the site. The code I use is not redirecting properly (I get a blank page with no source code). I've never set a cookie before so I'm not sure where the script is falling down.

Heres what appears before the head content on my page.

 

<?php

if(isset($_POST['checkage'])) {

  $day = ctype_digit($_POST['day']) ? $_POST['day'] : '';

  $month = ctype_digit($_POST['month']) ? $_POST['month'] : '';

  $year = ctype_digit($_POST['year']) ? $_POST['year'] : '';

  

  $birthstamp = mktime(0, 0, 0, $month, $day, $year);

  $diff = time() - $birthstamp;

  $age_years = floor($diff / 31556926);

  if($age_years >= 18) {

    setcookie('legal', 'yes', 0, '/', '.'.$_SERVER['SERVER_NAME']);

  # The above line sets a cookie called "legal" throughout the entire domain and its value is yes. The cookie will expire next year, if it is not manually deleted.

    $url = '/home.htm';

  } else {

    setcookie('legal', 'no', 0, '/', '.'.$_SERVER['SERVER_NAME']);

    # You're not old enough, come back next year!

    $url = '/minor.htm';

  }

  header('Location: $url');

}

?>

 

Any help would be appreciated.

 

Link to comment
Share on other sites

Cookies can be edited, use this instead

 

$_SESSION['Name'] = "value";

 

Ofcourse at the top of every page you need

 

session_start();

 

but then you can call on the session like any variable. Example:

 

<?php

$_SESSION['Age'] = 16;

if($_SESSION['Age'] < 18)
{
echo "You are not of age";
exit();
}

?>

 

Or even

 

<?php

session_start();

$_SESSION['Text'] = "Anything";
echo $_SESSION['Text'];
?>

 

The difference is that a user can edit the cookie, because it's stored with them, but they can't edit a session, because it's on the server, and sessions are easier.

Link to comment
Share on other sites

Thanks Garethp I can see how using sessions is easier on the 'follow on' pages but this code still gives me a blank page.

 

Here is the updated code.

 

<?php

session_start();

if(isset($_POST['checkage'])) {

  $day = ctype_digit($_POST['day']) ? $_POST['day'] : '';

  $month = ctype_digit($_POST['month']) ? $_POST['month'] : '';

  $year = ctype_digit($_POST['year']) ? $_POST['year'] : '';

  

  $birthstamp = mktime(0, 0, 0, $month, $day, $year);

  $diff = time() - $birthstamp;

  $age_years = floor($diff / 31556926);

 

  if($age_years >= 18) {

    $_SESSION['Age'] = "Yes";

  # The above line sets a cookie called "legal" throughout the entire domain and its value is yes. The cookie will expire next year, if it is not manually deleted.

    $url = '/home.htm';

  } else {

   $_SESSION['Age'] = "No";

    # You're not old enough, come back next year!

    $url = '/minor.htm';

  }

  header('Location: $url');

}

?>

 

Any ideas?

Link to comment
Share on other sites

Ok I have narrowed this down a bit. Is it possible that the "if (isset($_POST['checkage']))" statement has a syntax error. If I comment it out the page displays properly. Am I setting the Session variables properly from within an 'if' statement? Any help?

 

<?php

session_start();

 

if (isset($_POST['checkage'])) {

  $day = $_POST['day'] ? $_POST['day'] : '';

  $month = $_POST['month'] ? $_POST['month'] : '';

  $year = $_POST['year'] ? $_POST['year'] : '';

  $minage = $_POST['country'] ? $_POST['country'] : ''; 

  

  $birthstamp = mktime(0, 0, 0, $month, $day, $year);

  $diff = time() - $birthstamp;

  $age_years = floor($diff / 31556926);

 

  if($age_years >= $minage) {

    $_SESSION["Age"] = "Yes";

    $url = '/home.htm';

  } else {

   $_SESSION["Age"] = "No";

    $url = '/minor.htm';

  }

  header('Location: $url');

}

?>

Link to comment
Share on other sites

Can't see where I've got too many }. I want this code to run on submission of form and not as soon as the page is loaded.

 

Regarding the ? and the : They work like true and false in the statement. They set the values to null if the Post field does not exist. I have removed them but still have the same problem.

 

 

Link to comment
Share on other sites

Use this- Gives you the age.

 

function getAge($YYYYMMDD_In){
	  // Parse Birthday Input Into Local Variables
	  // Assumes Input In Form: YYYYMMDD
	  $yIn=substr($YYYYMMDD_In, 0, 4);
	  $mIn=substr($YYYYMMDD_In, 4, 2);
	  $dIn=substr($YYYYMMDD_In, 6, 2);

	  // Calculate Differences Between Birthday And Now
	  // By Subtracting Birthday From Current Date
	  $ddiff = date("d") - $dIn;
	  $mdiff = date("m") - $mIn;
	  $ydiff = date("Y") - $yIn;

	  // Check If Birthday Month Has Been Reached
	  if ($mdiff < 0) 
	  {
		// Birthday Month Not Reached
		// Subtract 1 Year From Age
		$ydiff--;
	  } elseif ($mdiff==0)
	  {
		// Birthday Month Currently
		// Check If BirthdayDay Passed
		if ($ddiff < 0)
		{
		  //Birthday Not Reached
		  // Subtract 1 Year From Age
		  $ydiff--;
		}
	  }
	  return $ydiff; 
}

Link to comment
Share on other sites

Thanks Garethp,

Ok I'm with you there. But if my code is included before my html and its the first time you load the page then the isset statement should be ignored (because no form data was sent). Then, on form submission page reloads and the code recognises that form data was sent and processes the script. Am I way off? My issue is that the if statement I have blocks the loading of the html on the page.

 

 

Link to comment
Share on other sites

I'll rewrite your code for you, see if it works, but yes, your spot on

 

Quite honestly, your code has me stumped. If this doesn't work, try commenting out all lines inside the { and } and see if it works, if so, un-comment each line one at a time to see which one breaks the page.

 

<?php
if (isset($_POST['checkage'])) 
{
$day = '';
$month = '';
$year = '';
$minage = '';

if(isset($_POST['day']))
	{
	$day = $_POST['day'];
	}

if(isset($_POST['month']))
	{
	$month = $_POST['month'];
	}

if(isset($_POST['year']))
	{
	$year = $_POST['year'];
	}

if(isset($_POST['country']))
	{
	$minage = $_POST['country'];
	} 

$birthstamp = mktime(0, 0, 0, $month, $day, $year);
$diff = time() - $birthstamp;
$age_years = floor($diff / 31556926);

if($age_years >= $minage) 
	{
	$url = '/home.htm';
	} 
else 
	{
	$_SESSION["Age"] = "No";
	$url = '/minor.htm';
	}

header('Location: $url');
}
?>

Link to comment
Share on other sites

Ok, in the age verification page, if they are under age add this

 

$_SESSION['Ofage'] = "No";

 

and then in a file called check.php

 

<?php
session_start();
if(isset($_SESSION['Ofage']))
{
if($_SESSION['Ofage'] == "No")
	{
	"You must be of age to view this site";
	exit();
	}
}
?>

 

and include "check.php" at the top of every page

Link to comment
Share on other sites

Almost there, I just about have this working.

 

It worked with sessions so I wanted to try and get it working with Cookies so that the user could decide to set up a 'remember me' option and set a cookie with a year long expiry date.

 

I have the cookies setting properly and the checking script on subsequent pages. I just can't get the $remember variable to add to the time() in the set cookie script.

Here's the code.

if (isset($_POST['checkage'])) 
{
$day = '';
$month = '';
$year = '';
$minage = '';
$remember = 'O';

if(isset($_POST['day']))
	{
	$day = $_POST['day'];
	}

if(isset($_POST['month']))
	{
	$month = $_POST['month'];
	}

if(isset($_POST['year']))
	{
	$year = $_POST['year'];
	}

if(isset($_POST['country']))
	{
	$minage = $_POST['country'];
	}

if(isset($_POST['remember']))
	{
	$remember = 31556926;
	}  

$birthstamp = mktime(0, 0, 0, $month, $day, $year);
$diff = time() - $birthstamp;
$age_years = floor($diff / 31556926);

if($age_years >= $minage) 
	{
	setcookie('legal', 'yes', time() + 31556926);
	$url = '/bntest/home.php';
	} 
else 
	{
	$_SESSION["legal"] = "No";
	setcookie('legal', 'no', time() + 31556926);
	$url = '/bntest/minor.php';
	}

header('Location: '.$url.'');
}

 

When I try to use

setcookie('legal', 'yes', time() + $remember)

The page fails.

 

 

Link to comment
Share on other sites

Ok I have it working now. Huge thanks to Garethp for your time. Here are all the scripts.

Home page php

<?php
/*if the cookie already exists from a previous remember me button click then skip this page*/			
if($_COOKIE['legal'] == "yes")
		{
		header("Location: home.php");	
		}			

if (isset($_POST['checkage'])) 
{
$day = '';
$month = '';
$year = '';
$minage = '';
$remember = 'O';

if(isset($_POST['day']))
	{
	$day = $_POST['day'];
	}

if(isset($_POST['month']))
	{
	$month = $_POST['month'];
	}

if(isset($_POST['year']))
	{
	$year = $_POST['year'];
	}

if(isset($_POST['country']))
	{
	$minage = $_POST['country'];
	}

$rembox=$_POST['remember'];



$birthstamp = mktime(0, 0, 0, $month, $day, $year);
$diff = time() - $birthstamp;
$age_years = floor($diff / 31556926);

if($age_years >= $minage) 
	{
	if($rembox=="yes")
		{
		setcookie('legal', 'yes', time() + 31556926);
		}
		else{
		setcookie('legal', 'yes', 0);

		}
		$url = '/bntest/home.php';
	} 
else 
	{
	setcookie('legal', 'no', 0);
	$url = '/bntest/minor.php';
	}

header('Location: '.$url.'');
}
/*echo "<p>".$_COOKIE["legal"]."</p>";*/	
?>

 

Home page form (I cut the years back to 1960) and the legal age for most countries is included.

<form id="form1" name="form1" method="post" action="">
                                     <select name="country" id="country">
                                      <option value="99" selected="selected">Choose Country</option>
                                      <option value="18">Ireland</option>
                                      <option value="99">Albania</option>
                                      <option value="18">Antigua</option>
                                      <option value="18">Argentina</option>
                                      <option value="99">Armenia</option>
                                      <option value="18">Australia</option>
                                      <option value="16">Austria</option>
                                      <option value="18">Azerbaijan</option>
                                      <option value="18">Bahamas</option>
                                      <option value="18">Barbados</option>
                                      <option value="18">Belarus</option>
                                      <option value="16">Belgium</option>
                                      <option value="18">Belize</option>
                                      <option value="18">Bermuda</option>
                                      <option value="18">Bolivia</option>
                                      <option value="18">Brazil</option>
                                      <option value="18">Bulgaria</option>
                                      <option value="19">Canada</option>
                                      <option value="18">China</option>
                                      <option value="18">Chile</option>
                                      <option value="18">Columbia</option>
                                      <option value="18">Costa Rica</option>
                                      <option value="18">Croatia</option>
                                      <option value="16">Cuba</option>
                                      <option value="17">Cyprus</option>
                                      <option value="18">Czech Republic</option>
                                      <option value="18">Denmark</option>
                                      <option value="18">Dominican Republic</option>
                                      <option value="18">Ecuador</option>
                                      <option value="21">Egypt</option>
                                      <option value="18">Estonia</option>
                                      <option value="18">Finland</option>
                                      <option value="18">Fiji</option>
                                      <option value="16">France</option>
                                      <option value="18">Georgia</option>
                                      <option value="16">Germany</option>
                                      <option value="17">Greece</option>
                                      <option value="18">Guam</option>
                                      <option value="16">Haiti</option>
                                      <option value="18">Hong Kong</option>
                                      <option value="18">Honduras</option>
                                      <option value="18">Hungary</option>
                                      <option value="20">Iceland</option>
                                      <option value="21">India</option>
                                      <option value="18">Israel</option>
                                      <option value="16">Italy</option>
                                      <option value="18">Jamaica</option>
                                      <option value="20">Japan</option>
                                      <option value="18">Kenya</option>
                                      <option value="18">Liechtenstein</option>
                                      <option value="17">Luxembourg</option>
                                      <option value="19">South Korea</option>
                                      <option value="18">Latvia</option>
                                      <option value="18">Lebanon</option>
                                      <option value="18">Lithuania</option>
                                      <option value="18">Macedonia</option>
                                      <option value="18">Malaysia</option>
                                      <option value="16">Malta</option>
                                      <option value="18">Mexico</option>
                                      <option value="18">Moldova</option>
                                      <option value="18">Morocco</option>
                                      <option value="18">Nepal</option>
                                      <option value="16">Netherlands</option>
                                      <option value="18">New Zealand</option>
                                      <option value="18">Nigeria</option>
                                      <option value="18">Norway</option>
                                      <option value="18">Paraguay</option>
                                      <option value="18">Peru</option>
                                      <option value="18">Philippines</option>
                                      <option value="18">Poland</option>
                                      <option value="16">Portugal</option>
                                      <option value="18">Puerto Rico</option>
                                      <option value="18">Romania</option>
                                      <option value="18">Russia</option>
                                      <option value="18">Serbia</option>
                                      <option value="18">Slovakia</option>
                                      <option value="18">Singapore</option>
                                      <option value="18">South Africa</option>
                                      <option value="18">Spain</option>
                                      <option value="18">Sri Lanka</option>
                                      <option value="18">Sweden</option>
                                      <option value="16">Switzerland</option>
                                      <option value="18">Taiwan</option>
                                      <option value="20">Thailand</option>
                                      <option value="18">Turkey</option>
                                      <option value="18">Uganda</option>
                                      <option value="18">Ukraine</option>
                                      <option value="21">U.A.E.</option>
                                      <option value="18">UK</option>
                                      <option value="21">USA</option>
                                      <option value="18">Venezuela</option>
                                      <option value="18">Other</option>
                                  </select>
                                     <select name="day" id="day">
                                      <option value="1">Day</option>
                                      <option value="1">1</option>
                                      <option value="2">2</option>
                                      <option value="3">3</option>
                                      <option value="4">4</option>
                                      <option value="5">5</option>
                                      <option value="6">6</option>
                                      <option value="7">7</option>
                                      <option value="8">8</option>
                                      <option value="9">9</option>
                                      <option value="10">10</option>
                                      <option value="11">11</option>
                                      <option value="12">12</option>
                                      <option value="13">13</option>
                                      <option value="14">14</option>
                                      <option value="15">15</option>
                                      <option value="16">16</option>
                                      <option value="17">17</option>
                                      <option value="18">18</option>
                                      <option value="19">19</option>
                                      <option value="20">20</option>
                                      <option value="21">21</option>
                                      <option value="22">22</option>
                                      <option value="23">23</option>
                                      <option value="24">24</option>
                                      <option value="25">25</option>
                                      <option value="26">26</option>
                                      <option value="27">27</option>
                                      <option value="28">28</option>
                                      <option value="29">29</option>
                                      <option value="30">30</option>
                                      <option value="31">31</option>
                                    </select>
                                    <select name="month" id="month">
                                      <option value="1" selected="selected">Month</option>
                                      <option value="1">January</option>
                                      <option value="2">February</option>
                                      <option value="3">March</option>
                                      <option value="4">April</option>
                                      <option value="5">May</option>
                                      <option value="6">June</option>
                                      <option value="7">July</option>
                                      <option value="8">August</option>
                                      <option value="9">September</option>
                                      <option value="10">October</option>
                                      <option value="11">November</option>
                                      <option value="12">December</option>
                                    </select>
                                    <select id="year" name="year">
                                      <option selected="selected" value="2008">Year</option>
                                      <option value="1998">1998</option>
                                     <option value="1997">1997</option>
                                      <option value="1996">1996</option>
                                      <option value="1995">1995</option>
                                    <option value="1994">1994</option>
                                     <option value="1993">1993</option>
                                      <option value="1992">1992</option>
                                       <option value="1991">1991</option>
                                      <option value="1990">1990</option>
                                      <option value="1989">1989</option>
                                    <option value="1988">1988</option>
                                      <option value="1987">1987</option>
                                      <option value="1986">1986</option>
                                    <option value="1985">1985</option>
                                      <option value="1984">1984</option>
                                    <option value="1983">1983</option>
                                     <option value="1982">1982</option>
                                      <option value="1981">1981</option>
                                      <option value="1980">1980</option>
                                     <option value="1979">1979</option>
                                      <option value="1978">1978</option>
                                      <option value="1977">1977</option>
                                     <option value="1976">1976</option>
                                      <option value="1975">1975</option>
                                      <option value="1974">1974</option>
                                      <option value="1973">1973</option>
                                      <option value="1972">1972</option>
                                       <option value="1971">1971</option>
                                      <option value="1970">1970</option>
                                      <option value="1969">1969</option>
                                      <option value="1968">1968</option>
                                      <option value="1967">1967</option>
                                      <option value="1966">1966</option>
                                      <option value="1965">1965</option>
                                      <option value="1964">1964</option>
                                       <option value="1963">1963</option>
                                      <option value="1962">1962</option>
                                      <option value="1961">1961</option>
                                      <option value="1960">1960</option>
                                    </select>
                                    <input name="remember" type="checkbox" id="remember" value="yes" />
                                    <input type="image" src="images/entersite.png" height="25" width="191" border="0" alt="Submit Form" name="checkage" value="submit"/>
                    </form>

 

and here is the include.php code to check on all pages within site

<?php 	
if(!isset($_COOKIE['legal'])) {  	
header("Location: index.php");	 
}

if($_COOKIE['legal'] == "no")
		{
		header("Location: index.php");	
		}			
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.