tags:

views:

169

answers:

5

How do I make it so that I can make a thing at the end of the address where the .php is and then tell it to do certain things. For example pull up a page like this:

sampardee.com/index.php?page=whatever

Help?

Anything else I could do with this?

A: 

I'm not quite sure what you're asking, but I think you're asking how to use GET requests.

Make GET requests against any PHP page as follows: www.mysite.com/page.php?key1=value1&key2=value2

Now, from within PHP, you'll be able to see key1 -> value1, key2 -> value2.

Access the GET hash from within PHP as follows: $myVal1 = $_GET['key1'] #resolves to "value1" $myVal2 = $_GET['key2'] #resolves to "value2"

From here, play with your GET variables as you see fit.

Stefan Kendall
A: 

The system of adding page parameters to a URL is know as HTTP GET (as distinct from HTTP POST, and some others less commonly used).

Take a look at this W3 schools page about GET in PHP and ahve a play about in getting parameters and using them in your PHP code.

Have fun!

Mark Pim
Why was this down-voted?
Skilldrick
+5  A: 

This is generally achieved with the global php array $_GET. You can use it as an associative array to 'get' whatever variable you name in the url. For example your url above:

//this gives the $page variable the value 'whatever'
$page = $_GET['page'];

if($page == 'whatever'){
//do whatever
}
elseif($page == 'somethingelse'){
//do something else
}

Check out the php documentation for more information:

$_GET documentation

and there's a tutorial here:

Tutorial using QUERY_STRING and _GET

Brett Bender
+1  A: 

A small improvement over Brett's code:

if (array_key_exists('page', $_GET) === false)
{
    $_GET['page'] = 'defaultPage';
}

$page = $_GET['page'];

// ... Brett Bender's code here
Alix Axel
+1  A: 

$_GET is usually used if you are sending the information to another page using the URL.

$_POST is usually used if you are sending the information from a form.

If you ever need to write your code so that it can accept information sent using both methods, you can use $_REQUEST. Make sure you check what information is being sent though, especially if you are using it with a database.

From your question it looks like you are using this to display different content on the page?

Perhaps you want to use something like a switch to allow only certain page names to be used?

i.e.

$pageName=$_REQUEST['page'];

switch($pageName){
case 'home':$include='home.php';break;
case 'about':$include='about.php';break;
case default:$include='error.php';break;
}
include($include);

This is a really simplified example, but unless the $page variable is either home or about, the website will display an error page.

Hope it helps!

Matt