Jump to content

Age Verification Script


rstewar

Recommended Posts

I am trying to create a age verification page for a website. I am having an issue getting the page to redirect if someone is not of age.

 

verify.php

<form method="POST" action="/ageCheck.php">
                    <input id="enterbutton" type="submit" name="v" value="I'm 21" />
                    <input id="enterbutton" type="submit" name="v" value="Nope. I sit at the kid's table" />
            </form>

 

ageCheck.php

<?
// start session
session_start();

$_SESSION['v'] = $_POST['v'];

// Check if verified
if($_SESSION['v'] == "I\'m 21") {
    $_SESSION['check'] = 1;
    header('Location: index.php');

}else {
    $_SESSION['check'] = 0;
    header('Location: http://google.com');

}

?>

 

Everything seems to work fine up to this point. But when I post the following code in each pages header it does not redirect.

<?
session_start();

if($_SESSION['check'] != 1) {
    header('Location: verify.php');
    print 'not verified'; // print for debugging

}
?>

 

The odd thing to me is that it does print "not verified", but it will not redirect. Thanks in advance for any help!

 

Link to comment
Share on other sites

The code you've posted is okay, but it could be improved upon. Let's start with your HTML, notice that you have the id element in each input tag the same, this is not valid. Also, you've named them both the same, this is how I would do it:

 

<form method="POST" action="/ageCheck.php">
                    <input type="submit" name="valid_age" value="I'm 21" />
                    <input type="submit" name="invalid_age" value="Nope. I sit at the kid's table" />
</form>

 

Unless you are using Javascript and need to access any of the inputs attributes, the id tag is not really required. Now, you're ageCheck.php page I would make look something like this:

 

<?php
//Start session
session_start();

/*
* First, we want to make sure they came to this ageCheck.php via a form.
* Then we can check to see if $_POST['valid_age'] is set, since it will only
* be set if they pressed the "I'm 21" button.
*/
if(isset($_POST)){
if(isset($_POST['valid_age'])){
	/*
	 * Since they got here, it means they are of the right age.
	 * Now we set the session value.
	 */
	$_SESSION['age_verified'] = true;
	header("Location: index.php");
	exit;
}else{
	/*
	 * To young! Just re-direct them to Google.
	 */
	header("Location: http://www.google.com");
	exit;
}
}else{
die("Trying to sneak in are we?");
}

?>

 

Then the header code for each of your pages where you have to have passed the age verification, you should have it like this:

 

<?php
//Start the session
session_start();

/*
* If they haven't passed the age test
* then the age_verified session will not
* be set, since it is only set if they
* say they are 21 years of age.
*/
if(!isset($_SESSION['age_verified'])){
header("Location: verify.php");
exit;
}
?>

 

That's how I would do it anyway.

 

Hope that gives you a bit of help, I commented throughout the changes that I made.

 

Regards,

ProjectFear

Link to comment
Share on other sites

  • 4 months later...
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.