[SOLVED] alternative to php switch case for include?
Moderator: General Moderators
[SOLVED] alternative to php switch case for include?
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?
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.
If it's all the links have the same name as the file, you can go along:
-Nay
Code: Select all
<?php
$link = $_GET['mlink'];
$str = "\\includes" . $link . ".php";
include($str);
?>getting error
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?
<?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?
- Saethyr
- Forum Contributor
- Posts: 182
- Joined: Thu Sep 25, 2003 9:21 am
- Location: Wichita, Kansas USA
- Contact:
try this
I believe isset is the correct code
Code: Select all
<?php
if (isset($_GET['mlink']))
{
$link = $_GET['mlink'];
}
else
{
$link = "new";
$str = "\\includes" . $link . ".php";
include($str);
}
?>Code: Select all
<?php
if (isset($_HTTP_GET_VARS['mlink']))
{
$link = $__HTTP_GET_VARS['mlink'];
}
else
{
$link = "new";
$str = "\\includes" . $link . ".php";
include($str);
}
?>-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
whats wrong with
and make the URL http://www.site.com/page.php?link=home
(would load home.php)
o_O
Code: Select all
include ('$link.php');(would load home.php)
o_O
$link has to be imported from GETLiLpunkSkateR wrote:whats wrong withand make the URL http://www.site.com/page.php?link=homeCode: Select all
include ('$link.php');
(would load home.php)
o_O
Wait a minute! Hold your horses. Be careful about some of the above code.
A better solution, for security reasons, is the following:
[php_man]basename[/php_man] pretty much protects against someone doing something like this:
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!
A better solution, for security reasons, is the following:
Code: Select all
<?php
include 'includes/'.basename($_GET['view'], '.php');
?>Code: Select all
url.php?view=../config.incRemember: ALWAYS VALIDATE EXTERNAL INPUT. If you didn't create it, it's not safe!
Well first I tried using Qads:
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
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.
Code: Select all
<?php
if (isset($_HTTP_GET_VARS['mlink']))
{
$link = $__HTTP_GET_VARS['mlink'];
}
else
{
$link = "new";
$str = "\\includes" . $link . ".php";
include($str);
}
?>Then i tried
Code: Select all
<?php
include 'includes/'.basename($_GET['view'], '.php');
?>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.
Maybe I should provide more examples of the code:
Here is my index.php
Here is my menu.php:
And here is my case.php:
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> 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>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>
<b>a. Run-Time Errors</b>
<br>
<b>i.<a href="index.php?mlink=runtime430"
style="text-decoration:none">430</b></a>
<br>
<b>ii.<a href="index.php?mlink=runtime"
style="text-decoration:none">-2147221231 or similar</b></a>
<br>
<b>iii.<a href="index.php?mlink=dtpassrt"
style="text-decoration:none">-2147217904 & 440</b></a>
<br>
<b>b.<a href="index.php?mlink=upgrade"
style="text-decoration:none">Upgrade DTDBEvolveDLL Error</b></a>
<br>
<b>c.<a href="index.php?mlink=dbq"
style="text-decoration:none">Configuration Error: DBQ</b></a>
<br>
<b>d.<a href="index.php?mlink=apperror"
style="text-decoration:none">Closing.exe Application Error</b></a>
<br>
<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>
<b>a.<a href="index.php?mlink=print52or6"
style="text-decoration:none">Error 6, 9, 51, 52 or 91</b></a>
<br>
<b>b.<a href="index.php?mlink=printline"
style="text-decoration:none">Data Running off the page</b></a>
<br>
<b>c.<a href="index.php?mlink=printinvestor"
style="text-decoration:none">Not printing from one specific Investor</b></a>
<br>
<b>d.<a href="index.php?mlink=printdocset"
style="text-decoration:none">Not printing a doc set</b></a>
<br>
<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>
<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>
b.Importing from DataTrac</b>
<br>
<b>i.<a href="index.php?mlink=nolender"
style="text-decoration:none">Lender Vanish after DataTrac import
</b></a>
<br>
<b>ii.<a href="index.php?mlink=mapping"
style="text-decoration:none">Some fields not importing</b></a>
<br>
<b>iii.<a href="index.php?mlink=dtracimperror"
style="text-decoration:none">Import Error -2147217887 or similar</b></a>
<br>
<b>iv.<a href="index.php?mlink=dtracimperror2"
style="text-decoration:none">Microsoft ODBC Driver Error
</b></a>
<br>
<b>v.<a href="index.php?mlink=imptrac"
style="text-decoration:none">Imptrac Error</b></a>
<br>
<b>vi.<a href="index.php?mlink=dtraccurtab"
style="text-decoration:none">Curtab Error</b></a>
<br>
<b>vii.<a href="index.php?mlink=connectionerror"
style="text-decoration:none">Connection Error</b></a>
<br>
<!-------------------IMPORTING FROM POINT--------------------------->
<b>b.Importing from POINT</b></a>
<br>
<b>i.<a href="index.php?mlink=pointmissinginfo"
style="text-decoration:none">Some Fields are blank</b></a>
<br>
<b>ii.<a href="index.php?mlink=pointcurtab"
style="text-decoration:none">Curtab Error</b></a>
<br>
<b>iii.<a href="index.php?mlink=imppoint1"
style="text-decoration:none">ImpPoint tabs: code 123456 ...</b></a>
<br>
<b>iv.<a href="index.php?mlink=imppoint2"
style="text-decoration:none">ImpPoint Run-time error '3265'</b></a>
<br>
<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>
<b>a.<a href="index.php?mlink=senddocvb"
style="text-decoration:none">E-mail Send Doc VB Error</b></a>
<br>
<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>
<b>a.<a href="index.php?mlink=senddocerror"
style="text-decoration:none">DTNDSH, and Read Error</b></a>
<br>
<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');
}
?>Maybe this helps a bit:
Code: Select all
<?php
if (file_exists("\includes" . $mlink . ".php")) {
include("\includes" . $mlink . ".php");
}
?>