How to determine the address of the current page in PHP?

Table of contents:

How to determine the address of the current page in PHP?
How to determine the address of the current page in PHP?
Anonim

The site developer can determine the desired content of the link to the web resource, and the search engine itself generates a query string to the site. In various cases, the address bar of a browser may contain more than just an indication of the protocol and domain name.

URL is a combination of important elements that can be used to form adequate content and collect statistics. Getting the address of the current page, parsed into essential components, is easy in PHP.

Main source of information

PHP offers a wide range of data to the developer when a page is requested by the browser. As soon as the visitor arrives at the site, a request is sent to the server, which fills the global PHP array - $_SERVER - with important information.

Global array Array
Global array Array

The address of the current page has a double meaning:

  • for development purposes in code context;
  • for content generation and statistics purposes.

In the first case, the values of $_SERVER['DOCUMENT_ROOT'] and $_SERVER['PHP_SELF'] arethe root of the site and the name of the requested page. Site folders that contain the necessary files are conveniently addressed relative to the site root, and not tied to absolute addresses. Knowing the name of the current page also makes sense.

Using a URL
Using a URL

In the second case, you can determine what content to display on the page, what language to use, what design, what to place for one reason or another.

Manage site via URL

Links to the site can be placed with the parameters and / or the desired start page. By placing different links on different third-party resources, you can get the address of the current page and the set of parameters that were specified through PHP.

For example, on the sites of one region of the country there are links to the site page 'visit.php' with the parameter 'region=1', on the sites of another region - 'statements.php' and the parameter 'call=phone'. There may be many options. Knowing what, where and why was posted, you can analyze visits, see where someone came from, and respond accordingly.

Resource links
Resource links

Using PHP, the address of the current page is obtained disassembled into components. You can parse the value of QUERY_STRING (parameter string), you can find out the protocol, port, time and other data.

Summarizing data from multiple hits, you can generate objective information about visitors and site performance.

Popular topic

Editor's choice

  • Windows 7 password bypass: possible methods and recommendations from experts
    Windows 7 password bypass: possible methods and recommendations from experts

    The fact that users (most often computer administrators) protect Windows operating systems from unauthorized use, for example, in their absence at the computer, does not surprise anyone. However, often the administrator may not give the registered user enough rights to perform any actions or set personal settings, and therefore it may be necessary to log in with an administrator registration

  • How to remove a virus from a flash drive without losing data?
    How to remove a virus from a flash drive without losing data?

    Removable USB storage devices in the form of the most common flash drives are susceptible to viruses no less than hard drives with operating systems installed on them. And it is often quite problematic to identify the presence of such a threat or neutralize it. How to remove a virus from a flash drive and restore files (hidden or infected) will be discussed further

  • File viruses are computer code to achieve goals bypassing security systems
    File viruses are computer code to achieve goals bypassing security systems

    File viruses are computer code to achieve goals bypassing security systems. This is what inspired people to create antiviruses, what allows hackers to learn and steal millions of dollars every day

  • PC prevention for stable operation - expert advice. Computer help
    PC prevention for stable operation - expert advice. Computer help

    The fact that almost all modern computer systems require constant care, apparently, all users know. But not everyone has a clear enough idea of what this should manifest itself in. To keep the computer, operating system and installed applications in the most efficient condition, it is necessary to carry out preventive maintenance of the PC software and monitor the hardware components (installed equipment)

  • Detector software: what is it in the field of anti-virus protection?
    Detector software: what is it in the field of anti-virus protection?

    Apparently, many users of modern computer systems have heard or know that there are so-called detector programs. What they are is easy to understand if you just turn to the translation or interpretation of the English word detect, which literally means “to detect”