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
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.

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.