note 48588 added to features.commandline
| From: | MathieuBOIS at osu1 dot php dot net | Date: | Fri, 31 Dec 2004 12:20:04 +0000 |
| Subject: | note 48588 added to features.commandline | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-82502@lists.php.net to get a copy of this message | ||
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">
----
Manual Page -- http://www.php.net/manual/en/features.commandline.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+48588
Delete -- http://master.php.net/manage/user-notes.php?action=delete+48588&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48588&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48588&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48588&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48588&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48588&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+48588&report=yes
Search -- http://master.php.net/manage/user-notes.php