Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

johnwaweru's avatar

Get query strings from previous URL

Is there a way to get a list of query strings from previous URL?

0 likes
3 replies
MThomas's avatar
MThomas
Best Answer
Level 35

To get the referer URL you can use: $_SERVER['HTTP_REFERER'], or the Laravel way: URL::previous();.

And you can use parse_url() to grab the query string:

$parsedUrl = parse_url(URL::previous());

$parsedUrl['post']; // www.example.com
$parsedUrl['path']; // /posts
$parsedUrl['query']; // param=val&param2=val

And you can get an array of params $output with value using parse_str().

parse_str($parsedUrl['query'], $output);
johnwaweru's avatar

Thanks. This works. I can then run explode() on the query part to get the individual components.

Edit: parse_str() is much better. Thanks again.

Please or to participate in this conversation.