Re: Call a Function Held in a $_GET Var

From: Date: Wed, 06 Apr 2005 13:04:19 +0000
Subject: Re: Call a Function Held in a $_GET Var
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-212412@lists.php.net to get a copy of this message
Shaun wrote:
Hi, If I have a function name held in a $_GET variable for example ...&func=print_user_list how can I call the function dynamically? ie. $_GET['func']() Thanks for your help Well, call_user_func() [ http://www.php.net/call_user_func ] will do the job. But that is really bad, because you can get hacked really easy. Here's a small example
www.url.com/file.php?func=evilfunction So that is _bad_ coding and not a good practise at all, but if that is the _only_ option be more than sure that you do some checking first. An example follows : $allowed = array('first_func', 'second_func', 'third_func') { if(in_array($_GET['func'], $allowed) { /* it's okay */ call_user_func($_GET['func']); } else { die('Next time, sucker'; } } Hope this helps, -- Josip Dzolonga http://josip.dotgeek.org jdzolonga[at]gmail.com

« previous php.general (#212412) next »