0

I've never understood this, and on my own projects I have never needed it. But I've been contributing to WordPress a little, and they use it heavily.

Somehow they are able to redirect the user to a different page with some sort of GET variable in the URL (which is what I understand to be the advantage of using GET over POST). How do they do this? Is it as simple as making a header like header('site.com/page.php?foo=true');? This can't be useful since you have to hard code everything in (unless you want to create a string based on other variables which is kind of annoying, even still). I thought there would be a built-in function like send_get('page.php', $foo);.

I understand how to use information by using $foo = $_GET['foo'];, but I don't know how to send it with PHP.

An explanation would be appreciated - thanks!

ICoffeeConsumer
  • 882
  • 1
  • 8
  • 22
  • 4
    please give a specific example or reference of what you want an explanation of – AD7six May 28 '12 at 15:49
  • Are you asking for the benefits of using $_GET? Or what it is in general? – dockeryZ May 28 '12 at 15:54
  • What is the customary way of sending variables with GET through PHP? I know how to set up a HTML form that sends variables this way, but how can I do the same thing with PHP? I'm asking how to use it and for a general overview of it as well. – ICoffeeConsumer May 28 '12 at 15:55

4 Answers4

2

There isn't exactly a "customary" way of using it. It is one of nine superglobals. The way you use them is at your discretion. As Greg P already mentioned, they are passed through the URL.

I know how to set up a HTML form that sends variables this way, but how can I do the same thing with PHP?

If you're talking about sending GET variables with PHP solely, the answer is no. You needn't even have PHP to send a GET variable. It is as simple as adding a question mark followed by a variable name = something. Separate several of them using an ampersand (&)

Setting up a GET variable is as easy as creating an anchor link. <a href='somepage.php?getVar1=foo&anotherVariable=2&thirdVar=3

You can use PHP to dynamically place certain information in there instead of writing it manually, which is the entire purpose of the language to begin with. It is a preprocessor

So, something like this should get you started

<?php
$someID = // An id pulled from a mysql_query
echo "<a href='somepage.php?someID=" . $someID . "'>GET LINK</a>";

I thought there would be a built-in function like send_get('page.php', $foo);

Again, PHP is a preprocessor. It doesn't send information, it only outputs it. What you're talking about is Javascript.. moreover, AJAX. AJAX is the only method that will allow you to send GET variables "behind-the-scenes". And, like was mentioned in another post, jQuery has an awesome codebase for this.

dockeryZ
  • 3,981
  • 1
  • 20
  • 28
  • Perfect - that's pretty much what I was looking for. Could I do something similar without any HTML, though? By using a PHP header? – ICoffeeConsumer May 28 '12 at 16:10
  • You can do that too, but it's the same concept as what I mentioned in my post. – dockeryZ May 28 '12 at 16:12
  • The header will only redirect you, while using AJAX will do it in the background, just exactly the way you're describing – dockeryZ May 28 '12 at 16:17
0

I think you're missing the forest for the trees...the $_GET/$_POST are just variables that are passed to the page processing them - what is DONE with them and how it is done, is up to the design of the application. For example, Joomla always puts the component_id and the item_id in the $_GET array, and has been designed with that in mind, so expects them to be there, and constructs the page, or redirects, or whatever with that in mind.

In your example, a send_get() function might be a good idea (I didn't design it), but the architects didn't see it that way for one reason or another. Joomla happens to have a redirect function that does have a certain dependancy on what was passed in the $_GET, but that is only by design of the applications authors.

GDP
  • 8,109
  • 6
  • 45
  • 82
  • I just totally made up that function because I thought PHP might have something like that built in. I'd like to know how to actually send information with GET. – ICoffeeConsumer May 28 '12 at 16:00
  • Via the URL. The receiving PHP script is then able to access and process it. – Ross May 28 '12 at 16:04
  • In my day to day use, the data is sent with GET or POST via the HTML that PHP generated. If you need to do it WHILE you're in PHP generating the form, then cURL might be the answer. Or a AJAX request like .get or .post from the client browser. – GDP May 28 '12 at 16:04
  • A GET is a simply request to GET something, and PHP recognizes the particulars of that request in the $_GET variable. – GDP May 28 '12 at 16:11
0

Maybe redirect URL is kept in some of session variable. Properly $_GET variable, indicate for wordpress: "check session variable for redirect URL".

IProblemFactory
  • 9,551
  • 8
  • 50
  • 66
0

PHP.net says:

An associative array of variables passed to the current script via the URL parameters.

URL parameters: generally are variables passed to a script via the URL such as in your example site.com/page.php?foo=true. Everything after the ? is considered a paramter.

Quoted from a StackOverflow question:

The HTTP protocol defines GET type requests as being idempotent, while POST may have side effects. In pain English that means that GET is used for viewing something, without changing it, while POST is used for changing something. For example, a search page should use GET, while a form that changes your password should use POST.

Community
  • 1
  • 1
PenguinCoder
  • 4,335
  • 1
  • 26
  • 37