NYCPHP Meetup

NYPHP.org

[nycphp-talk] How do you do the equivalent of $PHP_SELF for PHP scripts not in docroot?

Phillip Powell phillip.powell at adnet-sys.com
Tue May 18 17:38:25 EDT 2004


Joel De Gan wrote:

>On Tue, 2004-05-18 at 15:43, Phillip Powell wrote:
>  
>
>>I can easily write config.php, however, my knowledge of "define" comes 
>>from the PHP manual, and I see nowhere where it states that anything in 
>>define() is in a persistent state.  If I run "config.php", I lose all of 
>>my defined constants, don't I?  And when I go to "index.php" via 
>>browser, how would it know how to go to config.php if config.php would 
>>have to reside outside of the docroot?
>>
>>Again I'm probably overcomplicating this but I can't honestly follow 
>>your train of logic, though I want to.
>>    
>>
>
>just try it.
>
>if you want to know all your defined constants try the following in a
>script.
>
>echo "<pre>\n";
>print_r($GLOBALS);
>
>
>joeldg - developer, Intercosmos media group.
>http://lucifer.intercosmos.net
>
>_______________________________________________
>talk mailing list
>talk at lists.nyphp.org
>http://lists.nyphp.org/mailman/listinfo/talk
>
>  
>
Wow, ok I did overcomplicate it, the solution was so simple it escaped 
me altogether.

Here is my install.php, I just run this from command line:

[PHP]
<?php

  
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    File: INSTALL.PHP
    Created: 5/18/2004
    Modified: 5/18/2004
    Purpose: This file will be housed in the /docs document folder for 
the IVC project documentation list and not kept inside the document
             root.  It will generate all necessary constants for use 
within the IVC by dynamically generating the necessary content from the
             $argv array values from user input and include them into 
script that will be placed into
             /[your document root]/[your client folder name]/[your 
project folder name]/include/constants.inc.php

    Dependencies:
            Will be called via command-line PHP and can be set up as a 
cron, if so, move to /cron/[client folder name]/[project folder name].
            Takes four required parameters:

                1) client folder name
                2) project folder name
                3) your home directory path where this script resides
                4) the document root (e.g. "/var/www/html")

    Cookies: NONE
    Sessions: NONE
    Privacy Scope: Command-line PHP and not meant to be viewable by web 
server
  
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
  if (!$_SERVER['PHP_SELF'] && $_SERVER['PWD']) {
   $clientFolderName = $argv[1];
   $projectFolderName = $argv[2];
   $installHome = $argv[3];
   $docRoot = $argv[4];
   $includeCode = '<' . "?\n\n define('CLIENT_FOLDER_NAME', 
\"$clientFolderName\");\n define('PROJECT_FOLDER_NAME', 
\"$projectFolderName\");\n\n ?" . '>';
   $fileID = 
@fopen("$docRoot/$clientFolderName/$projectFolderName/include/constants.inc.php", 
'w');
   @fputs($fileID, $includeCode); @fflush($fileID); @fclose($fileID);
  }
?>
[/PHP]

Then once this runs it creates a PHP script within the docroot called 
"constants.inc.php" that is included by "index.php" to, you guessed it, 
define constants.

[from index.php:]
[PHP]
 /*---------------------------------------------------------------------------------------------------------------------------------
    This script will now look for a file called 'constants.inc.php' in 
the /include folder.  This script had to have
    already been generated by the user/admin running "install.php" from 
the command line.
  
----------------------------------------------------------------------------------------------------------------------------------*/ 

  if (!defined('CLIENT_FOLDER_NAME') || !defined('PROJECT_FOLDER_NAME')) 
@include('./include/constants.inc.php');
  if (!defined('CLIENT_FOLDER_NAME') || !defined('PROJECT_FOLDER_NAME')) 
die('You must first run "install.php" from the command line - please see 
admin');
  // BOTH CONSTANTS EXIST AS SET BY install.php - SET TO LOCAL VARIABLES
  list($clientFolderName, $projectFolderName) = 
array(CLIENT_FOLDER_NAME, PROJECT_FOLDER_NAME);
  
  //---END OF CONSTANTS RETRIEVAL 
BLOCK--------------------------------------------------------------------------------------------------------------------------------------------
[/PHP]

*whew* so simple it naturally escaped me.  Thanx
Phil

-- 
---------------------------------------------------------------------------------
Phil Powell
Multimedia Programmer
BPX Technologies, Inc.
#: (703) 709-7218 x107 
Fax: (703) 709-7219

	




More information about the talk mailing list