1 script - multiple operations?

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
CrazyJimmy
Forum Commoner
Posts: 34
Joined: Tue Nov 19, 2002 1:40 pm

1 script - multiple operations?

Post by CrazyJimmy »

Hello,

I have a number of scripts that are not very long, and it would be much simpler if i could have these in the one script and pass some paramater to the script to choose which part I want to use. The use of multiple IF statements seems inefficient

any ideas?
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

try a switch() statement?

ex: use a POST form to send info, along with with field "action".

on the script page:

switch($_POST['action']){
case 'Alpha': do stuff;
break;
case 'Beta': other stuff;
break;
}
CrazyJimmy
Forum Commoner
Posts: 34
Joined: Tue Nov 19, 2002 1:40 pm

Post by CrazyJimmy »

Yes, that is exactly what I was looking for, but I want to do it just from a link and not a form. I can just use $_GET for this but im not sure of the security implications of it.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I'm not sure if switching is any faster than IF-ing - could be wrong. I think the key to making either optimal is to put the most frequently called case first, second next etc (even then it's probably only saving microseconds).

Using a GET var to switch / case on is fine.

Of course, anyone could edit the URL to call any function in the switch statement, at any time. Suppose one of the cases contains an admin-level function: you'd have to make sure to authenticate the user beforehand (another IF!) - but then you'd be doing that anyway, right?

Also, query string tampering could call functions in a "wrong" order: would that mess things up? Look at how your code would cope. Maybe a forum "new poll" script expects a new topic to be created first, for example

The final possibility (query string tampering which creates an undefined switch case value) would call the "default" case, if there is one (I think - never actually tested this).
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

McGruff is correct about the 'default case' scenario.

The security concern is valid also. But if you are not linking to secured pages, you should not have to worry.
Post Reply