darkchest
October 10th, 2009, 08:52 PM
Hi all,
I installed php, apache and mysql on my ubuntu laptop and I am going thru some tutorial files. I run one of the php files on my localhost but it does not return any variables as explained.
The code is as below:
================================================== =======
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF">
<table width="95%" border="1" bordercolorlight="#0000CC" bordercolordark="#0000CC">
<tr>
<td width="30%" valign="top" bgcolor="EEEEFF">
<p><font face="Arial, Helvetica, sans-serif"><b>Code File 02 01-2 - External
Variables</b></font></p>
<p><font face="Arial, Helvetica, sans-serif" size="-1"><b>Scenario:</b></font></p>
<p><font face="Arial, Helvetica, sans-serif" size="-1">You need to gather
variables from the user's request, the HTTP request, or from the environment
to manage interactions with the user. The solution demonstrates how to
use external variables available in Php to access these variables.</font></p>
<p><font face="Arial, Helvetica, sans-serif" size="-1"><br>
<b>Coding Solution:</b></font></p>
<p><font face="Arial, Helvetica, sans-serif" size="-1">Php automatically
provides several external variables in the form of SuperGlobal arrays
(SuperGlobals are variables that can automatically be accessed from anywhere
in a script without identifying them as Global variables). The names/methods
used to access these variables has changed in more recent versions of
Php.</font></p>
<p><font face="Arial, Helvetica, sans-serif" size="-1">In order to demonstrate
accessing these variables, we'll simply print them to the screen using
the print_r function, along with HTML <PRE> tags to preserve the
formatting and spacing. Like any other array, Php variables that are arrays
can be accessed by their names and any index or name associated with a
particular slot in the array.</font></p>
<p><font face="Arial, Helvetica, sans-serif" size="-1"><b>Code:</b></font></p>
<table width="100%" border="1" cellpadding="2">
<tr>
<td> <code> echo "<pre>";<br>
print_r($GLOBALS);<br>
echo "</pre>";</code></td>
</tr>
</table>
<p><font face="Arial, Helvetica, sans-serif" size="-1">Php <pre> provides
the following external variables as SuperGlobals:</font></p>
<ul>
<li><font face="Arial, Helvetica, sans-serif" size="-1">$GLOBALS (part
of Php since Php 3.0) This displays all the arrays for all the variables
that are available within the global scope of the script you are running.</font></li>
<li><font face="Arial, Helvetica, sans-serif" size="-1">$_SERVER (use
$HTTP_SERVER_VARS for older versions of Php). Includes variables from
the Web server, such as CONTENT_LENGTH, HTTP_HOST, and so forth.</font></li>
<li><font face="Arial, Helvetica, sans-serif" size="-1">$_GET (use $HTTP_GET_VARS).
Includes variables provided as part of a query string.</font></li>
<li><font face="Arial, Helvetica, sans-serif" size="-1">$_POST (use $HTTP_POST_VARS).
Includes variables provided as part of a form post.</font></li>
<li><font face="Arial, Helvetica, sans-serif" size="-1"> $_COOKIE (use
$HTTP_COOKIE_VARS). Includes cookies sent with a request.</font></li>
<li><font face="Arial, Helvetica, sans-serif" size="-1">$_FILES (use HTTP_POST_FILES).
Includes variables from post file uploads. </font></li>
<li><font face="Arial, Helvetica, sans-serif" size="-1">$_ENV (use $HTTP_ENV_VARS).
Includes variables from the environment, such as PATH. </font></li>
<li><font face="Arial, Helvetica, sans-serif" size="-1">$_REQUEST Includes
variables provided by any user input method. There is no similar array
available before Php 4.1.0. </font></li>
<li><font face="Arial, Helvetica, sans-serif" size="-1">$_SESSION (use
$HTTP_SESSION_VARS). Includes variables that are part of the current
user session</font></li>
</ul>
</td>
<td width="70%" valign="top">
<form method="post" action="0201_2.php">
<font face="Arial, Helvetica, sans-serif" size="-1">
<input type="hidden" name="posted" value="True">
</font>
<p><font face="Arial, Helvetica, sans-serif" size="-1">Please pick an
external variable to display:
<select name="external_variable">
<option value="GLOBALS">GLOBALS</option>
<option value="_SERVER">_SERVER</option>
<option value="_GET">_GET</option>
<option value="_POST">_POST</option>
<option value="_COOKIE">_COOKIE</option>
</select>
</font></p>
<p> <font face="Arial, Helvetica, sans-serif" size="-1">
<input type="submit" name="pick" value="Pick">
</font></p>
</form>
<font face="Arial, Helvetica, sans-serif"><?
if (isset($posted)) {
if ($external_variable == "GLOBALS") {
echo "<PRE>";
print_r($GLOBALS);
echo "</PRE>";
} elseif ($external_variable == "_SERVER") {
echo "<PRE>";
print_r($_SERVER);
echo "</PRE>";
} elseif ($external_variable == "_GET") {
echo "<PRE>";
print_r($_GET);
echo "</PRE>";
} elseif ($external_variable == "_POST") {
echo "<PRE>";
print_r($_POST);
echo "</PRE>";
} elseif ($external_variable == "_COOKIE") {
echo "<PRE>";
print_r($_COOKIE);
echo "</PRE>";
}
}
?></font></td>
</tr>
<tr>
<td width="30%" valign="top" bgcolor="EEEEFF"><img src="spacer.gif" width="360" height="1"></td>
<td width="70%" valign="top"> </td>
</tr>
</table>
</body>
</html>
================================================== ==
Can you please tell me why? I checked to see if my server was started and it was. Thank you.
I installed php, apache and mysql on my ubuntu laptop and I am going thru some tutorial files. I run one of the php files on my localhost but it does not return any variables as explained.
The code is as below:
================================================== =======
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF">
<table width="95%" border="1" bordercolorlight="#0000CC" bordercolordark="#0000CC">
<tr>
<td width="30%" valign="top" bgcolor="EEEEFF">
<p><font face="Arial, Helvetica, sans-serif"><b>Code File 02 01-2 - External
Variables</b></font></p>
<p><font face="Arial, Helvetica, sans-serif" size="-1"><b>Scenario:</b></font></p>
<p><font face="Arial, Helvetica, sans-serif" size="-1">You need to gather
variables from the user's request, the HTTP request, or from the environment
to manage interactions with the user. The solution demonstrates how to
use external variables available in Php to access these variables.</font></p>
<p><font face="Arial, Helvetica, sans-serif" size="-1"><br>
<b>Coding Solution:</b></font></p>
<p><font face="Arial, Helvetica, sans-serif" size="-1">Php automatically
provides several external variables in the form of SuperGlobal arrays
(SuperGlobals are variables that can automatically be accessed from anywhere
in a script without identifying them as Global variables). The names/methods
used to access these variables has changed in more recent versions of
Php.</font></p>
<p><font face="Arial, Helvetica, sans-serif" size="-1">In order to demonstrate
accessing these variables, we'll simply print them to the screen using
the print_r function, along with HTML <PRE> tags to preserve the
formatting and spacing. Like any other array, Php variables that are arrays
can be accessed by their names and any index or name associated with a
particular slot in the array.</font></p>
<p><font face="Arial, Helvetica, sans-serif" size="-1"><b>Code:</b></font></p>
<table width="100%" border="1" cellpadding="2">
<tr>
<td> <code> echo "<pre>";<br>
print_r($GLOBALS);<br>
echo "</pre>";</code></td>
</tr>
</table>
<p><font face="Arial, Helvetica, sans-serif" size="-1">Php <pre> provides
the following external variables as SuperGlobals:</font></p>
<ul>
<li><font face="Arial, Helvetica, sans-serif" size="-1">$GLOBALS (part
of Php since Php 3.0) This displays all the arrays for all the variables
that are available within the global scope of the script you are running.</font></li>
<li><font face="Arial, Helvetica, sans-serif" size="-1">$_SERVER (use
$HTTP_SERVER_VARS for older versions of Php). Includes variables from
the Web server, such as CONTENT_LENGTH, HTTP_HOST, and so forth.</font></li>
<li><font face="Arial, Helvetica, sans-serif" size="-1">$_GET (use $HTTP_GET_VARS).
Includes variables provided as part of a query string.</font></li>
<li><font face="Arial, Helvetica, sans-serif" size="-1">$_POST (use $HTTP_POST_VARS).
Includes variables provided as part of a form post.</font></li>
<li><font face="Arial, Helvetica, sans-serif" size="-1"> $_COOKIE (use
$HTTP_COOKIE_VARS). Includes cookies sent with a request.</font></li>
<li><font face="Arial, Helvetica, sans-serif" size="-1">$_FILES (use HTTP_POST_FILES).
Includes variables from post file uploads. </font></li>
<li><font face="Arial, Helvetica, sans-serif" size="-1">$_ENV (use $HTTP_ENV_VARS).
Includes variables from the environment, such as PATH. </font></li>
<li><font face="Arial, Helvetica, sans-serif" size="-1">$_REQUEST Includes
variables provided by any user input method. There is no similar array
available before Php 4.1.0. </font></li>
<li><font face="Arial, Helvetica, sans-serif" size="-1">$_SESSION (use
$HTTP_SESSION_VARS). Includes variables that are part of the current
user session</font></li>
</ul>
</td>
<td width="70%" valign="top">
<form method="post" action="0201_2.php">
<font face="Arial, Helvetica, sans-serif" size="-1">
<input type="hidden" name="posted" value="True">
</font>
<p><font face="Arial, Helvetica, sans-serif" size="-1">Please pick an
external variable to display:
<select name="external_variable">
<option value="GLOBALS">GLOBALS</option>
<option value="_SERVER">_SERVER</option>
<option value="_GET">_GET</option>
<option value="_POST">_POST</option>
<option value="_COOKIE">_COOKIE</option>
</select>
</font></p>
<p> <font face="Arial, Helvetica, sans-serif" size="-1">
<input type="submit" name="pick" value="Pick">
</font></p>
</form>
<font face="Arial, Helvetica, sans-serif"><?
if (isset($posted)) {
if ($external_variable == "GLOBALS") {
echo "<PRE>";
print_r($GLOBALS);
echo "</PRE>";
} elseif ($external_variable == "_SERVER") {
echo "<PRE>";
print_r($_SERVER);
echo "</PRE>";
} elseif ($external_variable == "_GET") {
echo "<PRE>";
print_r($_GET);
echo "</PRE>";
} elseif ($external_variable == "_POST") {
echo "<PRE>";
print_r($_POST);
echo "</PRE>";
} elseif ($external_variable == "_COOKIE") {
echo "<PRE>";
print_r($_COOKIE);
echo "</PRE>";
}
}
?></font></td>
</tr>
<tr>
<td width="30%" valign="top" bgcolor="EEEEFF"><img src="spacer.gif" width="360" height="1"></td>
<td width="70%" valign="top"> </td>
</tr>
</table>
</body>
</html>
================================================== ==
Can you please tell me why? I checked to see if my server was started and it was. Thank you.