Jump to content

$_POST vs $_GET


Trium918

Recommended Posts

I have created 30 pages and counting without using

a single $_GET variable. I guess you can say that I really

donnot have an understanding of how to use and when to

use the $_GET variable. Which is true because http://php.net

doesn't help; therefore, I am asking for one of you to go into

more detail then what other websites have to offer. Thanks!'

 

Some you are probably wondering the same thing, I hope. 

 

Link to comment
Share on other sites

From:  http://us.php.net/manual/en/reserved.variables.php#reserved.variables.get

 

"An associative array of variables passed to the current script via the HTTP GET method. Automatically global in any scope."

 

Those are variables passed by URLs such as http://whatever.com/script.php?this=that&id=2, otherwise known as the HTTP GET method (as opposed to POST).

 

Thus, within script.php, $_GET['this'] = "that" and $_GET['id'] = 2.

Link to comment
Share on other sites

The $_GET method works with the URL instead of post data.

 

Lets take this URL for example:

www.example.com/index.php?user=Trium918

 

This could be useful if you had a profile for your users and you got whos profile to display from the URL. You  would get the persons name with the $_GET method like so:

<?php
$user = $_GET['user'];
echo $user; //will print "Trium918"
?>

 

You could now use this username from the URL to pull information on the user from the database.

 

<?php

$user = $_GET['user'];

$sql = mysql_query("SELECT name, address, country, age FROM users WHERE username='" . $user . "'");

if (mysql_num_rows($sql) > 0)){
  //print all the users information here
} else {
   echo "No such user";
}

?>

 

That is just one example out of many...hopefully that clears it up a bit.

Link to comment
Share on other sites

$_GET i find I use only when making pages with search queries, because it doesn't matter if the user can see that in the top browser bar. Also makes clicking the 'back'' button more friendly instead of that message when using POST pops up.

Link to comment
Share on other sites

@marf - Thats why you put this bit of code at the top of your scripts:

header("Cache-control: private");

 

If that is true (which I will be testing when I get home) you could of saved me headaches years ago with this stupid post problem. I will be running tests later tonight on it.

Link to comment
Share on other sites

Cache-control: private

Indicates that all or part of the response message is intended for a single user and MUST NOT be cached by a shared cache. This allows an origin server to state that the specified parts of the response are intended for only one user and are not a valid response for requests by other users. A private (non-shared) cache MAY cache the response.

 

Note: This usage of the word private only controls where the response may be cached, and cannot ensure the privacy of the message content.

 

Whatever that means...haha. I basically use it so that when you click back on your browser after submitting post data it doesn't give you a popup telling you about it then forces you to refresh the page and resubmit everything.

 

 

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.