0

我正在尝试从启动(年龄验证)页面重定向到我的主页,它只是不断弹出相同的年龄验证页面。

我在根文件夹中有ageVerify.php 脚本,在我的模板文件页面顶部有另一个脚本。在有人点击“是的,我 18 岁”后,我只需要找到正确的文件结构格式即可重定向

下面的代码在添加到我的 column1.phtml 文件顶部时不起作用 - 它只是不断返回并调用 verify.php 脚本。任何想法都会非常有帮助!

<?php

function verified()
{
    $redirect_url='http://www.johnsoncreeksmokejuice.com.vhost.zerolag.com/verify.php';
    $expires=-1;
    session_start();

    $validated=false; 

    if (!empty($_COOKIE["verified"])) {
        $validated=true;
    }
    if (!$validated && isset($_SESSION['verified'])) {
        $validated=true;
    }
    if (is_numeric($expires) && $expires==-1 && !isset($_SESSION['verified'])) {
        $validated=false;
    }
    if ($validated) {
        return;
    }
    else {
        $redirect_url=$redirect_url."?return=index.php&x=".$expires;
        Header('Location: '.$redirect_url);
        exit(0);
    }
}

verified();
?>
4

2 回答 2

1

如果 $_SESSION 没有设置总是会评估这个

  if (is_numeric($expires) && $expires==-1 && !isset($_SESSION['verified'])) {
        $validated=false;
    }

只需修复它,应该可以工作。假设其他一切都很好,我会更换

if (!empty($_COOKIE["verified"])) {
        $validated=true;
    }
    if (!$validated && isset($_SESSION['verified'])) {
        $validated=true;
    }
    if (is_numeric($expires) && $expires==-1 && !isset($_SESSION['verified'])) {
        $validated=false;
    }

经过:

if ( (isset($_COOKIE["verified"] && !empty($_COOKIE["verified"])) OR isset($_SESSION['verified']) ) {
    $validated=true;
}

因此,如果用户有一个非空的“已验证”cookie 或“已验证”会话集,则假定他已通过验证。

于 2012-10-01T19:26:03.160 回答
0

选择使用 javascript 替代方案。对我来说更容易解决:

function writeCookie(key,value,days) {
    if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = key+"="+value+expires+"; path=/";
}

function readCookie(key) {
    var nameEQ = key + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}


function ageGate() {
    var monthDays = {
        1: 31,
        2: 29,
        3: 31,
        4: 30,
        5: 31,
        6: 30,
        7: 31,
        8: 31,
        9: 30,
        10: 31,
        11: 30,
        12: 31
    };

    var months = {
        1: 'January',
        2: 'February',
        3: 'March',
        4: 'April',
        5: 'May',
        6: 'June',
        7: 'July',
        8: 'August',
        9: 'September',
        10: 'October',
        11: 'November',
        12: 'December'
    };

    var monthOptions = [];
    var dayOptions = {};
    var yearOptions = [];

    for (var month in monthDays) {
        var days = monthDays[month];
        monthOptions.push('<option value="' + month + '">' + months[month] + '</option>');
        dayOptions[month] = [];
        for (var i=1; i <= days; i++) {
            var day = i;
            dayOptions[month].push('<option value="' + day + '">' + day + '</option>');
        }
    }

    var currentYear = new Date();
    currentYear = currentYear.getFullYear();

    var startYear = currentYear - 120;

    for (var y=currentYear; y > startYear; y--) {
        yearOptions.push('<option value="' + y + '">' + y + '</option>');
    }



    $s(document).ready(function(){
        var monthHtml = '';
        for (var j=0; j < monthOptions.length; j++) {
            var html = monthOptions[j];
            monthHtml += html;
        }

        $s('#ageMonth').html(monthHtml);

        var yearHtml = '';
        for (var i=0; i < yearOptions.length; i++) {
            yearHtml += yearOptions[i];
        }

        $s('#ageYear').html(yearHtml);

        $s('#ageMonth').bind('change', function(){
            var dayHtml = '';
            var month = $s(this).val();

            for (var i=0; i < dayOptions[month].length; i++) {
                dayHtml += dayOptions[month][i];
            }

            $s('#ageDay').html(dayHtml);
        });

        $s('#ageEnterSite').click(function(e){
            e.preventDefault();
            var date = new Date();
            date.setMonth($s('#ageMonth').val() - 1);
            date.setDate($s('#ageDay').val());
            date.setYear($s('#ageYear').val());

            var maxDate = new Date();
            // alert(maxDate.getFullYear());

            maxDate.setYear(maxDate.getFullYear() - 18);

            if (date <= maxDate) {
                writeCookie('jcsj_age_verified', '1', 30);
                $s('#age-gate').fadeOut(function(){
                    $s(this).remove();
                    $s('body').removeClass('age-gate-visible');
                });
            }
            else {
                window.location.href = 'http://google.com';
            }

        });

        $s('#ageMonth').change(); // load default month
        // $s('#ageDay').prop('disabled', true);
        setTimeout(function(){
            $s('body').addClass('age-gate-visible');
            $s('#age-gate').fadeIn();
        }, 200);
    });
}

if (readCookie('jcsj_age_verified')) {

} else {
    ageGate();
}

</script>
于 2012-11-21T15:20:08.150 回答