Wednesday, June 6, 2012

Learn abour your server: php variables


Learn about your server: PHP variables

The server which is hosting your site (we will suppose in this tutorial you have an account with an ISP, so you are not controlling the server) may provide you  a lot of useful information. In the next table we have a very simple code which will allow us to get a huge amount of information from the server.
 

serverex.php
<form method=post action=serverex.php?a=1&b=2&c=3>
Name: <input type=text name=name><br>
Age: <input type=text name=age><br>
<input type=submit value=submit>
</form>

<hr>

<?php
print phpinfo();
?>

When visiting this page you will find a form in the top an a huge amount of information bellow, but in order to get even more information, fill the form and submit it.  Then we will look to the response page.
 
 

In the response page you will get the PHP version instaled in your server and additional information about the computer and how it has been set up to support .

Additionally, and we will focus on this part of the information provided, in the last part of the response page we will find a section on PHP variables. Depending on the PHP version  installed in the server, we may find slightly different data in that table. We do believe printing this response page is very useful, so we recomment so. 

We have extract one of the lines from the PHP variables obtained from that section of the response page to show you how to use it.
 

PHP_SELF /mydir/serverex.php    

So we have a variable and its associated value (in this case the relative path to our page). 


Let suppose we want to show in our php page that information. For example in the page "serverex.php" above, we want the action of our form to be entered automatically, so that it will work exactly in the same way with a different file name (file1.php, file2.php, etc.). So we want to get this:

<form method=post action="relative path to our script">

We know PHP_SELF variable will provide us that information, so we will make a small transformation of the variable name from PHP_SELF to $PHP_SELF, and we will used the latter one in our php page as in the example bellow:

pagename.php (you may change the name of the file)
<form method=get action=<?php print $PHP_SELF; ?>>
Name: <input type=text name=name><br>
Age: <input type=text name=age><br>
<input type=submit value=submit>
</form>

<hr>

Hi <?php print $_GET["name"]; ?>
I know you are  <?php print $_GET["age"]; ?> years old.
 

So in the response page the form will point back to the same page. Additionally, in this response page we have adding two php codes which will be used to add the information entered in the form within the response page. To learn more about responding to forms you may go to this page.

Depending on the PHP version installed on the server we may used $_GET["name"] or $HTTP_GET_VARS["name"] (in older versions of PHP). Anyway, the result will be the same (just use that one you will find in the list of PHP variables obtained from "askyourserver.php" above).

Selected PHP variables

We have select some PHP variables we consider more likely to be used by a beginner and we have put all them in the script bellow. In the right cell of the table you will find some useful ways to use them.

getphpvariables.php
<form method=post action=<?php print $PHP_SELF; ?>?a=1&b=2&c=3>
Name: <input type=text name=name><br>
Age: <input type=text name=age><br>
<input type=submit value=submit><br>
</form>
Please fill the form and submit it.

<hr>

Lets get some info:
<p>PHP_SELF: <?php print $PHP_SELF; ?>

<p>The value of variable "a" (get method): <?php print $_GET["a"]; ?>
<p>Value of variable "b" (get method): <?php print $_GET["b"]; ?>
<p>Value of variable "c" (get method): <?php print $_GET["c"]; ?>
<p>Value of variable "name" (post method): <?php print $_POST["name"]; ?>
<p>Value of variable "age" (post method): <?php print $_POST["age"]; ?>

<p>Language(s) selected in the browser: <?php print $HTTP_ACCEPT_LANGUAGE; ?>

<p>Host: <?php print $HTTP_HOST; ?>
<p>Referer: <?php print $HTTP_REFERER; ?>
<p>Browser of the visitor: <?php print $HTTP_USER_AGENT; ?>
<p>Path to our file: <?php print $PATH_INFO; ?>
<p>Query string: <?php print $QUERY_STRING; ?>
<p>Remote address: <?php print $REMOTE_ADDR; ?>
<p>Remote host: <?php print $REMOTE_HOST; ?>
<p>Name of script: <?php print $SCRIPT_NAME; ?>
<p>Name of server: <?php print $SERVER_NAME; ?>
<p>Local address: <?php print $LOCAL_ADDR; ?>
<p>Path: <?php print $PATH_TRANSLATED; ?>











Example


Check Get and Post
             here



Languaje specific response

Example comming soon
Example comming soon
Example comming soon
Example comming soon
Example comming soon
Example comming soon
Example comming soon
Example comming soon
Example comming soon
Example comming soon
Example comming soon

Some of the values you will get from this script will be the same.

The data may be obtained in different ways as for example those ones:
  <?php print $QUERY_STRING; ?>
 
<?php print $_SERVER["QUERY_STRING"]; ?>


No comments:

Post a Comment