cgm225 Posted February 7, 2008 Share Posted February 7, 2008 Currently, I have a site that has content included from files in a /includes/php directory, files located right in that directory, or any of its subdirectories. I grab those files with an index.php script based on an "id" variable that corresponds the the file name (see below). However, I am still relatively new to php, and wanted to know if there were better ways to code what I have below, to make the script shorter/more efficient? Any ideas? Thanks in advance! <?php session_start(); ob_start(); $id = $_GET['id']; include_once "includes/php/functions.inc.php"; include_once "includes/php/admin/configurations.inc.php"; include_once "includes/php/header.inc.php"; if ($id == "") { include_once "includes/php/general/frontpage.inc.php"; include_once "includes/php/footer.inc.php"; die(); } if (file_exists("includes/php/general/$id.inc.php")) { include_once "includes/php/general/$id.inc.php"; include_once "includes/php/footer.inc.php"; die(); } if (file_exists("includes/php/gallery/$id.inc.php")) { include_once "includes/php/gallery/$id.inc.php"; include_once "includes/php/footer.inc.php"; die(); } if (file_exists("includes/php/admin/$id.inc.php")) { include_once "includes/php/admin/$id.inc.php"; include_once "includes/php/footer.inc.php"; die(); } include_once "includes/php/error.inc.php"; include_once "includes/php/footer.inc.php"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/89891-how-would-you-make-this-script-shortermore-efficient/ Share on other sites More sharing options...
haku Posted February 7, 2008 Share Posted February 7, 2008 You can take out $id=$_GET['id'] and change the line that references $id so that it references $_GET['id'] instead. If your goal is faster loading etc, then you should take out ob_start() as that slows pages down a fair bit. Its not recommended. Quote Link to comment https://forums.phpfreaks.com/topic/89891-how-would-you-make-this-script-shortermore-efficient/#findComment-460732 Share on other sites More sharing options...
cgm225 Posted February 7, 2008 Author Share Posted February 7, 2008 Anything else? Quote Link to comment https://forums.phpfreaks.com/topic/89891-how-would-you-make-this-script-shortermore-efficient/#findComment-460743 Share on other sites More sharing options...
aschk Posted February 7, 2008 Share Posted February 7, 2008 <?php session_start(); ob_start(); $id = $_GET['id']; include_once "includes/php/functions.inc.php"; include_once "includes/php/admin/configurations.inc.php"; ?> There you go, much shorter... oh dang, did you say it needed to do something still? LOL Die() = BAD in any scenario Also, why are you output buffering? Quote Link to comment https://forums.phpfreaks.com/topic/89891-how-would-you-make-this-script-shortermore-efficient/#findComment-460765 Share on other sites More sharing options...
cgm225 Posted February 7, 2008 Author Share Posted February 7, 2008 I am output buffering so the page included (based on the "id" variable), can "tell" the header file what the <title> of the page is going to be. Do you have another way to accomplish this? Also, going back to my original question of making the script shorter (and possibly removing the die() functions), is there a way to have a loop go through every subdirectory looking for the $id.inc.php file rather than me having to specify specific directories as I have done? How would I do that? Thank you all for your feedback so far! Quote Link to comment https://forums.phpfreaks.com/topic/89891-how-would-you-make-this-script-shortermore-efficient/#findComment-460839 Share on other sites More sharing options...
kenrbnsn Posted February 7, 2008 Share Posted February 7, 2008 Here's my take: <?php session_start(); $tst = array('general','gallery','admin'); include_once "includes/php/functions.inc.php"; include_once "includes/php/admin/configurations.inc.php"; include_once "includes/php/header.inc.php"; if (!isset($_GET['id'])) include_once "includes/php/error.inc.php"; elseif ($_GET['id'] == '') include_once "includes/php/general/frontpage.inc.php"; else { $ff = false; for ($i = 0;$i < count($tst) && !$ff;$i++) { if file_exists('includes/php/' . $tst[$i] . '/' . $_GET['id'] . '.inc.php') { $ff = true; include_once('includes/php/' . $tst[$i] . '/' . $_GET['id'] . '.inc.php'); } } if (!$ff) include_once "includes/php/error.inc.php"; } include_once "includes/php/footer.inc.php"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/89891-how-would-you-make-this-script-shortermore-efficient/#findComment-460847 Share on other sites More sharing options...
cgm225 Posted February 7, 2008 Author Share Posted February 7, 2008 Ken, you are a genius. That is a great restructuring of my script! Two follow-up question:: 1. how could I make the $tst array more generic to simply include all subdirectories so I do not have to specifically specify them? 2. what about searching for files simply in the root includes/php directory? Thank you so much in advance! Quote Link to comment https://forums.phpfreaks.com/topic/89891-how-would-you-make-this-script-shortermore-efficient/#findComment-460871 Share on other sites More sharing options...
aschk Posted February 7, 2008 Share Posted February 7, 2008 This sort of reminds me of a front controller. Have a look at the php __autoload() function http://uk.php.net/autoload. You might find this handy for auto loading any include files you need. Quote Link to comment https://forums.phpfreaks.com/topic/89891-how-would-you-make-this-script-shortermore-efficient/#findComment-460942 Share on other sites More sharing options...
cgm225 Posted February 7, 2008 Author Share Posted February 7, 2008 Thank you for the autoload reference. Can you show me how that would be applied to my script. I am not sure how to apply that? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/89891-how-would-you-make-this-script-shortermore-efficient/#findComment-460976 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.