PHP Program to show current page URL

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.

Читайте также:  Firebase realtime database android kotlin

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.
Читайте также:  Java code style xml

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.

Источник

How to retrieve URL parameters in PHP.

In this beginner PHP tutorial, we will show you how to retrieve query string parameters from a URL. We will also tell you about some of the most common pitfalls.

Take the following URL as an example, which contains two GET parameters:

In the URL above, we have two GET parameters. id, which contains the value 23, and page, which contains the value 34.

Let’s say that we want to retrieve those values so that we can use them in our PHP script.

If we want to retrieve the values of those two parameters, we can access the $_GET superglobal array like so:

//Get our two GET parameters. $id = $_GET['id']; $page = $_GET['page'];

Although the PHP code will work, it wrongly assumes that the GET parameters in question will always exist.

As a result, there is a possibility that our script will throw an ugly undefined index notice if a user removes one of the parameters from the URL.

This will result in PHP spitting out the following message:

Notice: Undefined index: id in /path/to/file.php on line 4

To guard against this kind of issue, you will need to check to see if the GET variable exists before you attempt to use it:

$id = false; if(isset($_GET[‘id’])) < $id = $_GET['id']; >$page = false; if(isset($_GET[‘page’]))

In the example above, we use PHP’s isset function to check whether or not the parameter in question actually exists.

If it does exist, we assign it to one of our variables. If it doesn’t, then our variables will retain their default FALSE values.

Never trust GET parameters. Always validate them.

GET parameters should always be treated with extreme caution.

  1. You cannot assume that they will always exist.
  2. If they do exist, you can’t discount the possibility that the user has tampered with them.

In other words, if you expect id to be an integer value and a user decides to manually change that to “blahblahblah”, your PHP script should be able to handle that scenario.

URL parameters are external variables, and external variables can never ever be trusted.

Never directly print GET parameters onto the page.

Printing out GET parameters without sanitizing them is a recipe for disaster, as it will leave your web application wide open to XSS attacks.

Take the following example:

$page = false; if(isset($_GET['page'])) < $page = $_GET['page']; >if($page !== false)< echo '

Page: ' . $page . '

'; >

Here, we’ve done everything right except the final step:

  1. We check to see if the GET parameter exists before we access its value.
  2. We do not print the page number out if it doesn’t exist.

However, we did not sanitize the variable before we printed it out. This means that an attacker could easily replace our GET variable with HTML or JavaScript and have it executed when the page is loaded.

They could then redirect other users to this “tainted” link.

To protect ourselves against XSS, we can use the PHP function htmlentities:

//Guarding against XSS if($page !== false)< echo '

Page: ' . htmlentities($page) . '

'; >

The htmlentities function will guard against XSS by converting all special characters into their relevant HTML entities. For example, will become <script>

Источник

How to Extract Query Params From a String URL in PHP?

In this post, you’re going to learn about extracting key/value params from a URL string (which is different from getting the query string as key/value pairs from the current page’s URL).

Extract Query String Part From a URL String

To extract the query string part from a string URL you can simply use the parse_url() function, for example, like so:

$url = 'https://designcise.com/?key1=value1&key2=value2'; echo parse_url($url, PHP_URL_QUERY); // output: 'key1=value1&key2=value2'

Create an Array of Query Params From String URL

To get the query string params as an array you can use the parse_str() function after extracting the query string part from the URL string. For example:

$url = 'https://designcise.com/?key1=value1&key2=value2'; $queryStr = parse_url($url, PHP_URL_QUERY); parse_str($queryStr, $queryParams); echo var_dump($queryParams); // output: [ 'key1' => 'value1', 'key2' => 'value2' ]

This would also correctly parse duplicate key names followed by square brackets (for example, ?key[]=value1&key[]=value2 ) into a numerically indexed child array belonging to that key. For example:

$url = 'https://designcise.com/?key[]=value1&key[]=value2'; $queryStr = parse_url($url, PHP_URL_QUERY); parse_str($queryStr, $queryParams); echo var_dump($queryParams); // output: [ 'key' => ['value1', 'value2'] ]

Similarly, if the squared brackets had some text between them, they would be correctly parsed into an associative array belonging to the parent key. For example:

$url = 'https://designcise.com/?key[foo]=value1&key[bar]=value2'; $queryStr = parse_url($url, PHP_URL_QUERY); parse_str($queryStr, $queryParams); echo var_dump($queryParams); // output: [ 'key' => ['foo' => 'value1', 'bar' => 'value2'] ]

Hope you found this post useful. It was published 07 May, 2021 . Please show your love and support by sharing this post.

Источник

How to Get Parameters from a URL String with PHP

Almost all developers at least once in their practice have wondered how to get parameters from a URL string with the help of PHP functions.

In our tutorial, we will provide you with two simple and handy functions such as pase_url() and parse_str() that will allow you to do that.

Read on and check out the options below.

Before starting, note that the parameters and the URL are separated by the ? character.

In case you want to get the current URI you can use $url = $_SERVER[REQUEST_URI];

Using the parse_url() and the parse_str Functions

This function is aimed at returning the URL components by parsing the URL.

So, it parses the URL, returning an associative array that encompasses different components.

The syntax of the parse_url() function is as follows:

parse_url($url, $component = -1);

With the help of the parse_str() function, you can parse query strings into variables.

The syntax of this function is like so:

Now, let’s see examples of how to use these two functions for getting the parameters from the URL string.

 // Initialize URL to the variable $url = 'http://w3docs.com?name=John'; // Use parse_url() function to parse the URL // and return an associative array which // contains its various components $url_components = parse_url($url); // Use parse_str() function to parse the // string passed via URL parse_str($url_components['query'], $params); // Display result echo ' Hi ' . $params['name']; ?>

The output of this example will be:

Now, let’s consider another example:

 // Initialize URL to the variable $url = 'http://w3docs.com/register?name=Andy&[email protected]'; // Use parse_url() function to parse the URL // and return an associative array which // contains its various components $url_components = parse_url($url); // Use parse_str() function to parse the // string passed via URL parse_str($url_components['query'], $params); // Display result echo ' Hi ' . $params['name'] . ' your emailID is ' . $params['email']; ?>

And, the output of this example will look like this:

So, in this tutorial, we highlighted the two handy functions that will allow getting parameters from a URL string with PHP.

Hope, this tutorial helped to achieve your goals.

Источник

Оцените статью