Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8707417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:51:58+00:00 2026-06-13T03:51:58+00:00

(Preliminary note: There are seemingly many variants of this question already here, but they

  • 0

(Preliminary note: There are seemingly many variants of this question already here, but they all seem to focus on the filesystem location of the physical PHP files themselves, not the web URL location of things, which is what I’m after.)


INTRO 1:

I have a small “website” with the following physical filesystem structure:

variable.php
folder
  test.php

(In other words, variable.php in the top-level, and test.php in the folder folder.)

Here are the file contents:

<?php
//variable.php
$basePath = dirname($_SERVER['SCRIPT_NAME']);
?>

<?php
//test.php
include("../variables.php");
echo $basePath;
?>

INTRO 2:

When developing this “website” locally, it is on this address:

http://localhost/~noob/test-website

QUESTION:

The problem is that if I put http://localhost/~noob/test-website/folder/test.php into my browser, it prints out /~noob/test-website/folder/.

I want it to print out /~noob/test-website (the “web location” of variable.php). In other words, I want to have global access from all files, even ones deeper in the filesystem hierarchy, to /~noob/test-website. How do I accomplish this?

I obviously do not want to hardcode this path in. The reason is that when I upload it onto a production server, the location changes to something more sane like http://example.com/ (and I don’t want to have to modify this hardcoded path after uploading to the server, hence this question of course).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:51:59+00:00Added an answer on June 13, 2026 at 3:51 am

    The problem here is, that your webserver (and thus PHP) does not know that your testing root directory is being served out of a subdirectory, and in fact neither of them care.

    The obvious solution would be to provide a configuraiton script on each server, that has the hardcoded relative root in it. As you already stated, you don’t want to do this. (It’s not actually a bad thing to provide server-specific configuration files, lots of people do far more absurd things)

    In order to get around this fact, a solution will have to hinge on the reference points that are available to it for a specific request. Those reference points are:

    • The filesystem location of variables.php
    • The filesystem location of the script being requested.
    • The filesystem location that is the root that pages are served from

    (I know you’ve emphasised not filesystem – keep reading, we use these to get your value)

    With these values, there will be an overlap in the directory structure. This is the value you are after.

    Example:

    • Your test site is at: http://localhost/c/
    • Request: http://localhost/c/d/myscript.php
    • variables.php is at: /a/b/c/variables.php
    • myscript.php is at: /c/d/myscript.php includes ../variables.php

    As you can see the paths overlap by the /c/ directory.

    In your variables.php you can use the following code to calculate that overlap.

    $fsRoot = __DIR__; // dirname(__FILE__) for PHP < 5.3 (and shame on you!)
    $webRoot = str_replace($_SERVER['PHP_SELF'], '', $_SERVER['SCRIPT_FILENAME']);
    $urlRoot = str_replace($webRoot, '', $fsRoot);
    

    You should never rely on DOCUMENT_ROOT it can very easily be incorrect, so we’re calculating it ourself ($webRoot variable).

    This takes the filesystem directory of variables.php which is in your site root, and removes the the filesystem directory that is the base that pages are served from. What remains is everything that was between the actual web root, and your subdirectory web root.

    In myscript.php you can do the following.

    print $urlRoot . '/filename.js';
    

    The output from the example would be /c/filename.js

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if this is an obvious question, but some preliminary googling came up with
Preliminary story There is this program which uses the Parallel Port to synchronize with
Dear all, I now have a preliminary macro (defmacro key-if(test &key then else) `(cond
I have a file that has many xml-like elements such as this one: <document
This question is following a previous one which i posted: Django Callback on Facebook
(preliminary note: I'm not yet fully up to speed with the whole 'interop' thing...)
I haven't been able to find any satisfactory coverage of this topic all in
I have a preliminary question I want users to answer before a certain EditText
This is a long text. Please bear with me. Boiled down, the question is:
I have the following scenario (preliminary apologies for length, but I wanted to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.