Jump to content

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
https://forums.phpfreaks.com/topic/185807-reduce-redundancies-in-switch-functions/
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);

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'); } 

?>


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

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

@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).

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(); }
?>

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.

Re: http://www.phpfreaks.com/forums/index.php/topic,281354.msg1333298.html#msg1333298

 

Doing this:

if (!@require_once($page_path)) {
    // something
}

is not a good idea. include and its relatives return whatever the file returns; it doesn't return true on success and false on failure. A file may very well return false.

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.