Be a Supporter!

PHP crap question

  • 442 Views
  • 4 Replies
New Topic Respond to this Topic
Toxxicc
Toxxicc
  • Member since: Mar. 2, 2006
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
PHP crap question 2006-11-19 15:04:39 Reply

Well, I'm making a directory script based off tidbits from here and there,
but I keep running into something I can't figure out...
I have the page read a directory based on what the url is,

e.g. "directory.php?dir=index"

the script I made can be found here.

It's messy, yes, but I'm not worried about how it looks, just how it works.

Anyway, the problem is that when add "?dir=xxx" it works perfectly, but if I don't put it then it results in an error (click.)

What is causing this error, and how could I fix it?

thecoshman
thecoshman
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to PHP crap question 2006-11-19 15:19:44 Reply

not looking at your script, but i bet that it has no way of coping with out a variable set. either tell it a efult variable, on only have it run if the variable is set.

you can check if a varable is set buy going:

if (isset($var)){
true
} else {
flase
}

JeremysFilms
JeremysFilms
  • Member since: Feb. 18, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to PHP crap question 2006-11-19 15:28:30 Reply

Well you'd probably want to test if the dir variable is feeded into the URL and if not just redirect to the main page, along with if it's set to an unfound file.

So you need to test if the variable is set in the URL by doing something like:
if(!isset($_GET['dir'])){
//redirect stuff
}

Then you need to test if the file is found.. so use something like:
if(!file_exists($dir)){
//redirect stuff
}else{
//continue
}

The continue comment area is for the rest of the stuff, like this should go before it tries to open the directory.

elbekko
elbekko
  • Member since: Jul. 23, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to PHP crap question 2006-11-19 15:29:49 Reply

Well, the simplest solution for this kind of thing is to put this line on top:

$readdir = isset($_GET['dir']) ? $_GET["dir"] : 'index';

Basically, it'll check if the dir is specified, and uses that, but uses index if dir isn't specified.


"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature
Toxxicc
Toxxicc
  • Member since: Mar. 2, 2006
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to PHP crap question 2006-11-19 15:52:36 Reply

At 11/19/06 03:29 PM, elbekko wrote: Well, the simplest solution for this kind of thing is to put this line on top:

$readdir = isset($_GET['dir']) ? $_GET["dir"] : 'index';

Basically, it'll check if the dir is specified, and uses that, but uses index if dir isn't specified.

Thanks, works like a charm.