开发者

jquery check ip before doing rest of stuff

Hey all, I have a jquery script that's a pretty fun game, you can view it here link text.

now after the play button is clicked, I send the users ip via ajax to see if it matches an ip stored with my site. if it does then the rest of the action is performed. I'm having trouble getting the 1st ajax to work. here's the script.

var hitCount = 0,
missCount = 0;

function IsNumeric(n) {
return !isNaN(n);
}

$("#getit").click(function() {
var hitCount = 0,
missCount = 0;
$('#h开发者_如何学编程itcount').text(0);
$('#misscount').text(0);

/* ajax to check ip goes here if successful they below is performed*/  

$('#message').hide(100);        
var li = [],
    intervals = 0,
    n = parseInt($('#MyNumber').val());

var intervalId = -1;
if (IsNumeric(n)) {
    intervalId = setInterval(function() {
        li[intervals++ % li.length].text(Math.random() > .1 ? Math.floor(Math.random()     * (10 + n) + (n / 2)) : n).attr('class', '');
    }, <?php echo $time ?>);
}

$('#randomnumber').empty();

for (var i = 0; i < 7; i++) {
    li.push($('<li />').appendTo('#randomnumber'));
}

$('#randomnumber').delegate("li", "click", function() {
        var $this = $(this);

        if (!$this.hasClass('clicked')) {
            if (parseInt($this.text(), 10) === n) {
                $this.addClass('correct');
                $('#hitcount').text(++hitCount);
            } else {
                $this.addClass('wrong');
                $('#misscount').text(++missCount);
            }

            //New code If the missCount > 3 stop the game and save the value
            if(missCount>=2){
               clearInterval(intervalId);
               $('#randomnumber').undelegate("li", "click");
                // Use a ajax request to save the values

$.ajax({
type : 'POST',
url : 'FBhighscore_hwnd.php',
dataType : 'json',
data: {
tgameid: $('#tgameid').val(),MyNumber: $('#MyNumber').val(),totalHits: hitCount
},
success : function(data){
$('#waiting').hide(500);
$('#message').removeClass().addClass((data.error === true) ? 'error' : 'success')
.text(data.msg).show(500);
if (data.error === true)
$('#loginForm').show(500);
else
$('#send').hide(500);       
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
$('#waiting').hide(500);
$('#message').removeClass().addClass('error')
.text('There was an error.').show(500);
$('#loginForm').show(500); 
}
});

            }
        }

        $this.addClass('clicked');
    });

return false;
});


In the php file in the ajax post you could validate the ip against the users ip Such as... if($_SERVER['REMOTE_ADDR'] == $user_ip)) { //do stuff }

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜