Page 1 of 1
Using switches... with sticks. not solved I guess O.o
Posted: Thu Jul 28, 2005 9:29 am
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...
Re: Using switches... with sticks.
Posted: Thu Jul 28, 2005 9:33 am
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;
}
?>
Posted: Thu Jul 28, 2005 9:36 am
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';
}
Posted: Thu Jul 28, 2005 9:43 am
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.
Posted: Thu Jul 28, 2005 9:47 am
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";
}
Re: Using switches... with sticks.
Posted: Thu Jul 28, 2005 9:55 am
by nielsene
Just a few modifications from a security standpoint:
Code: Select all
&lt;?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;
}
?&gt;
Posted: Thu Jul 28, 2005 9:56 am
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.
.
Posted: Thu Jul 28, 2005 10:30 am
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;
}
?>
Posted: Thu Jul 28, 2005 11:42 am
by timvw
I still wonder, why do you mix $HTTP_*_VARS with $_* ?
Posted: Thu Jul 28, 2005 11:52 am
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.
Posted: Thu Jul 28, 2005 12:34 pm
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
Posted: Thu Jul 28, 2005 12:35 pm
by nielsene
Well PHP4.1 was the switch over point I think....