Jump to content

Exploding $_GET parameters into a list and checking for values


irken

Recommended Posts

Hello.

 

I have the following piece of code:

 

if (isset($_GET['q'])) {
  $params = explode('/', trim(stripslashes($_GET['q'])));
  @list($plugin, $function) = $params;

  # index.php?q=test/add
  # index.php?q=test/del
  if (isset($plugin) && isset($function)) {
    $plugin_path = './plugins/' . strtolower($plugin) . '/' . strtolower($function) . '.php';
    if (is_file($plugin_path)) {
      @include_once $plugin_path;
      exit;
    }   
  }

  # index.php?q=test
  if (isset($plugin) && !isset($function)) {
    $plugin_path = './plugins/' . strtolower($plugin) . '.php';
    if (is_file($plugin_path)) {
      @include_once $plugin_path;
      exit;
    }   
  }
}
else {
  // Show front page..
}

 

What this does is allow me to write for example:

index.php?q=test - to include the file located at ./plugins/test.php

index.php?q=test/add -  to include the file located at ./plugins/test/add.php

 

And so forth. I'm looking for a more dynamic way of doing this. What if I need to have yet another parameter for articles perhaps.

 

index.php?q=article/view/01

 

That would require me to make yet another if expression and expand the list statement:

 

@list($plugin, $function) = $params;
if (isset($plugin) && isset($function) && isset($article_id)) { .. }

 

Is there a way around this? Perhaps even a better way.

 

It's not very dynamic if I have to change my functions each time I need to add a feature and/or module and would result in like 50 if checks.

 

Thanks for reading.

Archived

This topic is now archived and is closed to further replies.

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