Jump to content

Include a scalar in my include argument?


tmallen

Recommended Posts

I set a pretty global variable ($base) that defines the site root. Very useful with my includes. My only problem is using this URL prefix in include() arguments. Example:

<?php
$base = '/mybaseurl/';
include($base . 'includes/header.php');
?>

I've tried smushing $base in there in many different ways, and I ALWAYS get errors. How can I fit $base into this sort of statement?

Link to comment
Share on other sites

Basically in includes they are always path structure not URL (if you include a URL it would give you the output of the php source not the source itself) so if you can provide some code it would be helpful... you could use $_SERVER['DOCUMENT_ROOT'] for apache to define your root directory

 

more information on the same can be found at

 

http://in.php.net/function.include

Link to comment
Share on other sites

Oh, I understand my problem (sort of). Obviously $base couldn't be called because $base is defined in header.php! What's a better way of declaring this $base variable so that I can easily use it throughout the site? My thinking just feels...backward.

 

Also, $base calls within my PHP code don't work even when added properly with $base defined (include() calls, mainly), but <?php print $base; ?> works fine inline (calling an image, stylesheet, etc.).

Link to comment
Share on other sites

Problem is I can't edit $_SERVER['Doc...']. I would just precede my statements with the root indicator ('/') if that were the case. I'm declaring my own site root here, so that no matter where I deploy, I only have to change a single variable to adapt to the server and directory structure.

Link to comment
Share on other sites

You may be running into problems with variable scope.  The easiest way around this is to use mod_rewrite to direct all request through a single PHP script.  From that script you can define constants to use throughout the site; I highly recommend using constants over a variable for what you're doing.

 

Example directory structure:

/home/username/public_html/
/home/username/public_html/index.php
/home/username/public_html/constants.php
/home/username/public_html/Models
/home/username/public_html/Models/user.php
/home/username/public_html/Models/news.php

 

First you set up mod_rewrite to direct all requests through index.php.

 

index.php

  require_once(realpath(dirname(__FILE__)) . 'constants.php');
  
  // Examine the URL and route as necessary

 

constants.php

  define( 'DIR_SRC', realpath(dirname(__FILE__)) ); // source root directory
  define( 'DIR_MODELS', DIR_SRC . 'Models/' ); // Model directory
  // define more constants as necessary

 

random_file.php

  // We got here through index.php, thus all the constants in constants.php are declared
  include(DIR_MODELS . 'user.php');

  $user = new User();
  $user->throwOffCliff();

Link to comment
Share on other sites

Both constants.php and index.php should reside in your top-level web-accessible directory; this is usually the directory you land in when you FTP to your account.

 

If your web host supports it, you can set up mod_rewrite via a .htaccess file; it should reside in the same directory as index.php and constants.php.  I won't bother with the implementation details as there's plenty of tutorials on it and even a specific thread area on this discussion board for mod_rewrite.

 

One last thing, identical paths beginning with "/" are working for images, etc. but failing on plain include() statements. Why would this be?

Depends on how you're using the images.  If you're just passing URLs back to the browser then it's not the PHP interpretor that's seeing the paths but the browser, in which case '/' would refer to the directory starting from public_html (or www or whatever your web host calls it).

 

Remember, / is the top-level directory on a *nix file system.  Any time in your PHP code you try and work on a file:

/path/to/some/file

it will look for that file relative to the top-level directory of the file system.  Basically, / on *nix is equivalent to c:\ on Windows.

 

The reality is that you probably have an account on that file system and your account is mapped to:

/home/users/your_account_name/

And finally within your directory is the one accessible via web browsers, usually named public_html or www:

/home/users/your_account_name/public_html

 

Thus in your PHP code:

<?php
  include('/some/include/file.php');
  /* Above maps to:
      /some/include/file.php
      It DOESN'T map to:
      /home/users/your_account_name/some/include/file.php
  */
?>

 

However if you were to do:

<?php
  echo '<img src="/images/house.jpg" alt="A House"/>';
?>

This path is seen by the web browser.  The web browser sends a request to www.yourdomain.com for the file:

/images/house.jpg

As far as the web browser is concerned, the path / is mapped to:

/home/users/your_account_name/public_html

Thus, the path:

/images/house.jpg

is mapped to:

/home/users/your_account_name/public_html/images/house.jpg

 

 

I probably didn't explain that as clearly as I could have, so here is an attempt at a short summary.  A file path beginning with '/' from within PHP refers to the root directory of the server.  This is not the same thing as a src attribute in the HTML that begins with '/', which simply means start the path at http://www.yourdomain.com/.  The reason its not the same is http://www.yourdomain.com is not mapped to the root directory of the entire server; instead, it is mapped to the web-accessible directory of your account.

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.