- How to Get Current Page URL, Domain, Query String in PHP
- How to Get Current Page URL in PHP
- PHP Program for get Current Page Url
- Program 1 full source Code
- PHP Program to show current page URL
- PHP Program show Current page full URL
- Program 2 full source Code
- PHP Program show Current page full URL
- PHP Program show Current full URL with Query String
- Program 3 full source Code
- PHP Program show Current page full URL with Query String
- Other Important elements of $_SERVER that are very are useful and you must know that:
- Author Admin
- Get url from page php
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
- Most convenient way to Extract URLs from webpage using PHP
- Normal way of extracting urls from web-page (not recommended)
- The most convenient Example of extracting links (URLs) from any Webpage (Recommended by infoconic)
- PHP Get URL – How to Get the Full URL of the Current Page
- What are Superglobals?
- The $_SERVER Superglobal
- So, how do we get the full URL?
- Output
- Summary
How to Get Current Page URL, Domain, Query String in PHP
In this tutorial, We would love to share with you, How to get current page URL in PHP, PHP get current URL path, PHP get full URL with parameters, PHP get a current page, PHP get current URL with query string, PHP get a domain from URL.
How to Get Current Page URL in PHP
Here you will discuss about superglobal variable $_SERVER, this is built-in PHP variable. We will use this variable get the current page URL in PHP. You can get every piece of information about the current URL using the $_SERVER superglobal.
Before we try to fetch the Current Page Full URL lets see how a normal URL structure looks like:
http://www.abc.com/dir1/test.php?glob=hello&name=world
Any typical URL like this can be broken into several common parts like:
- HTTP: The URL protocol
- www.abc.com – The domain name or the hostname.
- dir1: The directory within the root
- test.php – The actual PHP script
- glob=hello – the first URL parameter (glob) and it’s a value (hello)
- name=world – the second URL parameter (name) and its value (world)
Now we will create a program that is used to get current page url.
PHP Program for get Current Page Url
Program 1 full source Code
PHP Program to show current page URL
PHP Program show Current page full URL
Program 2 full source Code
PHP Program show Current page full URL
PHP Program show Current full URL with Query String
Program 3 full source Code
PHP Program show Current page full URL with Query String
Other Important elements of $_SERVER that are very are useful and you must know that:
- $_SERVER[‘SERVER_ADDR’]: IP address of the server
- $_SERVER[‘REQUEST_METHOD’]: Returns the page access method used i.e. ‘GET’, ‘HEAD’, ‘POST’, ‘PUT’.
- $_SERVER[‘REQUEST_TIME’]: timestamp of the start of the request.
- $_SERVER[‘HTTP_REFERER’]: returns the referrer page uri – used in redirecting to last page after login
- $_SERVER[‘SCRIPT_FILENAME’]: returns the path including the filename, like DIR
- $_SERVER[‘HTTP_COOKIE’]. returns the raw value of the ‘Cookie’ header sent by the user agent.
- $_SERVER[‘HTTP_ACCEPT_LANGUAGE’]): returns the default set language – useful for websites with multilingual content & readers
- $_SERVER[‘HTTP_USER_AGENT’]: returns the kind of device being used to access (desktop/mobile device etc) – suitable for switching interface for different devices.
Author Admin
My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.
Get url from page php
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter
Most convenient way to Extract URLs from webpage using PHP
The most User friendly definition of URL extraction is the process of fetching all the links from any website page. Technically it means extracting hrefs or src attribute from anchor tags or img tags.
The URL extraction procedure is used in many cases, for generating the Sitemap.xml file or displaying all the images of a webpage. The URL extraction PHP code is executed in PHP file at server side scripts and response is generated at browser’s front-end.
Personally I never recommend the below procedure to extract the links (URLs) because for loop is being used and is considered slower than the foreach loop. The foreach loop seems to be better in performance than the for loop. The foreach loop is executed over an array of elements and finishes the loop in less time comparatively.
Normal way of extracting urls from web-page (not recommended)
Below is the improved version of above code. Its faster and more convenient way. we can even store extracted URLs to PDF, CSV or TXT file. Let us know in comment section so we will publish new article on how to save extracted URLs to any file format.
The most convenient Example of extracting links (URLs) from any Webpage (Recommended by infoconic)
Another way for extracting links from a web-page Using PHP function getElementsByTagName() . The getElementsByTagName() PHP function is great but it doesn’t provide developers the facility to extract HTML tags inside specific HTML elements. When developers use DOMXPath() and evaluate() method, extraction of HTML tags is possible from any specific HTML elements from a web page.
PHP Get URL – How to Get the Full URL of the Current Page
In this PHP-focused article, we will explore how to get the URL of the current page in the PHP programming language.
You may want to get the current page URL for the following reasons:
- Building internal links
- Using filters with GET requests, for example, currentURL.com?myFilterParameter=Food
PHP actually stores lots of useful information as users navigate your web application. One of these is, of course, the current URL.
PHP stores these pieces of useful information in its array of super-global variables.
What are Superglobals?
Superglobals are already defined variables by the PHP engine which can be used in any kind of scope. They are readily available at any one time.
There are many of these superglobals, but the one we are interested in is the $_SERVER superglobal.
The $_SERVER Superglobal
The $_SERVER superglobal variable has many properties that are accessible with an associative style index.
Some of the values we can access include:
You can see more of these indicies in the PHP documentation here.
So, how do we get the full URL?
With the above points on superglobals and the $_SERVER superglobal in mind, we can go ahead and get the current page’s URL.
In the following screenshot, I’ve rendered a PHP application in a local environment in a page named «home.»
The URL is http://localhost/home.
In the code base of this page, I’m going to use the $_SERVER variable.
With this variable, we will have to use 2 separate indices to get each part of the current page’s URL. The first part will be the host, localhost, and the second part will be the page name, home.
The first index we will use is HTTP_HOST — The current web address host, for example localhost, or example.com
The second is REQUEST_URI which will give us the part of the URL after the host, so this is anything after localhost or example.com
$currentPageUrl = 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; echo "Current page URL " . $currentPageUrl;
Output
And that is it — pretty straightforward!
Summary
The $_SERVER superglobal variable stores a lot of vital information for modern day use-cases. As we’ve discovered in this instance, getting the current page’s URL is made simple with the ability to access this specific variable.
It’s worth checking out the documentation to see what other indices are available, though, as it’s good to have in mind how helpful this variable can be.