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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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