dongpu2476 2011-12-04 01:33
浏览 81
已采纳

如何确保无法访问处理ajax请求的PHP脚本并对用户进行身份验证?

I have an jquery ajax request that calls a PHP script that will send an email. This email is happening from the admin, so a user must be authenticated in order to be able to do this. I've got two questions:

  1. How can I lock this PHP file down from somebody being able to go directly to the path in the browser and keep submitting it?
  2. How do I only run the file if the user is authenticated?

PHP:

$emailer = new GiftCardEmailer();
$emailer->SendGiftCardEmail($gift_card);

jQuery:

 $(document).ready(function() {                
     var status = $('p#status');
     status.hide();

     $('#sendemail').click(function() {                   
          $.ajax({
                    url: 'mail-handler.php',
                    beforeSend: function() {
                        status.fadeIn();
                        status.html('<img src="images/ajax-loader.gif" />');  
                    },                        
                    success: function( data ) {
                        if (console && console.log){
                            console.log( 'Sample of data:', data.slice(0,100) );
                        }                            
                        status.html('Email Sent Successfully.');   
                        setTimeout(function() {
                            status.fadeOut(); 
                        }, 4000);                            
                    }
                });
            });
        });
  • 写回答

2条回答 默认 最新

  • doujinge9648 2011-12-04 01:54
    关注

    One approach would be to check for a valid session at the head of the file containing the actual mail code. If no session exists then simply terminate the script.

    Check out ajax and security, might be helpful.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?