Page 1 of 1

1 script - multiple operations?

Posted: Fri Mar 28, 2003 11:01 am
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?

Posted: Fri Mar 28, 2003 11:07 am
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;
}

Posted: Fri Mar 28, 2003 3:49 pm
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.

Posted: Fri Mar 28, 2003 7:41 pm
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).

Posted: Mon Mar 31, 2003 9:09 am
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.