11, October 2008

$_GET, isset(), and switch. - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: $_GET, isset(), and switch.
« previous next »
Pages: [1] Print

Author Topic: $_GET, isset(), and switch.  (Read 1044 times)
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« on: May 07, 2007, 12:42:33 AM »

i have a very simple question, but am not sure where to even go in the right direction with this! here it is!

which method for php page management (for lack of a better phrase?) do you think is the safest/fastest, etc... which is the overall BEST method! see the code snippets below for examples, and please reply telling me which of the 3 is the best in your honest opinion.
In this example, we will only be using numbers as the variable that is changing from page to page. the function "isNum" will not be shown in this example. For more information on function.isNum, please see my tutorial, "Better ways to handle submitted data.."

Direct $_GET Method
Code:
<?php
$id 
isNum($_GET['id']);
if (
$_GET['id']=='1'){
include(
'filename1.php'); 
} elseif (
$_GET['id']=='2'){
include(
'filename2.php'); 
} else {
include(
'default.php'); 
}
?>


function.isset() Method
Code:
<?php
if (isset($id)){
include(
'filename.php?id='.$id);
} else {
include(
'default.php'); 
}
?>



Switch Method (using $_GET as well though)
Code:
<?php 

$id 
isNum($_GET['id']);
switch(
$id) { default: include('filename.php'); 
break; case 
"1": include('filename1.php'); 
break; case 
"2": include('filename2.php'); 
break; case 
"3": include('filename3.php'); 
break; case 
"4": include('filename4.php'); 
break; case 
"5": include('filename5.php'); 
break; case 
"6": include('filename6.php'); 
break; case 
"7": include('filename7.php'); 
?>


Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8116
41653 credits
Members referred : 3



« Reply #1 on: May 07, 2007, 01:30:57 AM »

I guess the isNum($id) is the (int)$id Wink

The answer to your question depends. If you have 2 or 3 or 5 possible ways to go, then an if statement is fine. Otherwise a switch() statement looks better.

In anyway those are very simple functions of php so I guess there is no big difference anyway.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #2 on: May 07, 2007, 05:41:05 AM »

I guess the isNum($id) is the (int)$id Wink

The answer to your question depends. If you have 2 or 3 or 5 possible ways to go, then an if statement is fine. Otherwise a switch() statement looks better.

In anyway those are very simple functions of php so I guess there is no big difference anyway.

so $_GET and isset are virtually the same? using either $_GET or isset to call the variable does not compromise security of the script in anyway?

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
Cyberpunk Wannabe
*
Gender: Male
Posts: 43
280 credits
Members referred : 0


Lolo means Grandpa in the Philippines!


« Reply #3 on: May 07, 2007, 12:15:46 PM »


The answer to your question depends. If you have 2 or 3 or 5 possible ways to go, then an if statement is fine. Otherwise a switch() statement looks better.

Switch Method (using $_GET as well though)
Code:
<?php 

$id 
isNum($_GET['id']);
switch(
$id) { default: include('filename.php'); 
break; case 
"1": include('filename1.php'); 
break; case 
"2": include('filename2.php'); 
break; case 
"3": include('filename3.php'); 
break; case 
"4": include('filename4.php'); 
break; case 
"5": include('filename5.php'); 
break; case 
"6": include('filename6.php'); 
break; case 
"7": include('filename7.php'); 
?>


Hey Nikolas!

You made me laugh!  "looks better" is a can of worms (to a chicken or a fish - that's a good thing!!).

I had something like this with 27 cases once...
Code:
<?php 
switch($id) { 
     case "1": include('filename1.php'); break;
     case "2": include('filename2.php'); break;
     case "3": include('filename3.php'); break;
     case "4": include('filename4.php'); break;
     case "5": include('filename5.php'); break;
     case "6": include('filename6.php'); break;
     case "7": include('filename7.php'); break;
     default: include('filename.php');
}
?>

I obvously think the break "looks better" after the test and the default "looks better" after all else has failed (and I am sure many will disagree!).

My 27 cases were slowly pared down to just...
Code:
<?php 
switch($id) { 
     case "1": include('filename1.php'); break;
     default: include('filename.php');
}
?>

Was that "wrong"?  God forbid, that is still perfect code!

In fact I have a student that generally likes statements like...
Code:
<?php switch($id) { case "1": include('filename1.php'); } ?>
To him it makes more sense than...
Code:
<?php if ($id "1") { include('filename1.php') ; } ; ?>
And that is what made me laugh, because in PHP there are a bunch of ways to make things "look better" (to a bunch of different people!)  Cool
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8116
41653 credits
Members referred : 3



« Reply #4 on: May 07, 2007, 01:47:19 PM »

Hey Lan I think a teacher should not laugh in other people's opinions or english anyway Wink

The best way to do this if you are going to use a statement to include a module in my honest opinion is something like:

Code:
<?php
$module 
'/path/to/modules/'.$_GET['id'].'.php';
if ( 
file_exists$module ) )
 include 
$module;
else
 die(
'unknown module');

?>


Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Cyberpunk Wannabe
*
Gender: Male
Posts: 43
280 credits
Members referred : 0


Lolo means Grandpa in the Philippines!


« Reply #5 on: May 07, 2007, 02:34:35 PM »

Hey Lan I think a teacher should not laugh in other people's opinions or english anyway Wink

The best way to do this if you are going to use a statement to include a module in my honest opinion is something like:

Code:
<?php
$module 
'/path/to/modules/'.$_GET['id'].'.php';
if ( 
file_exists$module ) )
 include 
$module;
else
 die(
'unknown module');

?>


Hi Nikolas,

Oops, Hope you didn't think I was laughing at you.  No Offence, huh?

I agreed with your point, just laughed at the comcept of 2 or 3 or 5 if statements!  A student asked me not long ago, "Where is that line?" Is it 2, or 3, or 5?  The truth is that it is the best way for the programer (unless your are talking about functionality or security.

Far be it for me to laugh about english either.  I live in the Philippines and we say things like "The other, other day!"  You can read that as "two days before yesterday!"

The module idea is a great idea... but it does add complexity that may not be needed for a very simple statment!  Although, I find myself writing more and more "modules" and stringing them together as needed. It makes editing features very easy because only one little piece of code is all you are dealing with.

Oh, by the way, I am NOT a teacher!  I have students because I teach!  I retired from programing fortran and colbol and PL-1 years ago (eight grand children so far!).  I "teach" poor people who cannot go to school.  I do have a class I help with (club advisor / server admin) at a college here, but my students are 21 "kids about 20yrs" that are learning XHTML, PHP and MySQL on 8 workstations I set up in a spare part of my house.  So far, I have helped a few dozen people get jobs (seven in other countries) that feed their families.  This schools is free because the students cannot afford to pay.  A great way to retire!

But this "teacher" is still learning!  I have my "young" daughter in the University IT program and she comes home and teaches her papa!

I like your ideas Nikolas, they are very practical and helpful and I was not laughing at you... more laughing with you about the fact that the "right way" may be a matter of opinion, yours, mine or anyones!

Have a great day and keep up the good work, I love the site.  In fact I may even copy some of it for a forum in the Tagalog and Cebuano languages! (the 2 largest language groups of the 117 languages of the Philippines).  Fantastic Job!

Yo Meth0d...
I like the module method Nikolas proposed also.  Because if you make an error (maybe you don't do that!) the program will still give you a warning with the

Code:
<?php
else
 die(
'unknown module');
?>

Although I would prefer...
Code:
<?php
else
 exit(
'unknown module');
?>
Because of how "die" is preceived in the Philippines!  Grin
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8116
41653 credits
Members referred : 3



« Reply #6 on: May 07, 2007, 02:40:01 PM »

I am not a man that got offended easily anyway, so no hard feelings Wink

The method I propose is good because it will help you create some kind of framework. I mean this way you wont need to put a line of code to your index file when you add or remove modules.

But I have to say that it is VERY IMPORTANT to use :

$module '/path/to/modules/'.$_GET['id'].'.php';

Because if you use something like :

$module $_GET['id'].'.php';

you will have security problems Wink

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Cyberpunk Wannabe
*
Gender: Male
Posts: 43
280 credits
Members referred : 0


Lolo means Grandpa in the Philippines!


« Reply #7 on: May 07, 2007, 03:00:17 PM »

But I have to say that it is VERY IMPORTANT to use :

$module '/path/to/modules/'.$_GET['id'].'.php';

Because if you use something like :

$module $_GET['id'].'.php';

you will have security problems Wink


Very good security point!  In fact it may be best to even do some additional stuff.  My basic belief is that get and post statements, as well as address lines or anything else the user can pass to your server are really close to viruses and need to be treated with a high degree of disbelief that they contain any valid information!  Better to be safe than sorry!
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #8 on: May 07, 2007, 08:20:24 PM »

Code:
<?php
$module 
'/path/to/modules/'.$_GET['id'].'.php';
if ( 
file_exists$module ) )
 include 
$module;
else
 die(
'unknown module');

?>


Nik that is exactly what I am looking for! thanks mate!

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=6527
Tags : php Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: $_GET, isset(), and switch.
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Oct 11, 2008, 01:45:45 PM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 36.904
Total Topics: 7.557
Total Members: 4.148
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: ljay

17 Guests, 2 Users online :

14 users online today:



Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.