running function from a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wurdup
Forum Commoner
Posts: 39
Joined: Thu Apr 01, 2010 11:36 am

running function from a string

Post by wurdup »

I need to able to run a function from a string passed from a client but I can't get it to work. I've used eval($input) but can't use call_user_func because the parameters count may be different. I need something like:

Code: Select all


// read client input ( eg "myFunction()")
$input = socket_read($spawn, 1024);

$value = eval($input);
	
}

Any ideas?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: running function from a string

Post by John Cartwright »

call_user_func_array()

And in case you wern't paying attention, that is not the same as call_user_func()
wurdup
Forum Commoner
Posts: 39
Joined: Thu Apr 01, 2010 11:36 am

Re: running function from a string

Post by wurdup »

thanks would work but I've figured out a better solution

eval("\$output = $input;");

I had already tried this but put a \ infront of $input. Thanks anway.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: running function from a string

Post by John Cartwright »

Better solution? Ahem :roll: In fact, not only should you NEVER allow arbitrary user input to be executed as PHP code, simply put, you should just never use eval().
Post Reply