PHP导航页面

On my site many server tasks are implemented in separete php files. The tasks are executed from client-side via Ajax post.

var param = "p1=" + val1 + "&p2=" + val2; 
xmlhttp.open("POST", "/dir1/dir2/dir3/task_a.php", true);    
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
xmlhttp.send(params);

task_a.php:

<?php
  //...
  // some code $_SESSION and $_REQUEST are in use

I want:

  • To be able to change task-files names, without changing the client side code.
  • Hide the position of the task-files path from the client
  • Make the client-side code more general, shorter and simpler

My idea is that the user will access only one page the navigator.php and translate the task name trough post parameter (via ajax). But I dont know how the implement this idea.

navigator.php:

<?php
  if (isset($_REQUEST['task']) $action = $_REQUEST['task']; 

  if ($task == 'a') { 
    // how to execute task_a.php from here?
  }
  if ($task == 'b') { 
    // ...
  }

I tried simple redirection header("Location: ...");, but i can't send the post parameters.
I also tried cURL, but it is not allows to change the $_SESSION (this is required in several tasks).

Any suggestions will be appreciated.

You can easily do this using either include or bringing all the logic from the separate php files into one and using some functions.

For good measure and if applicable you should define an array of allowed tasks and verify if the incoming POST request contains an allowed value.

Example

<?php

$allowedTasks = ['a','b'];

$task = (isset($_POST['task'] and in_array($_POST['task'],$allowedTasks))? $task = $_POST['task'] : false; 

switch($task){
    case 'a':
        include 'task-a-file.php';
        functionFromFileA();
    break;
    case 'b':
        include 'someOtherfile.php';
        // run some other code
    break;
    default:
        print 'Error: Unsupported task or action';
    break;
}