What am i doing wrong with my first PHP code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
haykodya
Forum Newbie
Posts: 2
Joined: Sat May 22, 2010 1:47 am

What am i doing wrong with my first PHP code

Post by haykodya »

i am building a website for an alcohol related company, and i need age conformation code, which asks for users birth date, then checks if that person is over 21 years old, and decides to let him/her in or no.

the form i have in the first page of the website is

Code: Select all

<form action="c1.php", method="POST">
            <input name="day" type="text" value="DD" size="10" maxlength="2">&nbsp;&nbsp;
            <input name="month" type="text" value="MM" size="10" maxlength="2">&nbsp;&nbsp;
            <input name="year" type="text" value="YYYY" size="10" maxlength="4">&nbsp;&nbsp;
            <input type="submit" value="GO!">
          </form>
the c1.php file looks like

Code: Select all

<html>

<head>
  <title></title>
</head>

<body>

<?php
$y=date('Y');
$m=date("m");
$d=date("d");
$year=$_POST["year"];
$month=$_POST["month"];
$day=$_POST["day"];
$year2=$y-$year;

	if($year2>21)
    {
        echo '<META HTTP-EQUIV="Refresh" Content="0; URL=indexyes.html">';
    }
    if($year2<21)
    {
        echo '<META HTTP-EQUIV="Refresh" Content="0; URL=indexno.html">';
    }
    if($year2=21)
   	 {
     	   if($month>$m)
        	{
         	 echo '<META HTTP-EQUIV="Refresh" Content="0; URL=indexyes.html">';
       	 	}

       	   if($month<$m)
      	    {
             echo '<META HTTP-EQUIV="Refresh" Content="0; URL=indexno.html">';
      	    }
           if($month=$m)
       	      {
       	       if($day>$d)
       	      		{
       	     			 echo '<META HTTP-EQUIV="Refresh" Content="0; URL=indexyes.html">';
      	   		    }

     	       if($day<$d)
     	            {
      	                echo '<META HTTP-EQUIV="Refresh" Content="0; URL=indexno.html">';
      	            }
               if($day=$d)
      	            {
                        echo '<META HTTP-EQUIV="Refresh" Content="0; URL=indexyes.html">';
    	            }
      	     }

     }

?>

</body>

</html>
What am i doing wrong here, when i use it, and input 2006 as year, it still takes the customer to the website
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: What am i doing wrong with my first PHP code

Post by Christopher »

You are checking against the age 21 instead of the year 21 years ago. You might want to look at strtotime('-21 year').
(#10850)
haykodya
Forum Newbie
Posts: 2
Joined: Sat May 22, 2010 1:47 am

Re: What am i doing wrong with my first PHP code

Post by haykodya »

tried that but no luck,

i tried to redo everything, but same problem happens again, by the way when i take out the part that checks the month and the day, it works,

does anybody know if

Code: Select all

$m=date("m");
will consider june as "6" instead of "June"?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: What am i doing wrong with my first PHP code

Post by AbraCadaver »

You're making it too complicated.

Code: Select all

if(strtotime("$month/$day/$year") <= strtotime('-21 years')) {
   // 21 or older
   // echo '<META HTTP-EQUIV="Refresh" Content="0; URL=indexyes.html">';
   header('Location: http://www.example.com/indexyes.html');
   exit;
} else {
   // under 21
   // echo '<META HTTP-EQUIV="Refresh" Content="0; URL=indexno.html">';
   header('Location: http://www.example.com/indexno.html');
   exit;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
lcarron000
Forum Newbie
Posts: 13
Joined: Thu May 20, 2010 2:51 pm

Re: What am i doing wrong with my first PHP code

Post by lcarron000 »

I added

Code: Select all

date_default_timezone_set()
and changed

Code: Select all

  if($var = $var) to if($var == $var)
Code:

Code: Select all

<html>

<head>
  <title></title>
</head>

<body>

<?php
date_default_timezone_set('America/Los_Angeles'); //Set (YOUR) timezone
$y = date('Y');
$m = date('m');
$d = date('d');
$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];
$year2 = ($y - $year);

if ($year2 > 21) {
    echo '<META HTTP-EQUIV="Refresh" Content="0; URL=indexyes.html">';
}
if ($year2 < 21) {
    echo '<META HTTP-EQUIV="Refresh" Content="0; URL=indexno.html">';
}
if ($year2 == 21) { //Change = to ==
    if ($month > $m) {
        echo '<META HTTP-EQUIV="Refresh" Content="0; URL=indexyes.html">';
    }

    if ($month < $m) {
        echo '<META HTTP-EQUIV="Refresh" Content="0; URL=indexno.html">';
    }
    if ($month == $m) { //Change = to ==
        if ($day > $d) {
            echo '<META HTTP-EQUIV="Refresh" Content="0; URL=indexyes.html">';
        }

        if ($day < $d) {
            echo '<META HTTP-EQUIV="Refresh" Content="0; URL=indexno.html">';
        }
        if ($day == $d) { //Change = to ==
            echo '<META HTTP-EQUIV="Refresh" Content="0; URL=indexyes.html">';
        }
    }

}
?>

</body>

</html>
Post Reply