dongliang7545 2010-12-03 16:58
浏览 33

如何确保我的PHP在所有服务器环境中都可移植?

It's not a rare thing for me to deploy PHP to a server, or move it from one server to another, and have it completely fail with database or cookie errors. I'd like to get better at avoiding these kinds of issues before they happen.

What are some potential portability problems in PHP, with regards to different servers, OS's, and PHP configurations, and how can they be worked around? Let's assume that I (unfortunately) have no control over the server environment, besides FTP. (I'll add one answer to get things started)

  • 写回答

3条回答 默认 最新

  • doudi4137 2010-12-03 17:01
    关注

    Some servers are configured with magic quotes enabled (magic_quotes_gpc=1 in php.ini), which automatically calls addslashes() on $_GET, $_POST, $_COOKIE, and $_REQUEST. It was originally introduced to help beginners unknowningly write more secure code, but its use is now depreciated.

    To simulate disabling this misfeature at runtime if it's enabled, run the following code before using any of the request vars (adapted from this comment):

    function destroyTheMagic($array, $topLevel = true) {
        $ret = array();
        foreach($array as $key => $value) {
            if(!$topLevel)
                $key = stripslashes($key);
            if(is_array($value))
                $ret[$key] = destroyTheMagic($value, false);
            else
                $ret[$key] = stripslashes($value);
        }
        return $ret;
    }
    
    if(get_magic_quotes_qpc()) {
        $_GET = destroyTheMagic($_GET);
        $_POST = destroyTheMagic($_POST);
        $_COOKIE = destroyTheMagic($_COOKIE);
        $_REQUEST = destroyTheMagic($_REQUEST);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献