I'm following a shopping cart tutorial and this snippet is confusing. I have a vague idea, but can some one pls explain what this means?

// setting up the web root and server root for
// this shopping cart application
$thisFile = str_replace('\\', '/', __FILE__);
$docRoot = $_SERVER;

$webRoot = str_replace(array($docRoot, 'library/config.php'), '', $thisFile);
$srvRoot = str_replace('library/config.php', '', $thisFile);

define('WEB_ROOT', $webRoot);
define('SRV_ROOT', $srvRoot);

What are web and server roots?

Thnks

Dani AI

Generated

The snippet is creating two different "base" values the app can use: one for building URLs that browsers see, and one for PHP to find files on the server. is on the right track — the first (what the author calls WEB_ROOT) is a web-facing path used when creating href/src links in templates; the second (SRV_ROOT) is an absolute filesystem path used by include/require and file operations.

WEB_ROOT should be the path portion that sits under the web server document root (so links point to the correct subfolder when the app is not at the site root). The code normalizes separators and removes the config file portion so a clean URL prefix remains. $_SERVER values like DOCUMENT_ROOT are often involved here; see the PHP docs for the server variables for details ($_SERVER manual).

SRV_ROOT is the filesystem directory the app lives in. It is what PHP uses to locate files on disk. Modern code often relies on __DIR__ or dirname() of a known file to get that path, then canonicalizes it with realpath() to avoid problems with symlinks or mixed slashes; see the docs for predefined constants and dirname() for options (predefined constants, dirname).

Troubleshooting tips: print or log both roots to verify values (watch trailing slashes). If links 404, check that WEB_ROOT has the right leading slash and points to the app folder. If includes fail, confirm SRV_ROOT matches the actual disk path and that DOCUMENT_ROOT is set (it may be empty on CLI). Never expose server filesystem paths in client HTML. Using a single, explicit config constant (set once at bootstrap using __DIR__/realpath) is often safer than trying to infer paths in multiple places.

The webroot is the top level directory where files will be served to the web. For instance the directory structure may look like this:

/home/accountName/public_html/

In this case only files in public_html/ would be available to the people browsing the site. Also in this case I think they may be looking for a string to append to the begining of urls so the links in template files would point to the right place.

I think the server root in this case is probably the file path to the files used by the shopping cart.

In a more basic sense, the author doesn't expect the shopping cart to be placed in the top level directory and needed a way to determine what subfolders it is in.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.