Using switches... with sticks. not solved I guess O.o

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Using switches... with sticks. not solved I guess O.o

Post by theda »

Well, maybe not with sticks, but still...

I was curious if there's a way to convert

Code: Select all

<?php
$query = $_SERVER['QUERY_STRING'];
$query = explode(':',$query);
  if(empty($query[0]) && empty($query[1])) {
    include 'home.php';
  } elseif(empty($query[1])) {
    include $query[0].'.php';
  } else {
    include $query[0].'/'.$query[1].'.php';
  }
?>
to switch(). The only part that confuses me is the empty() part and the last 2 includes...
Last edited by theda on Thu Jul 28, 2005 10:16 am, edited 2 times in total.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Re: Using switches... with sticks.

Post by nielsene »

Perhaps:

Code: Select all

<?php
$query = $_SERVER['QUERY_STRING'];
$query = explode(':',$query);
$numComponents = count($query);
switch($numComponents) {
  case 0 : include 'home.php'; break;
  case 1 : include "{$query[0]}.php"; break;
  default : include "{$query[1]}/{$query[1]}.php"; break;
}
?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Why would you want to convert? What is unclear about the if-else structure?

Code: Select all

switch(true)
{
  case (empty($query[0]) && empty($query[1])):
    include 'home.php';
    break;
  case (empty($query[1])):
    include $query[0].'.php';
    break;
  default:
    include $query[0].'/'.$query[1].'.php';
}
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Well, in this case, I would like the switch version. It makes it more clear that the different logic branches are based on the number of path components. The if version using all those empty()'s seems a tad more confusing.
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

+1 For Nielsene

He got it right on the money. I originally didn't code that little snippet... The reason I use that snippet is to detect if a webpage exists... aka ?id=asdfasdflhsadf if it doesnt exist, it automatically opens the default page, and if its ?id= then it loads the default page.

The main reason I want it converted to switch() is so that I can use the invano navigation (instead of ?id= it's just ?)

Edit: Here's my script that I'm trying to convert to the invano navigation

Code: Select all

if (!$id) {
        $id = "news";
        $include = $id . ".php";
    } else {
        $include = $id . ".php";
    }

if (is_file($include) == "1") {
            include $include;
    } else {
        include "news.php";
    }
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Re: Using switches... with sticks.

Post by nielsene »

Just a few modifications from a security standpoint:

Code: Select all

&amp;lt;?php
$query = $_SERVER&#1111;'QUERY_STRING'];
$query = explode(':',$query);
$numComponents = count($query);
switch($numComponents) {
  case 0 : $gotoFile='home.php'; break;
  case 1 : $gotoFile=&quote;{$query&#1111;0]}.php&quote;; break;
  default : $gotoFile=&quote;{$query&#1111;1]}/{$query&#1111;1]}.php&quote;; break;
}
// Probably want to url_decode $gotoFile
// probably want to stip any '../'s from $gotoFile
// probably want to ensure that $gotoFile is in the webroot
//   or a list of approved safe directories, etc
if (file_exists($gotoFile) {
  include($gotoFile);
} else {
  include('home.php'); // or an error page and log &quote;attack&quote;
}
?&amp;gt;
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

So, this will do exactly what my script used to do (check if file exists) and also do the invano navigation?

Edit: Yes it does. Thanks. And it actually makes more sense then my previous snippet did.
.
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

Code: Select all

<?

// Author: theda of http://dumbass.ionichost.com 2005.
// This package distribution is meant solely for people to learn about how my page is displayed. It does not contain any of the content, but rather just the PHP back-end.
// Please feel free to snatch anything from this script, but please do not use the entire script for any means other than debugging or learning.
// This page helps define all of the identities (variables).
// Many of the pages were spliced off of this page into various parts to simplify modifications.
// Variable $del stands for "deletion of a cookie" variable.
// Variable $ver stands for the "language version" variable.
// Variable $the stands for the "theme scheme" variable.
// Variable $id stands for the "page identification" variable.
// If you have any questions about how this all works or if there's an error, please email me at mbspam@mindspring.com
// Thanks!!

// Deleting cookie Identity
    switch($del) {
        case "en": case "de": setcookie('ver',$del, time()-60*60*24*30); header("Location: /"); break;
        default:

// START $DEL SWITCH DEFAULT
// Language Identity
    if (isset($HTTP_COOKIE_VARS['ver']) AND empty($HTTP_GET_VARS['ver'])) {
          $ver = $HTTP_COOKIE_VARS['ver'];

// BEGIN $VER LAYOUT PRINT
// Theme Identity
    if (isset($HTTP_COOKIE_VARS['the']) AND empty($HTTP_GET_VARS['the'])) {
        $the = $HTTP_COOKIE_VARS['the'];
    } elseif (isset($HTTP_GET_VARS['the'])) {
        $the = $HTTP_GET_VARS['the'];
        setcookie('the',$the, time()+60*60*24*30);
        header('P3P: CP="NOI DSP NID STP"');
    } else {
        setcookie('the','see', time()+60*60*24*30);
        header('P3P: CP="NOI DSP NID STP"');
        $the = "see";
    }

// Opposite Language Identity
    switch($ver) {
        default:
        case "en": $ver2 = "de"; break;
        case "de": $ver2 = "en"; break;
    }

// Page Identity

$query = $_SERVER['QUERY_STRING'];
$query = explode(':',$query);
$numComponents = count($query);
switch($numComponents) {
  case 0 : print "<meta http-equiv=\"refresh\" content=\"0;url=/?news\">"; break;
  case 1 : $gotoFile="{$query[0]}.php"; break;
  default : $gotoFile="{$query[0]}/{$query[1]}.php"; break;
}

$id = $gotoFile;

// URL Identity
    switch($query[0]) {
        default:
        case "news": $url = "?news"; break;
        case "links": $url = "?links"; break;
        case "scripts": $url = "?scripts"; break;
        case "rants": $url = "?rants"; break;
        case "photos": $url = "?photos"; break;
        case "poems": $url = "?poems"; break;
        case "conts": $url = "?conts"; break;
    }

// Load time and Last Modified Identity
        $last_modified = @filemtime($id);
        $pagelast = date("d. M Y", $last_modified);
        $load = number_format(microtime(),2);

// Include Main Page
        include "img/main.php";

// END $VER LAYOUT PRINT
// Continuing Language Identity
    } elseif (isset($HTTP_GET_VARS['ver'])) {
          $ver = $HTTP_GET_VARS['ver'];
          setcookie('ver',$ver, time()+60*60*24*30);
        header('P3P: CP="NOI DSP NID STP"');
        header("Location: /?$id");
    } else {
        setcookie('ver','$ver', time()-60*60*24*30);
        include "img/splash.php";
    }

// END $DEL SWITCH DEFAULT

// Continuing Deleting cookie Identity
        break;
    }
?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I still wonder, why do you mix $HTTP_*_VARS with $_* ?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

timvw wrote:I still wonder, why do you mix $HTTP_*_VARS with $_* ?
He told me he's on php 4.0 but his local test server is php 5.0 so he changes stuff around as he moves from test server to live server.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

jshpro2 wrote:
timvw wrote:I still wonder, why do you mix $HTTP_*_VARS with $_* ?
He told me he's on php 4.0 but his local test server is php 5.0 so he changes stuff around as he moves from test server to live server.
Still deprecated as of PHP4 so that shouldn't make a difference ;)

<= PHP3 &-->& >=PHP4 would though
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Well PHP4.1 was the switch over point I think....
Post Reply