开发者

PHP Age Verification Dropdown

I have a basic script which generates dropdowns for age verification. What I want to do with this is add options to each dropdown at the beginning. So for day the first option would be 'Day', month - "Month", year - "Year" each with a value of 0. Here's the code:

<?php
//define the year
$thisYear = date('Y');
$filtered = array_map("strip", $_POST);

function strip($val){
$val = strip_tags($val);
$val = htmlentities($val,ENT_QUOTES);
return $val;
}

function renderDropDown($name, $opts, $valueIsKey=true){
$out = array();
$out[] = '<select name="'.$name.'">';
foreach($opts as $key => $val){
    if($valueIsKey){
        $out[] = '<option value="'.$key.'">'.$val.'</option>';
    } else {
        $out[] = '<option value="'.$val.'">'.$val.'</option>';
    }
}
$out[] = '</select>';

return implode("\n", $out);
}

if($_POST['submit'] != 'submit' && !isset($_POST['submit'])){
//define text months
for($i=2; $i<=13; $i++){
    $calTime = mktime(0, 0, 0, $i, 0, $thisYear);
    $months[date('m', $calTime)] = date('M', $calTime);
}
$renderHTML = true;
} else {    
    //try to construct a valid date from post data
if(checkdate($filtered['months'], $filtered['days'], $filtered['years'])){
    //valid date..check if they are 18+
    $validAge = $thisYear - 18;
    if($filtered['year开发者_JAVA技巧s'] <= $validAge){
        //inside you go
        die('yes');
    } else {
        header('Location: http://www.google.com');
    }
} else {
    //invalid date.. try again mr hacker
}
}

if($renderHTML){
?>
<form name="ageVerifier" action="" method="post">
    Day: <?php print(renderDropDown('days', range(1,31), false)); ?>
    Month: <?php print(renderDropDown('months', $months)); ?>
    Year: <?php print(renderDropDown('years', range($thisYear, $thisYear-100), false)); ?>
    <input type="submit" name="submit" value="submit" />
</form>
<?php
}
?>

Any help on this would be appreciated.

Cheers, Chris


I am not sure if this is what you need.

function renderDropDown($name, $opts, $valueIsKey=true){
    $out = array();
    $out[] = '<select name="'.$name.'">';

    $out[] = '<option value="0">'.ucfirst($name).'</option>';

    foreach($opts as $key => $val){
        if($valueIsKey){
            $out[] = '<option value="'.$key.'">'.$val.'</option>';
        } else {
            $out[] = '<option value="'.$val.'">'.$val.'</option>';
        }
    }
    $out[] = '</select>';

    return implode("\n", $out);
}

This is simple, but you'll have the plural s at the end of each name. You may want to add an argument $title to the function, so it will become:

function renderDropDown($name, $opts, $title, $valueIsKey=true){

    [...]

    $out[] = '<option value="0">'.$title.'</option>';

    [...]

}

You can call that function with something like:

renderDropDown('days', range(1,31), "Day", false);

Edit: you may want to look at this.

Hope that helps.
—Alberto


You can do something like this - I added an extra argument to the renderDropDown function; you just pass in what you want the first value to say as the last argument.

As a note, you may need to do some extra input checking. Adding in default values like 'Day' mean that you now have potential invalid inputs.

function renderDropDown($name, $opts, $valueIsKey=true, $default=null){
  $out = array();
  $out[] = '<select name="'.$name.'">';

  if ($default)
    $out[] = "<option value=''>{$default}</option>";

  foreach($opts as $key => $val){
    if($valueIsKey){
        $out[] = '<option value="'.$key.'">'.$val.'</option>';
    } else {
        $out[] = '<option value="'.$val.'">'.$val.'</option>';
    }
  }
  $out[] = '</select>';

  return implode("\n", $out);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜