- 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 request path in PHP for routing
- Get request path php
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
- Get Full URL & URL Parts In PHP (Simple Examples)
- TLDR – QUICK SLIDES
- TABLE OF CONTENTS
- URL BASICS
- THE VARIOUS URL PARTS
- GET FULL URL & URL PARTS
- 1) URL PARTS IN PHP
- 2) GETTING THE FULL URL
- 3) COMMON URL PARTS
- PROTOCOL & HOST
- PATH ONLY
- FILENAME ONLY
- PATH WITH FILENAME
- EXTRA) PARSE URL
- DOWNLOAD & NOTES
- SUPPORT
- EXAMPLE CODE DOWNLOAD
- EXTRA BITS & LINKS
- HOW ABOUT THE HASH?
- LINKS & REFERENCES
- INFOGRAPHIC CHEAT SHEET
- THE END
- Leave a Comment Cancel Reply
- Search
- Breakthrough Javascript
- Socials
- About Me
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 request path in PHP for routing
Ever been in the pain of retrieving current URL of a request with plain PHP under different root domains or local environments (with strange paths)?
php function request_path() $request_uri = explode('/', trim($_SERVER['REQUEST_URI'], '/')); $script_name = explode('/', trim($_SERVER['SCRIPT_NAME'], '/')); $parts = array_diff_assoc($request_uri, $script_name); if (empty($parts)) return '/'; > $path = implode('/', $parts); if (($position = strpos($path, '?')) !== FALSE) $path = substr($path, 0, $position); > return $path; >
Retrieves p/new from coderwall.com/p/new.
Or languages/PHP from github.com/languages/PHP.
What to use it for? Do some quick routing like this:
php $routes = [ '/' => function() // home page callback >, 'about-us' => . 'profile/edit' => . ]; $path = request_path(); if (isset($routes[$path]) AND is_callable($routes[$path])) $routes[$path](); > else // show some fancy error page with nyan cat >
RewriteEngine On # RewriteBase / RewriteCond %REQUEST_FILENAME> !-f RewriteRule ^ index.php [QSA,L]
Get request path 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
Get Full URL & URL Parts In PHP (Simple Examples)
Welcome to a quick tutorial on how to get the full URL and URL parts in PHP. Need to get the path, base, domain, or query string from the URL? In Javascript, we can pretty much get all this information with just one line of code. But sadly in PHP, things are a little… backward.
- To get the full URL in PHP – $full = (isset($_SERVER[«HTTPS»]) ? «https://» : «http://») . $_SERVER[«HTTP_HOST»] . $_SERVER[«REQUEST_URI»];
- To remove the query string from the full URL – $full = strtok($full, «?»);
That should cover the basics, but if you need more specific “URL parts” – Read on for more examples!
TLDR – QUICK SLIDES
TABLE OF CONTENTS
URL BASICS
All right, let us start with some “boring” basic URL parts. Yep, this stuff is important if you are new.
THE VARIOUS URL PARTS
- Protocol – HTTP, HTTPS, FTP, WS, WSS, and whatever else.
- Host – Better known as the “website address” to the non-technical folks.
- Port – Usually left out. Commonly understood to be 80 for HTTP, 443 for HTTPS, and 21 for FTP.
- Path – Beginners usually mistake this to be the “folder”, but it’s really not. I.E. The path can be virtual, not an actual physical folder.
- File – Yes, the physical file name.
- Query String – Extra information and parameters.
I know, it’s kind of ironic. The URL is supposed to be “easily understood” by humans, but there are so many parts to it.
GET FULL URL & URL PARTS
Now that you know the individual parts of a URL, let us now walk through how to obtain the full URL and the “common parts” using PHP.
1) URL PARTS IN PHP
2) GETTING THE FULL URL
// (A4) THE PATH, FILE NAME, AND QUERY $url .= $_SERVER["REQUEST_URI"]; // (A5) INCLUDE QUERY STRING? if ($query===false) < $url = strtok($url, "?"); >// (A6) THE FULL URL return $url; > // (B) GET CURRENT URL echo getFullURL(true); // WITH QUERY echo getFullURL(); // WITHOUT QUERY
This is pretty much the “expanded version” of the introduction snippet, packaged into a function for your convenience.
3) COMMON URL PARTS
As for the “rest of the parts” that are not included in $_SERVER , we will need to do some mix-and-match on our own. Here are a few of the common ones.
PROTOCOL & HOST
// (A) PROTOCOL + DOMAIN $host = isset($_SERVER["HTTPS"]) ? "https://" : "http://" . $_SERVER["HTTP_HOST"] ; echo $host;
PATH ONLY
// (B) PATH ONLY $path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH); echo $path;
https://site.com/ path /file.php?p=123
FILENAME ONLY
// (C) FILENAME ONLY // USE BASENAME() TO GET THE FILE + STRIP QUERY STRING $file = basename($_SERVER["REQUEST_URI"], "?". $_SERVER["QUERY_STRING"]); echo $file;
https://site.com/path/ file.php ?p=123
PATH WITH FILENAME
// (D) PATH + FILENAME $filepath = strtok($_SERVER["REQUEST_URI"], "?"); echo $filepath;
https://site.com /path/file.php ?p=123
EXTRA) PARSE URL
For you guys who have a URL string from somewhere – You can use the parse_url() function to quickly get all the parts.
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
SUPPORT
600+ free tutorials & projects on Code Boxx and still growing. I insist on not turning Code Boxx into a «paid scripts and courses» business, so every little bit of support helps.
EXAMPLE CODE DOWNLOAD
Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
EXTRA BITS & LINKS
That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.
HOW ABOUT THE HASH?
Want to get the hash section of the URL? For example, http://site.com/path/file.php #section . Sadly, it is nowhere to be found in $_SERVER . Your best bet is to use Javascript instead.
LINKS & REFERENCES
INFOGRAPHIC CHEAT SHEET
THE END
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!
Leave a Comment Cancel Reply
Search
Breakthrough Javascript
Take pictures with the webcam, voice commands, video calls, GPS, NFC. Yes, all possible with Javascript — Check out Breakthrough Javascript!
Socials
About Me
W.S. Toh is a senior web developer and SEO practitioner with over 20 years of experience. Graduated from the University of London. When not secretly being an evil tech ninja, he enjoys photography and working on DIY projects.
Code Boxx participates in the eBay Partner Network, an affiliate program designed for sites to earn commission fees by linking to ebay.com. We also participate in affiliate programs with Bluehost, ShareASale, Clickbank, and other sites. We are compensated for referring traffic.