Help - Search - Members - Calendar
Full Version: Php Age Gate?
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
EMOruffino
How would i go about getting or making an age gate for my site? the new episodes coming out for bnstv require it, and i cant find anywhere on the web on how to make one...

ideas?

example:
http://www.brothersinarmsgame.com/us/age_g...stURL=index.php
Jack
You mean like "Enter age to continue:"? Those are done by cookies
EMOruffino
QUOTE(Jack @ Jan 6 2008, 01:28 PM) *
You mean like "Enter age to continue:"? Those are done by cookies

exactly....

ill do some digging around and post back

edit: if you could help, that would rock...
Brandon
QUOTE(EMOruffino @ Jan 6 2008, 07:35 PM) *
exactly....

ill do some digging around and post back

Hi EMOruffino I have found some code you could try editing ?

CODE
<script language="javascript" type="text/javascript">
<!-- "hide" code in case someone is liveing in the stone age...
function validate() {
//Shorthand vars
var year = parseInt(document.forms["verification"]["year"].value);
var month = parseInt(document.forms["verification"]["month"].value) - 1;
var day = parseInt(document.forms["verification"]["day"].value);
//Date stuff
var date1 = new Date((year + 18), month, day); //Change 18 to the min age
var now = new Date;
//Are they old enough?
if ( (today.getTime() - theirDate.getTime()) < 0) {
//*boot* too younge, kiddo!
document.location="http://www.google.com";
} else {
//They are old enough, but is the email there? (not advanced checking)
if ( document.forms["verification"]["email"] == "" ) {
//No email!
alert("You forgot to enter your email.");
} else {
//All seems OK...
document.location="http://www.xxx.xxx/xxx.xxx"; //move to the welcome page
}
}
}
-->
</script>
<p>Please enter your age and email:</p>
<form name="verification" method="post" onsubmit="validate()" action="">
<p>Month:
<select name="month">
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select><br />
Day:
<select name="day">
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
<option>31</option>
</select><br />
Year:
<input type="text" name="year" size="4" maxlength="4" /></p>
<input type="checkbox" name="remember" value="1" checked />
<small>Don't ask me again</small><br />
<input type="text" name="email" size="35" maxlength="35" /><br />
<input type="submit" name="submit" value="Verify" />
</form>

Sorry if this is not what your looking for.

EDIT: I have just found this link if any use http://www.avengex.com/tutorials/209/Age_Calculation/
EMOruffino
QUOTE(Crown @ Jan 6 2008, 01:45 PM) *
Sorry if this is not what your looking for.

EDIT: I have just found this link if any use http://www.avengex.com/tutorials/209/Age_Calculation/

ima try that out.... that link you gave dosnt work for me...

i also found this:
http://www.kuranes.co.uk/utils/agecheck.html

but what i need is something like that, but when a user tries to access a page like: /episodes.php it redirects thme to index.php unless they have a cookie... so, how would i do this?
Brandon
QUOTE(EMOruffino @ Jan 6 2008, 07:54 PM) *
ima try that out.... that link you gave dosnt work for me...

i also found this:
http://www.kuranes.co.uk/utils/agecheck.html

but what i need is something like that, but when a user tries to access a page like: /episodes.php it redirects thme to index.php unless they have a cookie... so, how would i do this?

Could you try something with hmm Sessions?

Hold on mate ill copy that site to PDF
EMOruffino
i got the script on that site, (the link i posted) but sessions..... hmmm

CODE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Please enter your age</title>
<script language="javascript">
        function checkAge()
        {
            /* the minumum age you want to allow in */
            var min_age = 18;

            /* change "age_form" to whatever your form has for a name="..." */
            var year = parseInt(document.forms["age_form"]["year"].value);
            var month = parseInt(document.forms["age_form"]["month"].value) - 1;
            var day = parseInt(document.forms["age_form"]["day"].value);

            var theirDate = new Date((year + min_age), month, day);
            var today = new Date;

            if ( (today.getTime() - theirDate.getTime()) < 0) {
                alert("You are too young to enter this site!");
                return false;
            }
            else {
                return true;
            }
        }
    </script>
</head>

<body>
<div align="center">
<form action="http://localhost/test/test2.htm" method="get" name="age_form">
Day : <select name="day">
    <option>1</option>
    <option>2</option>

    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>

    <option>9</option>
    <option>10</option>
    <option>11</option>
    <option>12</option>
    <option>13</option>
    <option>14</option>

    <option>15</option>
    <option>16</option>
    <option>17</option>
    <option>18</option>
    <option>19</option>
    <option>20</option>

    <option>21</option>
    <option>22</option>
    <option>23</option>
    <option>24</option>
    <option>25</option>
    <option>26</option>

    <option>27</option>
    <option>28</option>
    <option>29</option>
    <option>30</option>
    <option>31</option>
</select>

Month : <select name="month">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>

    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
    <option>11</option>

    <option>12</option>
</select>

Year : <select name="year">
    <option>2003</option>
    <option>2002</option>
    <option>2001</option>
    <option>2000</option>

    <option>1999</option>
    <option>1998</option>
    <option>1997</option>
    <option>1996</option>
    <option>1995</option>
    <option>1994</option>

    <option>1993</option>
    <option>1992</option>
    <option>1991</option>
    <option>1990</option>
    <option>1989</option>
    <option>1988</option>

    <option>1987</option>
    <option>1986</option>
    <option>1985</option>
    <option>1984</option>
    <option>1983</option>
    <option>1982</option>

    <option>1981</option>
    <option>1980</option>
    <option>1979</option>
    <option>1978</option>
    <option>1977</option>
    <option>1976</option>

    <option>1975</option>
    <option>1974</option>
    <option>1973</option>
    <option>1972</option>
    <option>1971</option>
    <option>1970</option>

    <option>1969</option>
    <option>1968</option>
    <option>1967</option>
    <option>1966</option>
    <option>1965</option>
    <option>1964</option>

    <option>1963</option>
    <option>1962</option>
    <option>1961</option>
    <option>1960</option>
    <option>1959</option>
    <option>1958</option>

    <option>1957</option>
    <option>1956</option>
    <option>1955</option>
    <option>1954</option>
    <option>1953</option>
    <option>1952</option>

    <option>1951</option>
    <option>1950</option>
    <option>1949</option>
    <option>1948</option>
    <option>1947</option>
    <option>1946</option>

    <option>1945</option>
    <option>1944</option>
    <option>1943</option>
    <option>1942</option>
    <option>1941</option>
    <option>1940</option>

    <option>1939</option>
    <option>1938</option>
    <option>1937</option>
    <option>1936</option>
    <option>1935</option>
    <option>1934</option>

    <option>1933</option>
    <option>1932</option>
    <option>1931</option>
    <option>1930</option>
    <option>1929</option>
    <option>1928</option>

    <option>1927</option>
    <option>1926</option>
    <option>1925</option>
    <option>1924</option>
    <option>1923</option>
    <option>1922</option>

    <option>1921</option>
    <option>1920</option>
    <option>1919</option>
    <option>1918</option>
    <option>1917</option>
    <option>1916</option>

    <option>1915</option>
    <option>1914</option>
    <option>1913</option>
    <option>1912</option>
    <option>1911</option>
    <option>1910</option>

    <option>1909</option>
    <option>1908</option>
    <option>1907</option>
    <option>1906</option>
    <option>1905</option>
    <option>1904</option>

    <option>1903</option>
</select>

<input type="submit" name="_send_date_" value="Go" onClick="return checkAge()">
</form>
</div>
</body>
Brandon
QUOTE(EMOruffino @ Jan 6 2008, 08:07 PM) *
i got the script on that site, (the link i posted) but sessions..... hmmm

I meant for the site i posted? .. With the sessions you could like have a database or something i think ?

EDIT:PM'ed You mate
EMOruffino
alright so this is my code:
CODE
<?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', time() + 31556926, '/', '.'.$_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.html';
  } else {
    setcookie('legal', 'no', time() + 31556926, '/', '.'.$_SERVER['SERVER_NAME']);
    # You're not old enough, come back next year!
    $url = '/minor.html';
  }
  header('Location: '.$url.');
}
?>


but it gives me this error:

CODE
Warning: Unexpected character in input: ''' (ASCII=39) state=1 in C:\Users\emoruffino\Desktop\xampplite\htdocs\test\index.php on line 19

Parse error: syntax error, unexpected ')' in C:\Users\emoruffino\Desktop\xampplite\htdocs\test\index.php on line 19


Brandon
QUOTE(EMOruffino @ Jan 6 2008, 08:39 PM) *
alright so this is my code:
CODE
<?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', time() + 31556926, '/', '.'.$_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.html';
  } else {
    setcookie('legal', 'no', time() + 31556926, '/', '.'.$_SERVER['SERVER_NAME']);
    # You're not old enough, come back next year!
    $url = '/minor.html';
  }
  header('Location: '.$url.');
}
?>


but it gives me this error:

CODE
Warning: Unexpected character in input: ''' (ASCII=39) state=1 in C:\Users\emoruffino\Desktop\xampplite\htdocs\test\index.php on line 19

Parse error: syntax error, unexpected ')' in C:\Users\emoruffino\Desktop\xampplite\htdocs\test\index.php on line 19


You see line 19 you have
CODE
header('Location: '.$url.');

change it to
CODE
header('Location: .$url.');


Let me know if it works smile.gif
Brandon
QUOTE(Tom @ Jan 6 2008, 09:07 PM) *
That won't work, Crown. Variables aren't parsed in single quotes.

Change

CODE
header('Location: '.$url.')


to

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


Dan Said it should be

CODE
header("Location: $url");
EMOruffino
that did the trick.... lets see if i can make the rest work....
edit:


http://bnstv.com/test
EMOruffino
okay so this is what it is so far.... i can visit:

http://bnstv.com/test/test2.php without first entering in the age.... and when visiting the index it makes me do the age thing every reload, no cookies?
Brandon
QUOTE(EMOruffino @ Jan 6 2008, 09:29 PM) *
that did the trick.... lets see if i can make the rest work....
edit:
http://bnstv.com/test

Good smile.gif just if you press enter when you dont select your bday yeh you get this
CODE
Warning: mktime() expects parameter 4 to be long, string given in /home/bnstv3/public_html/test/index.php on line 7

Warning: Cannot modify header information - headers already sent by (output started at /home/bnstv3/public_html/test/index.php:7) in /home/bnstv3/public_html/test/index.php on line 11

Warning: Cannot modify header information - headers already sent by (output started at /home/bnstv3/public_html/test/index.php:7) in /home/bnstv3/public_html/test/index.php on line 19


EMOruffino
the thing is tho, theres no bom sig, so i dont know what else could be causing the headers already sent
Brandon
QUOTE(EMOruffino @ Jan 6 2008, 09:48 PM) *
the thing is tho, theres no bom sig, so i dont know what else could be causing the headers already sent

Very True Hmmm hard one i suppose although I'm no expert lol
_dexi
You'll have to put all the php code above the html (before the <html> tag), at the very least anything involving setcookie(); All headers (which includes cookies and location redirects) have to be sent before any html
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.