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

  • Home
  • SEARCH
  • 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 790721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:43:31+00:00 2026-05-14T21:43:31+00:00

I found this thread on StackOverflow about how to get the Application Root from

  • 0

I found this thread on StackOverflow about how to get the “Application Root” from inside my web app.

However, most of the approaches suggested in that thread can hardly be applied to my Xampp on Windows. Say, I’ve got a “common.php” which stays inside my web app’s app directory:

/
/app/common.php
/protected/index.php

In my common.php, what I’ve got is like this:

define('DOCROOT', $_SERVER['DOCUMENT_ROOT']);
define('ABSPATH', dirname(__FILE__));
define('COMMONSCRIPT', $_SERVER['PHP_SELF']);

After I required the common.php inside the /protected/index.php, I found this:

C:/xampp/htdocs     //<== echo DOCROOT;
C:\xampp\htdocs\comic\app //<== echo ABSPATH
/comic/protected/index.php //<== echo COMMONSCRIPT

So the most troublesome part is the path delimiters are not universal, plus, it seems all
superglobals from the $_SERVER[] asso array, such as $_SERVER[‘PHP_SELF’], are relative to the “caller” script, not the “callee” script.

It seems that I can only rely on dirname(__FILE__) to make sure this always returns an absolute path to the common.php file.

I can certainly parse the returning values from DOCROOT and ABSPATH and then calculate the correct “application root”. For instance, I can compare the parts after htdocs and substitute all backslashes with slashes to get a unix-like path

I wonder is this the right way to handle this? What if I deploy my web app on a LAMP environment? would this environment-dependent approach bomb me out?

I have used some PHP frameworks such as CakePHP and CodeIgniter, to be frank, They just work on either LAMP or WAMP, but I am not sure how they approached such a elegant solution.

Many thanks in advance for all the hints and suggestions.

  • 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-05-14T21:43:32+00:00Added an answer on May 14, 2026 at 9:43 pm

    dirname(__FILE__) is a common method of getting the path. Actually, if your script will only run on PHP 5.3 or later, __DIR__ will do the same thing and is a little shorter to type.

    I think your strategy involving common.php is a little redundant. For starters, DOCROOT and COMMONSCRIPT are useless – they just duplicate some data that is already global. (All PHP developers know what $_SERVER['PHP_SELF'] is, but I personally would not know what COMMONSCRIPT means without looking it up.)

    Second, it may be more trouble than it’s worth. Compare some different ways of getting the path to /protected/protected2.php from /protected/protected.php:

    ABSPATH . "/../protected/protected2.php";
    "protected2.php"
    dirname(__FILE__) . "/protected2.php";
    

    All of these are valid, but some are easier than others. This just illustrates that what you are asking for (getting the application root) may not always be what you need. So I would say, there is no magic path bullet that will solve all of your problems. If you find yourself in a situation in which the ABSPATH constant helps you, use it.

    Second part of your question: Forward slashes will work on Windows, even mixed with backslashes. For example:

    include("c:\whatever\whatever/something/anything.php");
    

    will work in PHP on Windows (at least on any 5.0+ version; not sure about 4.x). Backslashes will not work on Unix, so just always type forward slashes and you’ll be cross-platform-safe.

    If you are writing a cross-platform script, you never want to code in a drive letter. Instead, write it like this:

    include("/whatever/whatever/something/anything.php");
    

    The initial / will be interpreted by PHP on Windows as c:\. (I don’t know how to point to a different drive letter in a cross-platform way.)

    HTH

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

Sidebar

Ask A Question

Stats

  • Questions 423k
  • Answers 423k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You write the 5 characters into the cPort array. That's… May 15, 2026 at 11:44 am
  • Editorial Team
    Editorial Team added an answer Check your @@character_set_results. By default, MySQL uses latin1, not utf8.… May 15, 2026 at 11:44 am
  • Editorial Team
    Editorial Team added an answer I would suggest using a template instead of setting the… May 15, 2026 at 11:44 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.