2

I do a GET request as such:

http://www.smurf.com/path/?var=val

This shows up in the URL of the user. I of course see this in nearly every major web-site.

But I was just curious if there is a way around it. It seems kind of ugly and non necessary for the non-programmer to have to look at.

If there is not something current, is there something planned to get rid of the GET request syntax after is has completed?

So that there is just

http://www.foo.com/path/

after the request.

  • 1
    If the user should not be able to see it - is it possible it's not a GET you're after in the first place? What does the request do? What is `var=val` ? – Benjamin Gruenbaum Jul 30 '13 at 21:38
  • Use the form POST method instead of GET – Drew Jul 30 '13 at 21:39
  • You can always post via AJAX. – Sablefoste Jul 30 '13 at 21:39
  • 7
    Making GET request parameters "invisible" would be against why GET requests exist in the first place. For example, you would no longer be able to link to a search result, or a page in a long list – Pekka Jul 30 '13 at 21:39
  • Google AJAX - you can do get requests with ajax as well as post – Mark Baker Jul 30 '13 at 21:39
  • The POST request must be used for it – user4035 Jul 30 '13 at 21:39
  • To all you people suggesting POST. POST is intended for completely different things than GET. For example POST is not cache-able. It has different restrictions and changes state on the server. GET is idempotent and cacheable. – Benjamin Gruenbaum Jul 30 '13 at 21:40
  • 1
    Good background reading: [What is the difference between POST and GET?](http://stackoverflow.com/q/3477333) – Pekka Jul 30 '13 at 21:40
  • This particular case is not for ajax, my thinking was, is that at some point the user does not need to see this in the URL, that is all. Seems like a pipe dream. –  Jul 30 '13 at 21:42
  • The thing is that the user very often *does* need to see it in the URL so they can link to it, for example. You can of course prettify it using mod_rewrite, but you will never be able to get rid of the values alogether. – Pekka Jul 30 '13 at 21:43
  • 1
    you can use history.pushState to change the url bar using javascript. – dandavis Jul 30 '13 at 21:44
  • could use php session variables and store your values, in combination with using POST. Do note, this has some overhead to it though, as you're storing data server side. – user2366842 Jul 30 '13 at 21:51
  • @BenjaminGruenbaum Of course we know the difference. Without using ajax, how would you submit form data without seeing all the data sent in the URL string? – Drew Jul 30 '13 at 21:53
  • from the answer below ... https://en.wikipedia.org/wiki/Clean_URL ... this is the future of urls IMO, users don't want to see the query string. Remembers users are not programmers. Smurfette is calling me...got to run. –  Sep 12 '13 at 13:32

2 Answers2

3

It seems to be that you're looking to do a POST request. POST requests work, in PHP, identically to GET requests, just use the $_POST superglobal instead of the $_GET superglobal to get POST request parameters.

In a POST request, the data is sent just like GET (with a few disregardable differences), however it is not visible in the URL, so it fits the description of what you'd like perfectly. Send data like GET, but without user seeing it in the URL.

Example HTML form:

<form method="post" action="handler.php">
    <input type="text" name="username"></input>
</form>

Then if the user submitted this form, you could get the post parameter "username" in PHP just as you would for GET.

<?php
   $username = $_POST["username"];
?>

Then use it to your liking!

Also, it'd be remiss of me not to mention the intended usages of POST/GET requests. Traditionally, POST requests should be used when data is being sent to the server for storage/usage later such as in a database or online forum. Alternatively GET requests should be used for requesting ("getting") data from the server, and not providing data that will be stored. Using requests in this way is a best practice, but often ignored as it's not enforced by the language but is just a principle.

AlienHoboken
  • 2,574
  • 17
  • 23
1

No. That's what POST requests are for. If you want to make your URLs better aesthetically, I'd recommend (depending on your server) looking for a way to have clean URLs.

In Apache, for example, you can do this in .htaccess using the mod_rewrite module. You can see a tutorial about it here.

federico-t
  • 11,157
  • 16
  • 58
  • 108