如何确保处理ajax请求的PHP脚本无法使用,并且用户已经过身份验证


How to make sure a PHP script that handles an ajax request is not accessible to use and the user is authenticated?

我有一个jquery ajax请求调用PHP脚本,将发送电子邮件。这封电子邮件是从管理员发生的,所以用户必须经过身份验证才能做到这一点。我有两个问题:

  1. 我如何锁定这个PHP文件,从有人能够直接到浏览器中的路径,并继续提交它?
  2. 如何设置用户通过认证后才运行文件?
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);                            
                    }
                });
            });
        });

一种方法是在包含实际邮件代码的文件头部检查有效会话。如果不存在会话,则直接终止脚本。

查看ajax和安全性,可能会有帮助。

我在php页面的顶部使用session_start(),然后检查我的特定会话变量进行身份验证。