您的位置:首页 > 编程语言 > PHP开发

PHP中跳转,转向,ajax判断

2013-06-03 20:34 204 查看
ajax判断

function isAjax() {

    if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {

        if ('xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])) {

            return true;

        }

    }

    return false;

}

ajax返回

function ajax_return($data, $info, $status, $type = 'json') {

    $result = array();

    $result['status'] = $status;

    $result['info'] = $info;

    $result['data'] = $data;

  echo json_encode($result);

exit;

}

转向

function _return($info,  $url = '') {

        //页面跳转

        header("Content-Type: text/html; charset=utf-8");

        if (empty($url)) {

            $url = @$_SERVER["HTTP_REFERER"];

        }

        header("refresh:{$time};url={$url}");

        //header("Location:$url"); 

        exit($info);

}

跳转

function redirect($url, $time = 0, $msg = '') {

    //多行URL地址支持

    $url = str_replace(array("\n", "\r"), '', $url);

    if (empty($msg))

        $msg = "系统将在{$time}秒之后自动跳转到{$url}!";

    if (!headers_sent()) {

        // redirect

        if (0 === $time) {

            header("Location: " . $url);

        } else {

            header("refresh:{$time};url={$url}");

            //fixed   添加编码 by lee  2011年10月12日 09:29:50

            header("Content-type: text/html; charset=utf-8");

            echo($msg);

        }

        exit();

    } else {

   

        $str = "<meta http-equiv='Refresh' content='{$time};URL={$url}>";

        $str .='<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';

        if ($time != 0)

            $str .= $msg;

        exit($str);

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: