[SOLVED] alternative to php switch case for include?

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

User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

[SOLVED] alternative to php switch case for include?

Post by bironeb »

I need some ideas for an alternative to switch case for include statments. Basicly I have a site that gets about 15 to 25 visitors a day, and I have a header, Side Menu, and footer that remain Static, while the body dynamic changes when a link is clicked on the side menu. The body changes by passing a value to a string that is sent through a switch case statement to produce a different include statement for the body:

Example:

<a href="index.php?mlink=dbq"> Configuration Error: DBQ</b></a>

<?php
switch($mlink)
{
case"dbq":
include('\includes\dbq.php');
break;
default:
//when nothing is true do this
include('\includes\new.php');
}
?>

The problem is this switch case has become very large, and is slowing down the site.

Any Suggestions?
Last edited by bironeb on Mon Nov 24, 2003 10:46 am, edited 1 time in total.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

If it's all the links have the same name as the file, you can go along:

Code: Select all

<?php
$link = $_GET['mlink'];
$str = "\\includes" . $link . ".php";
include($str);
?>
-Nay
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

Post by bironeb »

didn't work, what did i forget?
Last edited by bironeb on Thu Nov 20, 2003 12:55 pm, edited 1 time in total.
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

getting error

Post by bironeb »

I thought it was working but i guess not. I replaced my switch case with

<?php
$link = $_GET['mlink'];
$str = "\\includes\\" . $link . ".php";
include($str);
?>

and im gettting


Warning: Failed opening '\includes\.php' for inclusion (include_path='.; \apache\includes;\apache\htdocs\;\apache\htdocs\phpmyadmin') in c:\apache\htdocs\includes\case.php on line 4


did i forget something?
User avatar
Saethyr
Forum Contributor
Posts: 182
Joined: Thu Sep 25, 2003 9:21 am
Location: Wichita, Kansas USA
Contact:

Post by Saethyr »

try this

Code: Select all

<?php
if (isset($_GET['mlink']))
{
$link = $_GET['mlink']; 
}
else
{
$link = "new";
$str = "\\includes" . $link . ".php"; 
include($str); 
}
?>
I believe isset is the correct code
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

Post by bironeb »

Well with that code I'm not getting any errors, but I am getting the new.php file to include no matter what value is passed. So every link loads new.php in the body.

I think we are getting somewhere tho... Any other ideas?
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Code: Select all

<?php
if (isset($_HTTP_GET_VARS['mlink'])) 
{ 
$link = $__HTTP_GET_VARS['mlink']; 
} 
else 
{ 
$link = "new"; 
$str = "\\includes" . $link . ".php"; 
include($str); 
} 
?>
try this ^^^
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

whats wrong with

Code: Select all

include ('$link.php');
and make the URL http://www.site.com/page.php?link=home

(would load home.php)

o_O
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

LiLpunkSkateR wrote:whats wrong with

Code: Select all

include ('$link.php');
and make the URL http://www.site.com/page.php?link=home

(would load home.php)

o_O
$link has to be imported from GET :P
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Wait a minute! Hold your horses. Be careful about some of the above code.

A better solution, for security reasons, is the following:

Code: Select all

<?php

include 'includes/'.basename($_GET['view'], '.php');

?>
[php_man]basename[/php_man] pretty much protects against someone doing something like this:

Code: Select all

url.php?view=../config.inc
And basically, searching through your site for various files. by setting the base directory in the include as 'include', and using basename(), this offers up the protection needed to use a system like this.

Remember: ALWAYS VALIDATE EXTERNAL INPUT. If you didn't create it, it's not safe!
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

Post by bironeb »

Well first I tried using Qads:

Code: Select all

<?php 
if (isset($_HTTP_GET_VARS['mlink'])) 
{ 
$link = $__HTTP_GET_VARS['mlink']; 
} 
else 
{ 
$link = "new"; 
$str = "\\includes" . $link . ".php"; 
include($str); 
} 
?>
And the same thing happens as before, no matter what value is passed it always runs the else and loades new.php ...

Then i tried

Code: Select all

<?php 

include 'includes/'.basename($_GET['view'], '.php'); 


?>
and now i get this error

Warning: Wrong parameter count for basename() in c:\apache\htdocs\includes\case.php on line 3

Warning: Failed opening 'includes/' for inclusion (include_path='.; \apache\includes;\apache\htdocs\;\apache\htdocs\phpmyadmin') in c:\apache\htdocs\includes\case.php on line 3


i might be forgetting to change one of the values to mach part of my code for the above suggestion, i'm still pretty new with php, any other suggestions? i really apreciate your help.. I think we are getting close.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Well, it depends, is the variable name view? Or are you using something else? In your previous example (which won't work, btw), you use mlink, so maybe you should use $_GET['mlink'] instead.
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

Post by bironeb »

Maybe I should provide more examples of the code:

Here is my index.php

Code: Select all

<html>
<head>
<title>Technical Support FAQ</title>
</head>
<body BGCOLOR="#FFFFFF" TEXT="#000000"
      LINK="#3300FF" VLINK="#3300FF" ALINK="#3300FF"
      topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0">

<?php include('\includes\header.php'); ?>
<b><a href="index.php?mlink=cca"  
	style="text-decoration:none">Click Here</a>&nbsp;if you did not 
	find your problem and solution, or to add corrections, or comments.</b>

<table border=2 width="1100" bordercolor="black"
	cellSpacing="0" cellPadding="0">
<tr>
	<td width="325" bgcolor="FFFFCC">
	  <?php include('includes\menu.php'); ?>		
	</td>
	<td width="775" bgcolor="CCFFFF" valign="top">
	  <?php include('\includes\case.php'); ?>	
	</td>
</tr>
</table>
<?php include('\includes\footer.php'); ?>
</font>
</body>
</html>
Here is my menu.php:

Code: Select all

<html>
<head>
<title>LEFT MENU</title>
</head>
<body>
<p><b><a href="index.php?mlink=new"  
	style="text-decoration:none">Recently Found Errors</b></a></p>
<!-------------------ERRORS WHEN OPENING CLOSING-------------------------------->
<p><b>1</b>.<b>Errors When Opening Closing</b></a></p>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>a. Run-Time Errors</b>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>i.<a href="index.php?mlink=runtime430"  
	style="text-decoration:none">430</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>ii.<a href="index.php?mlink=runtime"  
	style="text-decoration:none">-2147221231 or similar</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>iii.<a href="index.php?mlink=dtpassrt"  
	style="text-decoration:none">-2147217904 & 440</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>b.<a href="index.php?mlink=upgrade" 
style="text-decoration:none">Upgrade DTDBEvolveDLL Error</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>c.<a href="index.php?mlink=dbq"  
	style="text-decoration:none">Configuration Error: DBQ</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>d.<a href="index.php?mlink=apperror"  
	style="text-decoration:none">Closing.exe Application Error</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>e.<a href="index.php?mlink=dtpassword"  
	style="text-decoration:none">DTPassword Error -2147467259</b></a>
<!-------------------------PRINT PROBLEMS------------------------------>
<p><b>2</b>.<b>PRINT PROBLEMS</b></a></p>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>a.<a href="index.php?mlink=print52or6"  
	style="text-decoration:none">Error 6, 9, 51, 52 or 91</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>b.<a href="index.php?mlink=printline"  
	style="text-decoration:none">Data Running off the page</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>c.<a href="index.php?mlink=printinvestor"  
	style="text-decoration:none">Not printing from one specific Investor</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>d.<a href="index.php?mlink=printdocset"  
	style="text-decoration:none">Not printing a doc set</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>e.<a href="index.php?mlink=printblank"  
	style="text-decoration:none">Some pages come up blank</b></a>
<br>
<!-------------------------IMPORT PROBLEMS------------------------------>
<p><b>3</b>.<b>IMPORT PROBLEMS</b></a></p>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>a.<a href="index.php?mlink=filenotfound"  
	style="text-decoration:none">File Look Up Error, App not found!</a>
<br>
<!------------------------IMPORTING FROM DATATRAC--------------------------->
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
b.Importing from DataTrac</b>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>i.<a href="index.php?mlink=nolender"  
	style="text-decoration:none">Lender Vanish after DataTrac import 
</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>ii.<a href="index.php?mlink=mapping"  
	style="text-decoration:none">Some fields not importing</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>iii.<a href="index.php?mlink=dtracimperror"  
	style="text-decoration:none">Import Error -2147217887 or similar</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>iv.<a href="index.php?mlink=dtracimperror2"  
	style="text-decoration:none">Microsoft ODBC Driver Error
</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>v.<a href="index.php?mlink=imptrac"  
	style="text-decoration:none">Imptrac Error</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>vi.<a href="index.php?mlink=dtraccurtab"  
	style="text-decoration:none">Curtab Error</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>vii.<a href="index.php?mlink=connectionerror"  
	style="text-decoration:none">Connection Error</b></a>
<br>
<!-------------------IMPORTING FROM POINT--------------------------->
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>b.Importing from POINT</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>i.<a href="index.php?mlink=pointmissinginfo"  
	style="text-decoration:none">Some Fields are blank</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>ii.<a href="index.php?mlink=pointcurtab"  
	style="text-decoration:none">Curtab Error</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>iii.<a href="index.php?mlink=imppoint1"  
	style="text-decoration:none">ImpPoint  tabs: code 123456 ...</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>iv.<a href="index.php?mlink=imppoint2"  
	style="text-decoration:none">ImpPoint Run-time error '3265'</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>v.<a href="index.php?mlink=badfeecalc"  
	style="text-decoration:none">Fee Miscalculation</b></a>
<!------------VB ERROR #380 IMPORTING TO DOCUTECH------------------->
<p><b>4</b>.<a href="index.php?mlink=vb380"  
	style="text-decoration:none"><b>VB Error #380 Importing to DocuTech</b></a></p>
<!------------------WEBEX ERROR---------------------------->
<p><b>5</b>.<a href="index.php?mlink=webexerror"  
	style="text-decoration:none"><b>WEBEX ERROR</b></a></p>
<!-----------------SEND DOC CREATION ERRORS-------------------------->
<p><b>6.SEND DOC Creation Errors</b></a></p>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>a.<a href="index.php?mlink=senddocvb"  
	style="text-decoration:none">E-mail Send Doc VB Error</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>b.<a href="index.php?mlink=sd_filenotfound"  
	style="text-decoration:none">File Not Found Error</b></a>
<!-------------------SEND DOC ERRORS--------------------------->
<p><b>7</b>.<b>SEND DOC ERRORS</b></a></p>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>a.<a href="index.php?mlink=senddocerror"  
	style="text-decoration:none">DTNDSH, and Read Error</b></a>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>b.<a href="index.php?mlink=printall_noprint"  
	style="text-decoration:none">Not Printing, when click Print All</b></a>
<!----------------------------------------------------------------------->
</body>
</html>

And here is my case.php:

Code: Select all

<?php

	switch($mlink)
	{
	 case"connectionerror":
	 include('\includes\connectionerror.php');
	break;
	 case"badfeecalc":
	 include('\includes\badfeecalc.php');
	break;
	 case"cca":
	 include('\includes\cca.php');
	break;
	 case"upgrade":
	 include('\includes\upgrade.php');
	break;
	 case"runtime430":
	 include('\includes\runtime430.php');
	break;
	 case"runtime":
	 include('\includes\runtime.php');
	break;
	 case"dbq":
	 include('\includes\dbq.php');
	break;
	 case"apperror":
	 include('\includes\apperror.php');
	break;
	 case"senddocvb":
	 include('\includes\senddocvb.php');
	break;
	 case"dtpassword":
	 include('\includes\dtpassword.php');
	break;
	 case"dtpassrt":
	 include('\includes\dtpassrt.php');
	break;
	 case"print52or6":
	 include('\includes\print52or6.php');
	break;
	 case"printline":
	 include('\includes\printline.php');
	break;
	 case"printinvestor":
	 include('\includes\printinvestor.php');
	break;
	 case"printdocset":
	 include('\includes\printdocset.php');
	break;
	 case"printblank":
	 include('\includes\printblank.php');
	break;
	 case"mapping":
	 include('\includes\mapping.php');
	break;
	 case"dtracimperror":
	 include('\includes\dtracimperror.php');
	break;
	 case"dtracimperror2":
	 include('\includes\dtracimperror2.php');
	break;
	 case"imptrac":
	 include('\includes\imptrac.php');
	break;
	 case"dtraccurtab":
	 include('\includes\dtraccurtab.php');
	break;
	 case"pointmissinginfo":
	 include('\includes\pointmissinginfo.php');
	break;
	 case"pointcurtab":
	 include('\includes\pointcurtab.php');
	break;
	 case"imppoint1":
	 include('\includes\imppoint1.php');
	break;
	 case"imppoint2":
	 include('\includes\imppoint2.php');
	break;
	 case"webexerror":
	 include('\includes\webexerror.php');
	break;
	 case"vb380":
	 include('\includes\vb380.php');
	break;
	 case"senddocerror":
	 include('\includes\senddocerror.php');
	break;
	 case"filenotfound":
	 include('\includes\filenotfound.php');
	break;
	 case"printall_noprint":
	 include('\includes\printall_noprint.php');
	break;
	 case"sd_filenotfound":
	 include('\includes\sd_filenotfound.php');
	break;
	 case"nolender":
	 include('\includes\nolender.php');
	break;
	default:
	//when nothing is true do this
	 include('\includes\new.php');
	}

?>
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

Post by bironeb »

Well its Monday, I'm back to working on this.... Any suggestions?
h@mm3r
Forum Newbie
Posts: 10
Joined: Mon Nov 24, 2003 9:35 am

Post by h@mm3r »

Maybe this helps a bit:

Code: Select all

<?php
if (file_exists("\includes" . $mlink . ".php")) {
   include("\includes" . $mlink . ".php");
}
?>
Post Reply