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!
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';
}
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.
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
<?php
$query = $_SERVERї'QUERY_STRING'];
$query = explode(':',$query);
$numComponents = count($query);
switch($numComponents) {
case 0 : $gotoFile='home.php'; break;
case 1 : $gotoFile="e;{$queryї0]}.php"e;; break;
default : $gotoFile="e;{$queryї1]}/{$queryї1]}.php"e;; 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 "e;attack"e;
}
?>
<?
// 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;
}
?>