tags:

views:

180

answers:

7

I know the title isn't very clear. I'm new to PHP, so there might be name for this kind of thing, I'll try to explain as best as I can. Sometimes in a URL, when using PHP, there will be a question mark, followed by data. I'm sorry, I know this is very noobish, but I'm not sure what it's called to look for a tutorial or anything. Here is what I mean:

http://www.website.com/error%5Fmessages.php?error%5Fid=0

How do you configure it to display different text depending on what the number is (in this example it's a number)

Could somebody please tell me what this is called and how I could do this? I've been working with PHP for a couple days and I'm lost. Thank you so very much for understanding that I am very new at this.

A: 

its called "query string"

and you can retrieve it via $_SERVER["QUERY_STRING"]

or you can loop through $_GET

in this case the error_id, you can check it by something like this

echo $_GET['error_id'];
Wbdvlpr
+1  A: 

It's called the query string.

In PHP you can access its data via the superglobal $_GET

For example:

http://www.example.com/?hello=world

<?php

// Use htmlspecialchars to prevent cross-site scripting attacks (XSS)
echo htmlspecialchars($_GET['hello']);

?>

If you want to create a query string to append to a URL you can use http_build_query():

$str = http_build_query(array('hello' => 'world'));
Greg
+5  A: 

That "data" is the URL querystring, and it encodes the GET variables of that HTTP request.

Here's more info on query strings: http://en.wikipedia.org/wiki/Query_string

In PHP you access these with the $_GET "super-global" variable:

// http://www.website.com/error%5Fmessages.php?error%5Fid=0
// %5F is a urlencoded '_' character, which your webserver will most likely
// decode before it gets to PHP.
// So ?error%5Fid=0 reaches PHP as the 'error_id' GET variable
$error_id = $_GET['error_id'];
echo $error_id; // this will be 0

The querystring can encode multiple GET variables by separating them with the & character. For example:

?error_id=0&error_message=Something%20bad%20happened

  • error_id => "0"
  • error_message => "Something bad happened"

In that example you can also see that spaces are encoded as %20.

Here's more info on "percent encoding": http://en.wikipedia.org/wiki/Percent-encoding

James Wheare
Thank you, I'm looking into more information about querystrings. Thanks again.
Nate Shoffner
A: 

The term you are looking for is GET. So in php you need to access the GET variables in $_GET['variable_name'], e.g. in the example you gave $_GET['error_id'] will contain the value 0. You can then use this in your logic to echo back different information.

DaveJohnston
+2  A: 

The data after the question mark is called the "query string". It usually contains data in the following format:

param1=value1&param2=value2

Ie, it is a list of key-value pairs, each pair separated with the ampersand character (&). In order to pass special characters in the values, they have to be encoded using URL-encoding format: Using the percent sign (%) followed by two hexadecimal characters representing the character code.

In PHP, parameters passed via the query string are automatically propagated to your script using the super-global variable $_GET:

echo $_GET['param1']; // will produce "value1" for the example above.

The raw, unprocessed query string can be retrieved by the QUERY_STRING server variable:

echo $_SERVER['QUERY_STRING'];
Ferdinand Beyer
A: 

The bit after the question mark is called a Query String. The format is typically, although not necessarily always, key-value pairs, where the pairs are separated by an ampersand (&) and the value is separated from the name by an equals sign (=): ?var1=value1&var2=value2&.... Most web programming environments provide an easy way to access name-value pairs in this format. For example, in PHP, there is a superglobal, which is an associative array of these key-value-pairs. In your example, error_id would be accessible via:

$_GET['error_id']

The reason for the name "GET" is that query string variables are typically associated with a HTTP GET request. POST requests can contain GET variables too, whereas GET requests can't contain POST variables.

As to the rest of your question, you could approach the text issue in a number of ways, the simplest being switching on the error id:

$error_id = isset($_GET['error_id']) ? $_GET['error_id'] : 0;
switch($error_id) {
    case 1:
        echo "Error 1";
        break;
    default: 
        echo "Unknown Error";
        break;
}

and more complex ways involve looking up the error message from a file, database or what have you.

Rytmis
+1  A: 

As previously described, the data after the ? is the querystring (or GET data), and is accessed using the $_GET variable. The $_GET variable is an array containing the name=value pairs in the querystring.

Here is a breif description of $_GET and an example of it's usage:

http://www.w3schools.com/php/php_get.asp

Data can also be submited to a PHP script as POST data (found in the $_POST variable), which is used for passwords, etc, and is not stored in the URL. The $_REQUEST variable contains both POST and GET data. POST and GET data usually originates from being entered into a web form by a user (but GET data can also come directly from a link to an address, like in your example). More info about using web forms in PHP can be found here:

http://www.w3schools.com/php/php_forms.asp