Php Get Current Url

Learn how to get the Current Page URL in PHP.

  1. PHP Forms PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete PHP Advanced PHP Date and Time PHP Include PHP File Handling PHP File Open/Read PHP File Create/Write PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP Callback Functions PHP JSON PHP Exceptions PHP OOP.
  2. $SERVER is a superglobal variable in PHP, that provides information about headers, paths, and script locations. Besides the various uses of $SERVER variable, it helps to get current page URL in PHP. You can use $SERVER variable to get current page URL in PHP. The following example code shows how to get current full URL with query string using $SERVER variable in PHP.
  3. I want to add custom PHP code to ensure that whenever a page on my site loads in my browser, the URL of that page is echoed to the screen. I can use echo getpermalink, but that does not work on all pages.Some pages (e.g. My homepage) display several posts, and if I use getpermalink on these pages, the URL of the displayed page is not returned (I believe it returns the URL of the last.

If you want to implement smaller PHP projects and the decision goes against large frameworks, the question of URL routing will eventually come up. How does one particular URL cause a certain content to be outputted? I want to describe a simple and very elegant way of processing search engine friendly URLs using RegExp and PHP's anonymous functions.

Update: Since this post counts to one of the most read on this blog I have given the code a complete refacturing. You can also find the complete code on Github:

When I started working with PHP many years ago, pages were often generated by using the GET, POST or REQUEST variables and complex if statements. Often you could see URLs with many parameters like index.php?page=5&action=delete&return_to_page=3. The front controllers usually looked like this:

This method quickly becomes confusing and you lose yourself in parameters and if loops. The result is spagehtti code. In addition, the URLs are difficult to understand and not easy to remember. However, anonymous functions and RegExp make it easy to configure, search engine friendly, and elegant routes. An example index.php could look like this:

Any routes can be created here. The parameters can be parsed from the route via RegExp and passed to the handler. In addition, you can control through which methods (get, post, put, patch, etc ...) the routes may be accessed. The handlers gets only executed if the routes match the entered path.

Structure of the Route.php class:

As you can see, the router consists of less code than you might think. Only if the run() method gets executed, the individual routes registered with add () will be checked and executed. When running, the base path in which the project lives in, is always placed before the pattern and thus taken into account. If no match is found, the 404 route or the 405 route is automatically triggered.

Php Get Current Url Path

Of course all requests have to be forwarded to index.php with a rewrite. This can be reached on Appache with a .htaccess file:

This URL router is still very expandable, but it can already provide a solid basis for smaller projects.

Php Get Page Url

Php get current url with parameters

Php Get Current Url File Name

You can find the source on Github: