Age Validation redirect

software development

#1

I need to create a script/form that will redirect visitor to an age appropriate page. Under 18 to one page, Over 18 to another. A script or form that I could copy and paste would be ideal. Be kind. not too experienced. Thanks


#2

Form:

[code]
Please enter your age:

[/code][b]action.php[/b] will redirect to the preferred page ([b]under18.html[/b] or [b]18plus.html[/b]): [code]<?php $age = $_POST['age']; if($age < 18) { header("Location: under18.html"); exit; } else { header("Location: 18plus.html"); exit; } ?>[/code]--------------- Simon Jessey [url=http://keystonewebsites.com/][b]Keystone Websites[/b][/url] | [url=http://jessey.net/][b]si-blog[/b][/url]

#3

I set up “enter.html” that contained the form

Please enter your age:

I set up a php file:

<?php $age = $_POST['age']; if($age < 18) { header("Location: under18.html"); exit; } else { header("Location: 18plus.html"); exit; } ?>

I set up 2 pages “under18.html” and “18plus.html”

The form looks good, but when I enter the age, I get directed to what looks like the source of the php file.

Could you troubleshoot. Also, how difficult would it be to have the visitor enter their birthday instead of age.

I assume you can base it on year. <1987

Thanks for your help


#4

[quote]The form looks good, but when I enter the age, I get directed to what looks like the source of the php file.

Could you troubleshoot. Also, how difficult would it be to have the visitor enter their birthday instead of age.[/quote]
Please provide a URL for me to see the error, because I cannot see anything wrong with what you have done. Validating with a birthday just means having a more substantial bit of code in the IF statement.


Simon Jessey
Keystone Websites | si-blog


#5

I uploaded so I could share it with you and it works! Thank you. Now would you be so kind as to help me set it up to use the visitors birthday. month, day, year.

Thank you.

If this form is the index page, does it create issues with search engines?


#6

I was able to modify it myself. Thanks for all your help. I am still working on it, but if you have any suggestions, I’d appreciate it.

Will it effect search engines?

http://proof.twogeeksgraphics.com/npm/


#7

You can’t have been viewing the file through your local machine’s web server (assuming it has one). That’s why it works when it is uploaded.

It’s not a very user-friendly way of doing it, but assuming you want to go ahead with it you need to create a separate text box for the month, day, and year on the form. Then you’ll need to make a significant change to action.php:

<?php $month = $_POST['month']; $day = $_POST['day']; $year = $_POST['year']; // create timestamp for user's age $user_timestamp = mktime(12,0,0,$month,$day,$year); // work out difference between now and the user's timestamp $age = $time() - $user_timestamp; // convert $age timestamp to years (1yr = 31556926secs) $age = $age / 31556926; if($age < 18) { header("Location: under18.html"); exit; } else { header("Location: 18plus.html"); exit; } ?>The script is accurate to within 24 hours, because I’ve had to assume that their time of birth is noon. I’ve added comment lines (preceded by the //) to tell you what is happening, but they can be removed if you prefer.


Simon Jessey
Keystone Websites | si-blog


#8

Still need a little tweaking. I need the “year” field to be required. How do I make that happen. Right now, if the visitor doesn’t modify the form and hits enter, it directs them to the plus18 page - can’t have that happening.

thanks

site it temporarily on http://proof.twogeeksgraphics.com/npm


#9

Add this at the very beginning, just after the opening “<?php” tag:

if(!$_POST['year']) { header ("Location: form.html"); }If the year has not been submitted, it sends it back to your form. Don’t forget to make sure “form.html” is substituted with the actual filename of the form.


Simon Jessey
Keystone Websites | si-blog


#10

Thanks for all your help. I must have done something wrong.

I get the following error message. What did I do wrong?

Parse error: parse error, unexpected $ in /home/.panther/twogeeks/proof.twogeeksgraphics.com/npm/action1.php on line 12

here is the php

<?php if(!$_POST['year']) { header ("Location: index.html"); $age = $_POST['year']; if($year < 1987) { header("Location: 18plus.html"); exit; } else { header("Location: workshop.html"); exit; } ?>

#11

Look at the code I gave you. You are missing a closing curly brace “}”.


Simon Jessey
Keystone Websites | si-blog


#12

Still not working. Sorry I am not grasping this. I do appreciate your help though. Could you look one more time and help me solve this.

Thanks

http://proof.twogeeksgraphics.com/npm/


#13

I just went to the page you specified, and it seems to be working just fine. What’s the problem?


Simon Jessey
Keystone Websites | si-blog


#14

if the form is left blank and the visitor hits enter, they are directed to the “grownup” page


#15

Aaaahhh. I see. Try changing this:if(!$_POST['year']) { header ("Location: index.html"); }To this:if($_POST['year'] == "")) { header ("Location: index.html"); }---------------
Simon Jessey
Keystone Websites | si-blog


#16

still not working.
What if we changed it to report a message if the year field was left blank. Like, “please go back and enter the year”


#17

In order to do that, you would need to setup a session to store the values in session variables. No, the problem is one of logic. You need to set it up so that if the year is not entered, it goes back to the form. If the year is acceptable, it goes to the adult page. In all other cases, it goes to the kids page. Try this:

[code]<?php

// if year was submitted, redirect to form
if($_POST[‘year’] == “”) {
header(“Location: index.html”);
exit;
}

// calculate the year that was 18 years ago
$year = date(“Y”) - 18;

// if year indicates age 18 or older, redirect to adult page
if($_POST[‘year’] <= $year ) {
header (“Location: 18plus.html”);
exit;
}

// in all other case, redirect to kids page
header(“Location: workshop.html”);
exit;

?>[/code]I cannot think of any way to make it simpler than that. Since you are only checking the year, why bother making them fill in the day and month?


Simon Jessey
Keystone Websites | si-blog