Get string parameter php

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.

Источник

Get Query String from URL in php

To get the query string from a URL in php, you can use $_GET super global to get specific key value pairs or $_SERVER super global to get the entire string.

// http://theprogrammingexpert.com/?key1=value1&key2=value2&key3=value3 echo $_GET['key1']; echo $_GET['key2']; echo $_SERVER['QUERY_STRING']; //Output: value1 value2 key1=value1&key2=value2&key3=value3

A query string is a part of a URL that assigns values to specified parameters. When building web pages, query strings help us pass and access information to be used on our web pages.

When working in php, the ability to easily access and work with query strings is important.

To get a query string from a URL, there are a few different ways you can get the key value pairs.

To get a specific parameter from a query string, you can use $_GET super global to access individual parameters from the query string by name.

// http://theprogrammingexpert.com/?key1=value1&key2=value2&key3=value3 echo $_GET['key1']; //Output: value1

If you want to get the entire query string, you can use the $_SERVER super global.

// http://theprogrammingexpert.com/?key1=value1&key2=value2&key3=value3 echo $_SERVER['QUERY_STRING']; //Output: key1=value1&key2=value2&key3=value3

After getting the entire query string, you can parse it for the key value pairs with the php parse_str() function.

Using $_GET to Get Query String Parameters in php

The $_GET super global variable allows us to get individual key value pairs from query strings.

There are a few additional cases which occur when using query strings that I want to share with you.

First, in a query string, you can have duplicate key names followed by square brackets. This will create a child array in $_GET for that key with numerically indexed values.

// http://theprogrammingexpert.com/?key[]=value1&key[]=value2&key[]=value3 echo $_GET['key'][0]; echo $_GET['key'][1]; echo $_GET['key'][2]; //Output: value1 value2 value3

If the brackets are not empty, and have keys in them, then for a particular key, the child array belonging to the key will be an associative array.

// http://theprogrammingexpert.com/?key[ex1]=value1&key[ex2]=value2&key[ex3]=value3 echo $_GET['key']['ex1']; echo $_GET['key']['ex2']; echo $_GET['key']['ex3']; //Output: value1 value2 value3

Using $_SERVER to Get Query String in php

You can also use the $_SERVER super global variable to get the entire query string. After getting the query string, you can parse it with the php parse_str() function.

// http://theprogrammingexpert.com/?key1=value1&key2=value2&key3=value3 query_string = $_SERVER['QUERY_STRING']; parse_str(query_string); echo $key1; echo $key2; echo $key3; //Output: value1 value2 value3

Hopefully this article has been useful for you to learn how to get query strings and work with them in php.

  • 1. php e – Using M_E and exp() Function to Get Euler’s Constant e
  • 2. php range() – Create Array of Elements Within Given Range
  • 3. php is_float() Function – Check if Variable is Float in php
  • 4. php array_key_exists() Function – Check if Key Exists in Array
  • 5. In PHP isset Method is Used For Checking if Variable is Defined
  • 6. php Square Root with sqrt Function
  • 7. php session_destroy() – Delete Session File Where Session Data is Stored
  • 8. php array_walk() – Modify Elements in Array with Callback Function
  • 9. Capitalize First Letter of String with php ucfirst() Function
  • 10. preg_replace php – Use Regex to Search and Replace

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Источник

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>

Источник

PHP: Get the full query string.

This is a tutorial on how to get the FULL query string as a string using PHP.

Most people are aware of how to retrieve URL parameters using the $_GET array. However, what if you wanted to retrieve these parameters as a string?

Let’s say, for example, that we have the following URL:

test.com/file.php?id=299&mobile=Y&clid=392829

As you can see, the query string in the URL above contains three GET parameters.

If we want to retrieve everything after the question mark and assign it to a string, we can simply access the QUERY_STRING element in the $_SERVER superglobal array like so:

//Get the full string $queryString = $_SERVER['QUERY_STRING']; var_dump($queryString);

If we were to run our code snippet above on the URL in question, it would return the following string:

Note how this string does not contain the question mark symbol. If this symbol is needed, then you will need to re-add it yourself.

What if there is no query string?

If there is no query string, then the QUERY_STRING key in $_SERVER will be an empty string.

Unlike other elements in the $_SERVER array, QUERY_STRING should always exist.

Why is this useful?

This can be useful for a number of reasons.

The first two that spring to mind are:

QUERY_STRING and XSS.

You should never print the QUERY_STRING variable out onto the page without filtering it first.

If you do this, you will leave yourself open to the possibility of a Cross Site Scripting (XSS) attack.

The code above is vulnerable to XSS because the QUERY_STRING result is being printed out without any sort of filtering. As a result, malicious users could potentially put JavaScript code into the query string and have it executed on your page.

To be safe, you should wrap it in the htmlentities function like so:

Hopefully, you found this guide useful!

Источник

PHP: Get the full query string.

This is a tutorial on how to get the FULL query string as a string using PHP.

Most people are aware of how to retrieve URL parameters using the $_GET array. However, what if you wanted to retrieve these parameters as a string?

Let’s say, for example, that we have the following URL:

test.com/file.php?id=299&mobile=Y&clid=392829

As you can see, the query string in the URL above contains three GET parameters.

If we want to retrieve everything after the question mark and assign it to a string, we can simply access the QUERY_STRING element in the $_SERVER superglobal array like so:

//Get the full string $queryString = $_SERVER['QUERY_STRING']; var_dump($queryString);

If we were to run our code snippet above on the URL in question, it would return the following string:

Note how this string does not contain the question mark symbol. If this symbol is needed, then you will need to re-add it yourself.

What if there is no query string?

If there is no query string, then the QUERY_STRING key in $_SERVER will be an empty string.

Unlike other elements in the $_SERVER array, QUERY_STRING should always exist.

Why is this useful?

This can be useful for a number of reasons.

The first two that spring to mind are:

QUERY_STRING and XSS.

You should never print the QUERY_STRING variable out onto the page without filtering it first.

If you do this, you will leave yourself open to the possibility of a Cross Site Scripting (XSS) attack.

The code above is vulnerable to XSS because the QUERY_STRING result is being printed out without any sort of filtering. As a result, malicious users could potentially put JavaScript code into the query string and have it executed on your page.

To be safe, you should wrap it in the htmlentities function like so:

Hopefully, you found this guide useful!

Источник

Читайте также:  METANIT.COM
Оцените статью