Jump to content

variables in URL?


Guest svank

Recommended Posts

Guest svank

Can I have PHP variables in the URL for a page? ex: example.php?variable=value. I looked thorugh the PHP tutorial and couldn't find this. Thanks!EDIT: Never mind. I figured it out. Feel free to lock/delete this. :)

Link to comment
Share on other sites

If someone else would need this info you do it like this:

[url=http://subdomain.domain.toplvldomain/folder/file.php?var=value&var2=value2]http://subdomain.domain.toplvldomain/folde...lue&var2=value2[/url]

Link to comment
Share on other sites

This is for the benefit of anyone else who might want the answer to this question, I had a bit of fun when I first tried to work this out myself so figured I would give a detailed answer here:You can store strings in a url very easily, you simply add a question mark '?' and then whatever information you would like to the end of a url. The browser calls the url upto the question mark and then ignores the rest of it when calling the site, so you really can put whatever you like after it. That's it, it's that simple, you can store whatever data you like in a url in just that way.Once your browser has called the url with added string it is then available to your PHP scripts using the 'server' superglobal. Simply call the variable $_SERVER['QUERY_STRING']. This isn't entirely that useful for storing variables; it simply returns the string. However a few 'explode' statements later and you can turn your string into any type of variable you want.It's all very easy really, the real question is why would you want to do it? The main use of this form of passing a variable is to replace cookies. Cookies are great, but they don't always work - they are easily disabled by the users browser settings or other security features. This form of passing variables can't be disabled. There are of course obvious disadvantages, mainly that your urls will start to look a bit messy. Also remember that information passed in this way is no more secure than cookies (in fact its less so because any old fool can look at the screen and see the extension to your url, but most won't know how to find and look up the details of your cookies!).

Link to comment
Share on other sites

I don't think so. $_GET is a global array used to retrieve information passed to the current page by the HTML 'get' method - most commonly information entered by the user on a form (although most people would use the 'post' method and I have to admit I've never quite worked out why you would ever need to use get instead, if someone else would care to explain I'd love to hear an explanation).You should note that information passed to a page using either the post or get methods is also available to the $_REQUEST global variable, this is occassionally useful.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...