有条件的PHP包括基于URL? [英] conditional PHP include based on url?

查看:213
本文介绍了有条件的PHP包括基于URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应如何编写条件php以使其生效?
我已经编辑了购物车的主要index.php,以便根据当前url包含适当的配置文件,尽管编写方式无效:

how should a conditional php include be written for it to take effect?
I have edited my cart's main index.php so as to include the appropriate config file based on the current url, though the way it is written doesn't work:

(下面是整个文件,我的版本在//mine START和//mine END之间)

(whole file below, my edition is between //mine START and //mine 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的评论,下面的版本(仍然有些古怪?)效果很好

thanks to @SgtAJ 's comment, the below (still wonky?) edition works fine

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

推荐答案

您可以这样做:

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

这将检查?fr和?en,并且将包括一个文件,无论是否同时存在一个或两个文件.

This will check for ?fr and for ?en, and will include a single file no matter if one, both, or none of those is present.

这篇关于有条件的PHP包括基于URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆