Home  >  Article  >  Backend Development  >  ThinkPHP中url隐藏入口文件后接收alipay传值的方法_PHP

ThinkPHP中url隐藏入口文件后接收alipay传值的方法_PHP

WBOY
WBOYOriginal
2016-05-31 18:15:34725browse

本文实例讲述了ThinkPHP中url隐藏入口文件后接收alipay传值的方法。分享给大家供大家参考。具体方法如下:

现在公司项目的需求变化多端,项目使用的是Thinkphp2.0,而conf.php中设置的URL_MODEL=2,为了兼容.htaccess和隐藏index.php,但在使用一些第三方接口时(例如支付宝或MSN开放平台),他们返回的参数往往含有一个?号,而URL_MODEL=2时,TP会自动将含有?的URL进行转换.

转换前:http://www.xxx.com/index.php/alipay/callback/?is_success/T/sign_type/MD5/notify_id/RqPnCoPT3K9/vwbh3I

转换后:http://www.xxx.com/alipay/callback/is_success/T/sign_type/MD5/notify_id/RqPnCoPT3K9/vwbh3I

转换后的网址将无法访问,而转换前的网址是可以访问的,这时需要临时针对alipay这个控制器,URL_MODEL=1,但在该控制器里C('URL_MODEL',1)是没有用的,因为TP会在入口文件App::run()时直接404了.

想了很多方法,先是修改它的配置文件Conf.php,结果发现会生成缓存文件~app.php,所以必须根据情况删除这个缓存文件,于是我对index.php入口文件进行了修改(事实上,放在任何App::run()之前都可以,你可以自行封装).

PHP实例代码:

代码如下:

// 定义ThinkPHP框架路径 
define('THINK_PATH', '不告诉你我的TP文件夹名/'); 
//如果网址中存在这样的形式就删除缓存配置文件,并创建一个标识 
if(strstr($_SERVER['REQUEST_URI'],'alipay/callback/')||strstr($_SERVER['REQUEST_URI'],'order.return_pay')){ 
    unlink('./home/Runtime/~app.php'); 
    //创建一个缓存文件做为标识 
    $fn= fopen('./home/Runtime/re_url_model.php','wb'); 
    fclose($fn); 
}elseif(file_exists('./home/Runtime/re_url_model.php')){ 
    unlink('./home/Runtime/re_url_model.php'); 
    unlink('./home/Runtime/~app.php'); 

 
//定义项目名称和路径 
define('APP_NAME', 'home'); 
 
define('APP_PATH', 'home'); 
 
// 加载框架入口文件 
 
require(THINK_PATH."ThinkPHP.php"); 
 
//实例化一个网站应用实例 
App::run(); 
?>


项目的配置文件Conf.php根据你自己的情况做一个判断即可.

PHP实例源码:

代码如下:

//兼容支付宝快捷登录的?请求 
if(strstr($_SERVER['REQUEST_URI'],'alipay/callback/')||strstr($_SERVER['REQUEST_URI'],'order.return_pay')){ 
    $my_array['URL_MODEL'] = 1; 

return $my_array;


这样,访问www.xxx.com或www.xxx.com/shipin/等任何非alipay控制器的页面时,网站依然是使用url_model = 2来隐藏index.php,使得URL友好.

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn