条件PHP包含基于url?

时间:2016-05-26 02:34:09

标签: php include conditional

如何编写条件php包含才能使其生效? 我已经编辑了我的购物车主index.php,以便根据当前网址包含相应的配置文件,但是它的编写方式不起作用:

  

(以下整个文件,我的版本介于//我的START和//我的END之间)

<?php
session_start();

@$cart = $_SESSION["cart"];
if ($cart == "") {
    $cart             = md5(uniqid(rand()));
    $_SESSION["cart"] = $cart;
}
foreach ($_GET as $get_key => $get_value) {
    if ((preg_match("/\//", $get_value)) || (preg_match("/[\\\]/", $get_value)) || (preg_match("/:/", $get_value))) {
        eval("unset(\${$get_key});");
        die("A hacking attempt has been detected. For security reasons, we're blocking any code execution.");
    }
}
if (file_exists("install.php") && !file_exists("lib/datafiles.php")) {
    header("location: install.php");
    exit;
}

// include('config.php'); 
//mine START ROOT INDEX
$url = $_SERVER['PHP_SELF'];
if (strstr($url, "?fr")) {
    include 'configFr.php';
}
if (strstr($url, "?en")) {
    include 'config.php';
} else {
    include 'config.php';
}
//mine END

include('lang/' . $langfront . '.php');
include('lib/datafiles.php');
include('navigation.php');
include('stat.php');
include('gateways/currency.php');
require('lib/version.php');
$url      = basename($_SERVER['PHP_SELF']);
$filename = "db/$ItemsData.dat";
if (file_exists($filename)) {
    $fp = fopen($filename, "r");
    $i  = 0;
    while (!feof($fp)) {
        $offer = fgets($fp, 255);
        $os    = explode('|', $offer);
        if (@$os[7] == 1) {
            $SelectOffer[$i] = $offer;
            $i++;
        }
    }
    fclose($fp);
}
if (isset($SelectOffer)) {
    $nb = count($SelectOffer);
} else {
    $nb = '';
}
$show_per_page = 6;
$nbcol         = 3;
$nbnav         = 7;
$sort          = $nb / $show_per_page;
$sort          = ceil($sort);
$sort++;
$nbr_total = $sort;
if (!isset($_GET['page'])) {
    $nbr_pages = 1;
} else {
    $nbr_pages = $_GET['page'];
}
$row       = 0;
$ShowLigne = '';
if ($nb > $show_per_page) {
    $ShowLigne  = $nb - $show_per_page;
    $navigation = true;
}
if (isset($_GET['page'])) {
    $row       = --$_GET['page'] * $show_per_page;
    $ShowLigne = $nb - $show_per_page - $row;
    if ($ShowLigne < 0) {
        $ShowLigne = 0;
    }
}
function thumbsize($dir, $thumbsize)
{
    if (file_exists($dir)) {
        $ImgSize     = getimagesize($dir);
        $width_orig  = $ImgSize[0];
        $height_orig = $ImgSize[1];
        if ($width_orig <= $height_orig) {
            @$nwidth = $thumbsize * $width_orig / $height_orig;
            @$nheight = $thumbsize;
        } else {
            @$nwidth = $thumbsize;
            @$nheight = $thumbsize * $height_orig / $width_orig;
        }
        $nwidth  = intval($nwidth);
        $nheight = intval($nheight);
        return array(
            $nwidth,
            $nheight
        );
    }
}
function flash($file)
{
    if (file_exists($file)) {
        $fp = fopen($file, "r");
        $ct = fgets($fp, 255);
        fclose($fp);
        @list($idTime, $price, $nprice, $step, $days, $hours) = explode('|', $ct);
        $targetDate       = $idTime;
        $actualDate       = time();
        $secondsDiff      = $targetDate - $actualDate;
        $remainingDay     = floor($secondsDiff / 60 / 60 / 24);
        $remainingHour    = floor(($secondsDiff - ($remainingDay * 60 * 60 * 24)) / 60 / 60);
        $remainingMinutes = floor(($secondsDiff - ($remainingDay * 60 * 60 * 24) - ($remainingHour * 60 * 60)) / 60);
        $remainingSeconds = floor(($secondsDiff - ($remainingDay * 60 * 60 * 24) - ($remainingHour * 60 * 60)) - ($remainingMinutes * 60));
        $year             = date("Y");
        $month            = date("m");
        $refresh          = $remainingHour - 1;
        if ($remainingDay > '0' && $remainingHour == '0') {
            $refresh = 23;
        }
        $coef1      = ($days * 24) + $hours;
        $coef2      = ($remainingDay * 24) + $remainingHour;
        $coef       = ($days == '0') ? $hours - $remainingHour : $coef1 - $coef2;
        $step_price = $price - ($coef * $step);
        if ($secondsDiff <= '1') {
            $remainingDay     = 0;
            $remainingHour    = 0;
            $remainingMinutes = 0;
            $remainingSeconds = 0;
            $step_price       = $price;
            $close            = true;
        }
        $sec = mktime(date("H") + $remainingHour, date("i") + $remainingMinutes, date("s") + $remainingSeconds, $month, date("d") + $remainingDay, $year) - time();
    }
    return array(
        $sec,
        $refresh
    );
}
if (file_exists('pages/home.dat')) {
    $content = @file_get_contents("pages/home.dat");
    @list($page_id1, $title1, $display1, $link1, $url1, $urlrewriting1, $target1, $meta_keywords1, $meta_description1, $registered1, $description1) = explode('|', $content);
    $meta_title = $title . "-" . $title1;
    if ($meta_keywords1 != 'null') {
        $meta_keywords = $meta_keywords1;
    }
    if ($meta_description1 != 'null') {
        $meta_description = $meta_description1;
    }
    if (trim($description1) == 'null') {
        $description1 = '';
    }
    if (preg_match("#{(.*?)}#si", trim($description1), $matches)) {
        if (preg_match("#,#", $matches[1])) {
            list($module, $ModuleFolder) = explode(',', $matches[1]);
        } else {
            $module = $matches[1];
        }
        $description1 = preg_replace('#{(.*?)}#si', '|', $description1);
        list($UpText, $DownText) = explode('|', $description1);
    }
} else {
    $meta_title = $title;
}
?>

<?php
$include = "lib/display_home.php";
$design  = $template;
include("templates/" . $design . "/index.php");
?>

编辑:感谢@SgtAJ的评论,以下(仍然很难?)版本工作正常

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($_SERVER['REQUEST_URI'], '?fr') !== false){include 'configFr.php';}
else {include 'config.php';}

1 个答案:

答案 0 :(得分:2)

你可以这样做:

if (isset($_GET['fr'])) 
    { include 'configFr.php'; }
else if (isset($_GET['en'])) 
    { include 'config.php'; }
else 
    { include 'config.php'; }

这将检查?fr和for?en,并且将包含单个文件,无论是否存在一个,两个或都不存在。