路由转发(curl)

<?php
ini_set('memory_limit', '640M');
ini_set('default_charset', 'utf-8');
define('webroot', 'http://' . $_SERVER['HTTP_HOST']. substr($_SERVER['PHP_SELF'], 0, strpos($_SERVER['PHP_SELF'], '/', 0)) . '/');
// m 模块 c 子模块 a 类 v 视图
$m = $_GET["m"] ? $_GET["m"] : $_POST["m"];
$c = $_GET["c"] ? $_GET["c"] : $_POST["c"];
$a = ($_GET["a"] ? $_GET["a"] : $_POST["a"]);
$a = $a ? $a . '.class' : $a;
$a = $_GET["v"] ? $_GET["v"] : ($_POST["v"] ? $_POST["v"] : $a);
session_start();
$_SESSION['action'] = $c;
if (!$a) {
    // 已登录跳转
    if ($_SESSION['admin']) $a = 'main';
    else $a = 'login';
}
if ($_GET && $_GET['id']) $url = "$m/$c/$a.php?id=".$_GET['id'];
else $url = "$m/$c/$a.php";
$ch = curl_init(); //初始化
if (strrpos($url, 'logout.php') > -1) {
    session_destroy();
    $url = 'login.php';
}
if ($_GET) {
    $url .= $_SERVER['REQUEST_URI'];
}
// post数据转发
if ($_POST) {
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$_POST);
    header("Content-Type:application/json;charset='utf-8'");
}
// 获取访问页面
curl_setopt($ch, CURLOPT_URL, webroot . "$url"); //你要访问的页面
// 在转发页面无法正常使用session,写入了cookie
curl_setopt($ch, CURLOPT_COOKIE, 'name=' . $_SESSION['admin'] . ';action=' . $_SESSION['action']);
 //是否显示内容,不清楚为什么0才是显示
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_exec($ch); //执行
curl_close($ch); //返回关闭
//header("location:$url");
?>
原文地址:https://www.cnblogs.com/gabin/p/3927485.html