0

Is it possible to programatically change the query URL within PHP? I know I can use the $_GET['something'] function to get data from a URL query, but I don't know how to change a query string programatically in PHP.

This is an example of what I want to do:

//---- index.php ----\\    
$variable = 23;

And my URL is: http://example.com/index.php?variable=23

So if I make $variable = 10 later on in the program (not using headers, and not hard-coded into a link), I want to be able to programatically update the query URL, so that my URL will now read:

http://example.com/index.php?variable=10

Is this even possible, without using forms?

2
  • Most likely you want first solution proposed by @genesis (header()). However, if that's not what you want, explain what you're trying to accomplish. Commented Jul 16, 2011 at 19:27
  • Okay, thanks :) Yeah I think the header() method is the only possible approach I can take. Commented Jul 16, 2011 at 19:33

1 Answer 1

2
header("Location: index.php?variable=10");

or do you mean

$_GET['variable'] = 10; 

?

Sign up to request clarification or add additional context in comments.

4 Comments

Can I actually set $_GET['variable'] = 10 in PHP? Will that update the query URL? From what I know, that doesn't sound possible.
By "query URL", I mean: example.com/index.php?variable=10 Sorry for being unclear!
@Titus make sure to put exit; after header(), because with header() you're effectively "restarting" your script with different query parameters, and therefore want to finish current run.
@Titus: the superglobals (_GET/POST/REQUEST/etc...) are basically read only. You can write to those arrays, but the writes won't change the startup-state of the script, and won't affect urls you write later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.