note 48588 deleted from features.commandline by nlopess
| From: | nlopess@php.net | Date: | Mon, 16 May 2005 13:37:25 +0000 |
| Subject: | note 48588 deleted from features.commandline by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-89488@lists.php.net to get a copy of this message | ||
Note Submitter: Mathieu BOIS
----
Q: How to know whether we're in server or in script mode?
A (my proposal, there may be others, and maybe even a dedicated variable?):
<? if ($_SERVER['SERVER_PORT']) { echo "SERVER MODE"; } else { echo "CLI
MODE"; } ?>
This can be useful, for instance if you want to send by mail a web php page that is also accessible
by a browser.
Using this trick, you can then set a variable to make the links to the right location in all the
relevant href=, src=, action= parameters (and may be other ones too: in stylesheets,
javascripts...).
This needs also to be done in functions from included ("require") php files if they insert
such tags. That's the reason why I've declared all of them as global variables below.
For instance, here is a part of "thisscript.php":
<?
if ($_SERVER['SERVER_PORT'])
{ /* nothing */ }
else
{
$CLI_SERVERROOT= "http://www.myserver.com";
$CLI_URL_DIR=$CLI_SERVERROOT."/path/to/";
$CLI_URL_FULL=$CLI_URL_DIR."thisscript.php";
global $CLI_SERVERROOT,$CLI_URL_DIR,$CLI_URL_FULL;
}
?>
<link href="<? echo $GLOBALS["CLI_SERVERROOT"]; ?>/styles/nice.css"
title="Nice stylesheet" rel="stylesheet" type="text/css">
<a href="<? echo $GLOBALS["CLI_URL_FULL"]; ?>?newparameter=543">chge
param. to this script</a>
<a href="<? echo $GLOBALS["CLI_URL_DIR"]; ?>page2.php">local
page</a>
<img src=<? echo $GLOBALS["CLI_SERVERROOT"]; ?>/icons/fax.png">
<img src=<? echo $GLOBALS["CLI_URL_DIR"]; ?>localimage.png">