Get Current Url Path with Query String in PHP

Get entire URL, including query string and anchor

No, I am afraid not, since the hash (the string including the #) never gets passed to the server, it is solely a behavioural property of the browser. The $_SERVER['REQUEST_URI'] variable will contain the rest however.

If you really need to know what the hash is, you will have to use the document.location.hash JavaScript property, which contains the contents of the hash (you could then insert it in a form, or send it to the server with an ajax request).

Get current URL path with query string in PHP

You want $_SERVER['REQUEST_URI']. From the docs:

'REQUEST_URI'


The URI which was given in order to access this page; for instance, '/index.html'.

Laravel Request getting current path with query string

Try to use the following:

\Request::getRequestUri()

Get Query String URL Path in PHP with $_SERVER

Unless you send your form's POST request to this url :

example.com/folder/results.phplat=38.10591&lng=-51.556916898&zipcode=32827&countryIso=US

I think your problem is that you are logging the url of the url of the file processing the form. Have you tried storing the content of this into an hidden field of your form?

<?php $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; echo $url; ?>

Get the full URL in PHP

Have a look at $_SERVER['REQUEST_URI'], i.e.

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

(Note that the double quoted string syntax is perfectly correct)

If you want to support both HTTP and HTTPS, you can use

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Editor's note: using this code has security implications. The client can set HTTP_HOST and REQUEST_URI to any arbitrary value it wants.

How can I get parameters from a URL string?

You can use the parse_url() and parse_str() for that.

$parts = parse_url($url);
parse_str($parts['query'], $query);
echo $query['email'];

If you want to get the $url dynamically with PHP, take a look at this question:

Get the full URL in PHP

Get URL query string parameters

$_SERVER['QUERY_STRING'] contains the data that you are looking for.


DOCUMENTATION

  • php.net: $_SERVER - Manual

PHP Get URL with Parameter

basename($_SERVER['REQUEST_URI']);

This will return all URLs with page name. (e.g.: index.php?id=1&name=rr&class=10).

custom get query from url function in php

Your function is a very complicated way to get query parameters. You can simply use $_GET['email'] to get the email. You should also apply a default in case it is not set.

$email = $_GET['email'] ?? false;

If you wanted to turn this into a helper function,

function getQuery($key, $default = ''): string 
{
return $_GET[$key] ?? $default;
}

$email = getQuery('email');

The reason it is being truncated if you have a 6 in the query string is because of this line. strtok will tokenize a string by a delimiter but you have provided PHP_URL_QUERY as the delimiter. That is a predefined PHP constant and has a value of 6. So strtok will split the string on 6.

strtok($_SERVER['REQUEST_URI'], PHP_URL_QUERY)

Sample Image



Yes, I can use $_GET. but, I developed a simple routing system. In
this $GET is not working. So, I had to write this getQuery() function.

That doesn't make a whole lotta sense to me, but if you want to use parse_url you can do something like this. But know that $_GET works to get everything after the ? too.

function getQuery($key, $default = ''): string
{
$parts = parse_url($_SERVER['REQUEST_URI']);
parse_str($parts['query'], $query);

return $query[$key] ?? $default;
}

$email = getQuery('email');


Related Topics



Leave a reply



Submit