How to use $_GET in your included files

I'm used to doing just <c:import url="/viewaccount.do?id=${account.accountId}"/> in JSP. So when I was asked to make a simple PHP script I thought the same thing was possible in PHP using the include() command. I was wrong though and thought of using session globals as a workaround. It's a good thing that I searched the internet first for a more elegant solution.

What I needed was for the $_GET["id"] variable to be visible in one of my included file. The solution? It's a basic one, really. Just create a function that accepts the id as an argument


1
2
3
4
function showAccount($accountId)
{
//some complicated stuff here...

return;
}



And suppose the function above is in a file called functions.php. Just include it your code and make a call to showAccount like this


1
showAccount($_GET["id"]);



I wish I was doing Java instead of PHP though... [SIGH]
Published 02-15-2007 4:19 AM by lamia
Filed under:

Comments

Thursday, February 15, 2007 10:52 AM by cruizer

# re: How to use $_GET in your included files

make sure you validate whatever you get from $_GET or $_POST before sending it over to a database SQL string or to your HTML output or else you will be at risk from SQL injection, XSS, HTTP response splitting and all these other web attacks.

Thursday, February 15, 2007 9:26 PM by lamia

# re: How to use $_GET in your included files

Oh yes, thanks for that! ;)