Jump to content

php include variable issue


r00t0101

Recommended Posts

Hello all,

  I was wondering if this could be done. I would like to re-design my site where the files being called on is using global variables instead of using syntax "../" or "../../". I would like use a more readable approach using global variables like $dir_name/file_name instead if possible. I figured this way there will only be one place to update instead of scanning through the whole file doing a search and replace on each item.

  Below is a general idea of what I would like. In this example I have four files: a global, header, footer and an index file. The index file includes the global file without a problem however I had problems executing the header and footer files when using variables as seen commented.

 

Respectively yours,

Rick

 

<!-- Start testGlobal.php -->

<?

    $CFG->root = "http://127.0.0.1/test";

 

$CFG->headerDir = "$CFG->root/header";

$CFG->footerDir = "$CFG->root/footer";

?>

<!-- End testGlobal.php -->

 

<!-- Start index.php -->

<?php

include("TestGlobal.php");

?>

 

<!-- <? echo $CFG->headerDir; ?>/header.php -->

<?php include ("./header/header.php"); ?>

 

bla bla bla<br><br>

 

<!-- <? echo $CFG->footerDir; ?>/footer.php -->

<?php include ("./footer/footer.php"); ?>

<!-- End index.php>

 

<!-- Start header.php -->

<html>

<head>

<title>Hello world</title>

</head>

Header Test Section<br><br>

<body>

</html>

<!-- End header.php -->

 

<!-- Start footer.php -->

Copyright @ test.com

<!-- End footer.php>

Link to comment
Share on other sites

Just curious,  so there is no way to use URLs in my example. Just wondering how that would work if the site is being hosted other than locally?

 

I tried $CFG->root = /var/www as well as some other combinations but I still get errors.

Link to comment
Share on other sites

Just curious,  so there is no way to use URLs in my example. Just wondering how that would work if the site is being hosted other than locally?

 

I tried $CFG->root = /var/www as well as some other combinations but I still get errors.

 

i think you can not sure but give something like this a try

<?php

    $CFG->root = "http://127.0.0.1/";

 

$header = "header";

 

$CFG->headerDir = $CFG->root.$header;

 

?>

 

that should output (using and echo statment)

 

http://127.0.0.1/header

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.