Jump to content

Reduce redundancies in switch functions?


Kris.thedriftdemon

Recommended Posts

Hello all!  I somewhat new to PHP, and was wondering if anyone could give some suggestions on a switch function to reduce redundancies, and to efficaciously implement the script on to other sites.


<?php 

$choice=$_GET['act'];

switch($choice)
{
case 'about':
if(file_exists("includes/about.php"))
include("includes/about.php");
break;


case 'contact':
if(file_exists("includes/contact.php"))
include("includes/contact.php");
break;


default;
include("includes/about.php"); } 

?>



Link to comment
Share on other sites

define('URI_ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);
define('URI_PAGES', URI_ROOT . 'pages' . DIRECTORY_SEPARATOR);

function render($page, $variables) {
    $page_path = name2path($page);
    if (!@require_once($page_path)) {
        trigger_error("Page $page_path could not be loaded.");
        header('HTTP/1.0 404 Not Found');
        exit(0);
    }
}

function name2path($page) {
    return URI_PAGES . $page . '.php';
}

$default_page = 'about';
$request = array_merge($_GET, $_POST);
$page = isset($request['page']) ? $request['page'] : $default_page;
if (!preg_match('/[a-z]/', $page)) {
    trigger_error("Incorrect page format given: $page");
    render('error', array('errors' => 'Page not found.'));
    exit(0);
}

render($page, $request);

Link to comment
Share on other sites

Ok!  Not necessarily the answer I was looking for, but I understand.  What I was thinking about was something like this.


<a href="index.php?act=$page1"><li>About</li></a>
<a href="index.php?act=$page2"><li>Contact</li></a>

<?php

$page1     = include("includes/about.php") ;
$page2     = include("includes/contact.php") ;
?>

<?php 

$choice=$_GET['act'];

switch($choice)
{
case '$page1':
if(file_exists('$page1'));
break;



case '$page2':
if(file_exists('$page2'));
break;


default;
include('$page1'); } 

?>


Link to comment
Share on other sites

if your using the file_exists function you won't really need the switch

 

$choice= $_GET['act'];

include (file_exists("includes/$choice.php")  ? "includes/$choice.php" : "includes/about.php");

Couldn't that cause possible security issues as you can potentially include any file in the includes directory as long as it exists....

Link to comment
Share on other sites

if your using the file_exists function you won't really need the switch

 

$choice= $_GET['act'];

include (file_exists("includes/$choice.php")  ? "includes/$choice.php" : "includes/about.php");

Couldn't that cause possible security issues as you can potentially include any file in the includes directory as long as it exists....

 

Yes I though on that but since we prefix it with includes and postfix it with php the user will be including only php files, the file_exists will check if the file exists and execute, the user can execute the file directly from the browser giving http://www.site.com/includes/file.php so i ruled out that possibility

Link to comment
Share on other sites

@salathe would my solution work?

 

It would "work" in the same way that rajivgonsalves's example also "works". They both do the job and also suffer from precisely the same problem: yours pretends to make an effort to filter the input (even though that doesn't help at all).

Link to comment
Share on other sites

Oh God here we go again with this nonsense.

 

Use a white list.

 

<?php
$good = array( 'contact', 'about', 'home', 'index' );
$requested = array_key_exists( 'page', $_GET ) ? $_GET['page'] : 'home';
if( in_array( $requested, $good ) ) include( $requested . '.php' );
else { header( 'Location: 404.php' ); exit(); }
?>

Link to comment
Share on other sites

How so? The way I see it you are limited to a-z so something like ../../. wouldn't work or am I missing something?

 

You're missing something: the pattern just attempts to match any one single lowercase letter within the subject string. It makes no attempt to check that all of the characters within that string are lowercase letters.

Link to comment
Share on other sites

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.