PHP Classes

File: execScript.php

Recommend this page to a friend!
  Classes of Xavier Pérez   ExecScript   execScript.php   Download  
File: execScript.php
Role: Class source
Content type: text/plain
Description: execScript php
Class: ExecScript
Execute a Web page script from the command line
Author: By
Last change: Placed execScript in same directory than index.php, to work with any php framework
Date: 11 years ago
Size: 2,217 bytes
 

Contents

Class file image Download
#!/usr/local/bin/php
<?php
// Set time limit and memory for script execution, if necessary
// set_time_limit(0);
// Set memory limit if necessary
// ini_set('memory_limit', '512M');

/**
* execScript.php param1 param2
*
* param1 = controller/method as typed in url
* param2 = host
*
* Examples:
*
* php execScript.php welcome myhost.com
* php execScript.php mymodule/mymethod myhost.com
* php execScript.php mycontroller/mymethod myhost.com
*/

// Define commmand line script, you can protect your controller checking this.
// First line of your controller (if only allowed to be executed from commandline):
// <?php if (!defined("ONLY_COMMAND_LINE_SCRIPT") || ONLY_COMMAND_LINE_SCRIPT===false) die('Bad request');
define('ONLY_COMMAND_LINE_SCRIPT', true);


class
execScript
{
    public function
__construct()
    {
        global
$argv,$argc;
       
        if (!isset(
$argv[1]) || !isset($argv[2]))
        {
           
$this->instructions();
        }
       
// Set host
       
$HTTP_HOST = $argv[2];

       
// Unset host (second parameter)
       
unset ($argv[2]);

       
// If not set host, exit
       
if ($HTTP_HOST == '')
                return -
1;

       
// If this script are called from any other source, exit
       
if (isset($_SERVER['REMOTE_ADDR'])) die('Permission denied.');

       
// Set necessary server info
       
$_SERVER["argv"] = $argv;
       
$_SERVER["argc"] = $argc;
       
$_SERVER['PATH_INFO'] = '/' . implode('/', $argv) . '/';
       
$_GET = ''; // Required for some installations
       
$_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO'];
       
$_SERVER['HTTP_HOST'] = $HTTP_HOST;

    }
   
    protected function
instructions()
    {
        echo
"\n";
        echo
"\nUse: php exceScript.php controller/method host \n";
        echo
"\nExample: php execScript.php welcome myhost.com\n\n";
        echo
"\nBe sure to have the php executable in your PATH environment.\n";
        die();
    }
}

// Load the execScript class, assigns SERVER variables to emulate web access
$script = new execScript();

// Include the index.php of your framework
require_once('index.php');