Jump to content

Recommended Posts

I am trying to do age validation for a beverage site, and losing my mind.  First time trying local storage (to persist across browser sessions) and I assume I have something screwed up.  Anyone able to help me figure out how to make this work?

<?php
    if( isset( $_POST['yes'] ) )
    {
    	localStorage.setItem('age_verification', 'true');

        if( isset( $_GET['url'] ) )
        {
            die( header('Location: ' . $_GET['url'] ) ); 
        } 
        else
        {
            die( header('Location: index.php') );
        }
    }
    elseif( isset( $_POST['no'] ) )
    {
    	localStorage.setItem('age_verification', 'false');
    }
	var age_verification = localStorage.getItem('age_verification'); //new
	if (age_verification = "false" || age_verification = "null") die( header('Location: http://www.bing.com') );	
?>

Above is the code for the file checkagenew.php which I call from other pages using the below:

<?php
	if (localStorage.age_verification) { localStorage.setItem('age_verification', 'null'); }
	var age_verification = localStorage.getItem('age_verification');
	if (age_verification = "false" || age_verification = "null") die( header("Location: checkagenew.php?url=http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]") );
?>  

Obviously there is a form included in the top part (not shown) that has a submit for yes/no which triggers the $_POST validation... I did not include that for brevity.

Link to comment
https://forums.phpfreaks.com/topic/309112-help-wage-validation-local-storage/
Share on other sites

P.S.

I would recommend a checkbox which the user has to click (check) to verify age. Local storage would have to be set in the javascript.

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-language" content="en">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Example</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/javascript">
        
        function ageVerified(btn)
        {
            if ($(btn).is(":checked") && $(btn).val() == 'Yes' ) {
                localStorage.setItem('age_verification', 'Yes')
            }
            else {
                localStorage.setItem('age_verification', 'No')
            }
        }
        
    </script>
</head>
<body>

    <form method="post">
        I am over 18 &emsp; 
            <input type="checkbox" name="ageverify" onclick="ageVerified(this)" value="Yes">
            <br>
        <input type="submit" name="btnSub" value="Submit">
    </form>

</body>
</html>

Alternatively, use a pair of radio buttons. Either way it's a single input...

    <form method="post">
        I am over 18 &emsp; 
            <input type="radio" name="ageverify" onclick="ageVerified(this)" value="No" checked> No &emsp; 
            <input type="radio" name="ageverify" onclick="ageVerified(this)" value="Yes"> Yes
        <br>
        <input type="submit" name="btnSub" value="Submit">
    </form>

Whichever above method you use, the PHP processing would be the same ...

<?php

if ( ($_POST['ageverify'] ?? 'No') == 'Yes')  {
    echo "User is over 18<hr>";
}
else {
    echo "Under age user<hr>" ;
}

?>

 

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.