duanmao1919 2012-04-19 02:04
浏览 16
已采纳

仅允许PHP文件与之前网页的“授权”一起运行[重复]

Possible Duplicate:
AJAX only access

So I have these javascript functions I run depending on who won a contest.

function winA() 
{
var x = "<?php echo $id;?>"
$.ajax({ url: 'win.php' ,
         data: { id: x }
   }); 
}

function winB() 
{
var val = "<?php echo $i;?>"
$.ajax({ url: 'win.php' ,
         data: { id: val }
   }); 
}

The file win.php updates a MySQL with who won. What troubles me is if someone simply goes to

http://mysite.com/win.php?id=1

and refreshes it a whole lot of times I clearly will have some incorrect results on the MySQL server.

I am wondering exactly how one would make it so that URL could only be accessed when appropriate, via this ajax call. Alternatively make it so the win.php file only to actually execute when it has 'authorization' that expires after loading the php file.

Thanks in advance, Aaron.

  • 写回答

3条回答 默认 最新

  • douquanqiao6788 2012-04-19 02:24
    关注

    You could use a session, expanding from my comment:

    Main game page

    <?php
    session_start();
    
    //Do game....
    
    //Decide Winner
    
    $_SESSION['winner']='A'; //or B depending on who won
    
    ?>
    

    Then an AJAX request with blank params ect, just so won.php gets touched

    function win(){
      $.ajax({
        url: "win.php",
        cache: false
      }).done(function(html){
       //Dont do anything or you could get a response from the won.php request an update something
      });
     }
    

    Win.php

    <?php
    session_start();
    
    if(isset($_SESSION['winner'])){
        $winner=$_SESSION['winner'];
        //Do update ect
    
        //Unset Winner session var, by unsetting the user would need to play the game again to get it set
        unset($_SESSION['winner']);
    }else{
        header('Location: ./index.php');
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制