Jump to content

Automatically pass multiple $_GETs


mcarlie

Recommended Posts

I'm quite new to PHP coding and I'm having trouble with what I'm sure is a common beginners problem. What I need is a method that first checks if there's variable being passed through the URL and if there is then pass the new variable with an & instead of a ?.

 

I have been able to do this with a code that looks something like this:

 

if (isset($_GET['firstvar'])):
header ("Location: index.php?firstvar=1&secondvar=2")
else:
header ("Location index.php?secondvar=2");
endif;

 

The problem with this code is that I always need to adapt it to different parts of the code where "firstvar" is different.

 

So basically what I'm asking is; is there a way to check if any variable is being passed through the URL and if there is then pass a variable beginning with & at the end of the url.

 

P.S I've tried using $_SESSION['REQUEST_URI'] to get the url and pass variables at the end of that. The problem is that I have a pagination function that makes the URL look like www.mywebsite.com/index.php?page=1&page=2&page=3 etc etc.

Link to comment
https://forums.phpfreaks.com/topic/257237-automatically-pass-multiple-_gets/
Share on other sites

See this link - http_build_query

 

You can set/unset just the $_GET['variable_name'] you are interested in, then use http_build_query to build the query string that you place after the ? in the URL. This will leave all the other existing $_GET variables as is.

 

Edit: Here is a specific example showing how to use this in a pagination script that only modifies the 'page' parameter in the get parameters, but leaves a 'keyword' (search) value as is - http://www.phpfreaks.com/forums/index.php?topic=348834.msg1646676#msg1646676

 

If you are going to be using this in a header() redirect, you would use a '&' as the 3rd parameter instead. Using '&' for the third parameter is for when you output links directly in the html on a web page.

 

But what about creating an exception for the pagination?

 

What? I have no idea what that refers to.

 

http_build_query is a general purpose way of building a query string and letting you add/set/remove just the get parameter(s) that you are interested in, while leaving any/all other get parameters as is. The only time I have ever needed to add any extra logic is when you dynamically remove (unset) a get parameter and it could have been the only get parameter and you want to avoid putting a bare ? on the end of the URL (the string that http_build_query returns will be empty if there is no resulting query string built.)

 

Here is a specific example of this -

 

<?php
	unset($_GET['logout']); // remove logout parameter from the url
	$request = http_build_query($_GET, '', '&');
	$request = !empty($request) ? '?'.$request : '';
	header("Location:{$_SERVER['SCRIPT_NAME']}$request"); // redirect to the base page (without the ?logout on the URL)
	die; // prevent the remainder of the code from running while the browser performs the redirect

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.