1

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:

(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");
?>

EDIT: 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';}
4
  • Fellows, please correct me if I'm wrong: files that are inluced/required will be inluded before executing the actual script. That means you cannot include a file conditionally. Commented May 26, 2016 at 2:39
  • 1
    For starters, your ifs are wonky. Yeah, I said wonky. :) You have an if, then an if-else, so either way, config.php is going to be included. Was this intentional? Also, you might want to use strpos() instead of strstr() for better performance. For that matter, couldn't you just test for $_GET['fr'] ? @Jeff, according to the PHP documentation at php.net/manual/en/function.include.php, the includes don't have to be first, since it mentions them possibly being in functions. Commented May 26, 2016 at 2:45
  • @SgtAJ Yes, I've checked the docs, but couldn't find anything about such conditions. Just had it somewhere in the back of my head that includes as processed first. Beeing in functions is an argument, though.... I hereby take back my first comment.... Commented May 26, 2016 at 2:55
  • They hid that tidbit after the first example on that doc page. Commented May 26, 2016 at 3:34

1 Answer 1

3

You could just do it this way:

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

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.