.
Tutorial
Avatar

PHP basic lesson 6

By Remco on 2008-12-02
Views: 898

Introduction

This tutorial will teach you how to use the GET.
The GET functon of PHP does the same as the POST function, BUT the GET variables will put all the value's in the URL.
So it will not be save to transfer data ( think about passwords ).
So that's why we will not show you how to use get forms,
but we will use GET in a different way, please go to step 1 to read more.

Using GET variables

As you read above, using GET in a form is usually a real stupid thing to do.
But we are going to teach you the useful side of the GET function.

See this link: http://www.scripterplace.com/website/?page=register

At the end of the link it says: ?page=register
This is not a file like index.php ... it's a variable, a GET variable.
You can create as many as you like and as weird as you like...

Information

Just like the POST array you will create a GET array, and you can use these value's in your code.
For example:
The link we are going to use is: http://localhost/?page=home



Code:

<?php

$page 
$_GET['page']; // In this case $_GET['page'] = home

/*
If the link would have been: http://localhost/?page= , $_GET['page'] would be empty.

Now we have got the value we can do stuff with it.
*/

if($page == "home")
{
    echo 
"This is the home page";
}
elseif(
$page == "news")
{
    echo 
"This is the news page";
}
elseif(
$page == "404")
{
    echo 
"I'm sorry this page does not exists";
}
else
{
    
header("Location: http://localhost/?page=404"); 
    
// sends user to specified URL.
}

?>



Is it save ?

Yes it is save to use, but not if you are going to use these variables to create folders, execute queries etc.
Be sure to make your code as save as possible, use PHP functions to filter or check things so you can catch people trying to abuse your code.

I will give you a very basic PHP function that will clear variables.
You paste this code in every document you use, or you will use an include.
Includes will be used in the next tutorial.



Code:

<?php

function filter_string($string)
{
    
$string mysql_real_escape_string($string); 
        
// makes it save to use variable in a query.
    
$string strip_tags($string); 
        
// strips all tags used so no scripts will be bothering you.
    
$string htmlentities($stringENT_QUOTES); 
        
// turns all your html codes and characters into HTML entities
        // so you can use your variable in a query
    
    
return $string;

}

//You can use the function on the string like this:
$variable filter_string($_GET['page']);

?>


By using these simple functions you are able to filter your variables and you can use them in queries and such.

Using more than 1 GET variable

If you want to use more than one GET variable, for example:
http://localhost/?page=home&news_id=2

You can catch this by using the && opperator.



Code:

<?php

$page 
$_GET['page'];
$newsid $_GET['news_id'];

if(
$page == "home" && $newsid == "2")
{
    echo 
"Ths is the home page, and i will display news message two here !!";
}

?>





If there are any questions i would like to hear them.

You now know how the GET function in PHP works, by using this method you are able to make dynamic pages.
The next tutorial will be about includes and functions.
« Previous Next »




Comments

Flamer - 2008-12-02
Hi there! I hope you are going to ceate a tut about using PHP SESSIONS? Sessions are a saver way to transfer information. Also a reason why i use the REQUEST.

You have to be logged in to write a comment.

.