Jump to content

How would you make this script shorter/more efficient?


Recommended Posts

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";

?>

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.

<?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?

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!

 

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

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.