How to Find the Document Root using PHP Script

How to Find the Document Root using PHP Script Техника

Introduction

$_SERVER is a superglobal that holds information regarding HTTP headers, path and script location etc. All the server and execution environment related information is available in this associative array. Most of the entries in this array are populated by web server.

The document root, also known as the web root, is the topmost directory in your web server’s file structure where your website’s files and folders are stored. It is a crucial aspect of web development, as it serves as the starting point for your website. In this article, we will explore how to find the document root using PHP scripts, which will help you manage your website more effectively and enhance your web development skills.

Step 1: Understanding the $_SERVER Variable

Step 2: Displaying the Document Root

  1. Open your preferred text editor or integrated development environment (IDE).
  2. Create a new PHP file and save it with a .php extension, such as “find_document_root.php”.
  3. Add the following code to your newly created PHP file:
  4. Save the changes to the file.
  5. Upload the “find_document_root.php” file to your web server.
  6. Access the script via your web browser using your domain or server’s IP address, followed by the script’s file path (e.g., http://example.com/find_document_root.php).
Identifying Your Document Root using PHP Script
Getting the Document Root using PHP

The output may differ depending on your server’s configuration and operating system. The displayed path is the location where your website’s files and folders are stored on the server.

Дополнительно:  Как проверить монитор на битые пиксели и все починить |

Step 3: Using the Document Root in Your PHP Scripts

Once you know your document root, you can use it in your PHP scripts to access files or directories relative to the document root. This helps ensure that your file paths are consistent and accurate, regardless of where your script is located within your website’s file structure.

  1. In your PHP script, assign the value of $_SERVER[‘DOCUMENT_ROOT’] to a variable:
  2. When referencing files or directories in your script, concatenate the $docRoot variable with the relative path:

By using the document root variable in your PHP scripts, you can ensure that your file paths are accurate and reliable, regardless of the script’s location within your website’s structure.

Conclusion

Example

<?php
echo $_SERVER['PHP_SELF'];
?>
/test/testscript.php

SERVER_ADDR − This property of array returns The IP address of the server under which the current script is executing.

SERVER_NAME − Name of server hostunder which the current script is executing.In case of a erver running locally, localhost is returned

QUERY_STRING − A query string is the string of key=value pairs separated by & symbol and appended to URL after ? symbol. For example, http://localhost/testscript?name=xyz&age=20 URL returns trailing query string

REQUEST_METHOD − HTTP request method used for accessing a URL, such as POST, GET, POST, PUT or DELETE. In above query string example, a URL attached to query string wirh ? symbol requests the page with GET method

DOCUMENT_ROOT − returns name of directory on server that is configured as document root. On XAMPP apache server it returns htdocs as name of document root

C:/xampp/htdocs
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36

SERVER_PORT − port number on which the web server is listening to incoming request. Default is 80

Дополнительно:  Синий экран смерти Windows 10: причины, способы решения

Example

<?php
foreach ($_SERVER as $k=>$v)
echo $k . "=>" . $v . "<br>";
?>

List of all server variables

MIBDIRS=>C:/xampp/php/extras/mibs
MYSQL_HOME=>\xampp\mysql\bin
OPENSSL_CONF=>C:/xampp/apache/bin/openssl.cnf
PHP_PEAR_SYSCONF_DIR=>\xampp\php
PHPRC=>\xampp\php
TMP=>\xampp\tmp
HTTP_HOST=>localhost
HTTP_CONNECTION=>keep-alive
HTTP_CACHE_CONTROL=>max-age=0
HTTP_DNT=>1
HTTP_UPGRADE_INSECURE_REQUESTS=>1
HTTP_USER_AGENT=>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36
HTTP_ACCEPT=>text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
HTTP_SEC_FETCH_SITE=>none
HTTP_SEC_FETCH_MODE=>navigate
HTTP_SEC_FETCH_USER=>?1
HTTP_SEC_FETCH_DEST=>document
HTTP_ACCEPT_ENCODING=>gzip, deflate, br
HTTP_ACCEPT_LANGUAGE=>en-US,en;q=0.9,mr;q=0.8
PATH=>C:\python37\Scripts\;C:\python37\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\AMD\ATI.ACE\Core-Static;C:\python37\Scripts\;C:\python37\;C:\Users\User\AppData\Local\Microsoft\WindowsApps;C:\Users\User\AppData\Local\Programs\MiKTeX 2.9\miktex\bin\x64\;C:\MicrosoftVSCode\bin
SystemRoot=>C:\Windows
COMSPEC=>C:\Windows\system32\cmd.exe
PATHEXT=>.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW
WINDIR=>C:\Windows
SERVER_SIGNATURE=>
Apache/2.4.41 (Win64) OpenSSL/1.0.2s PHP/7.1.32 Server at localhost Port 80

SERVER_SOFTWARE=>Apache/2.4.41 (Win64) OpenSSL/1.0.2s PHP/7.1.32
SERVER_NAME=>localhost
SERVER_ADDR=>::1
SERVER_PORT=>80
REMOTE_ADDR=>::1
DOCUMENT_ROOT=>C:/xampp/htdocs
REQUEST_SCHEME=>http
CONTEXT_PREFIX=>
CONTEXT_DOCUMENT_ROOT=>C:/xampp/htdocs
SERVER_ADMIN=>postmaster@localhost
SCRIPT_FILENAME=>C:/xampp/htdocs/testscript.php
REMOTE_PORT=>49475
GATEWAY_INTERFACE=>CGI/1.1
SERVER_PROTOCOL=>HTTP/1.1
REQUEST_METHOD=>GET
QUERY_STRING=>
REQUEST_URI=>/testscript.php
SCRIPT_NAME=>/testscript.php
PHP_SELF=>/testscript.php
REQUEST_TIME_FLOAT=>1599118525.327
REQUEST_TIME=>1599118525

Оцените статью
Master Hi-technology
Добавить комментарий