Showing the Browser and IP Address

Here is a simple page that prints out the browser string and the IP address of the HTTP request. Create a file with the following content in your web directory, name it something like example.php3, and load it in your browser:
<html><head><title>PHP Example</title></head>
<body>
   You are using 
    <?php echo $_SERVER['HTTP_USER_AGENT'] ?>
   <br />
   and coming from 
    <?php echo $_SERVER['REMOTE_ADDR'] ?>
</body></html>
You should see something like the following in your browser window:
You are using Mozilla/5.0 (X11; U; Linux i686; en-US; 
rv:1.1b) Gecko/20020722
and coming from 127.0.0.1

1.13.2 Intelligent Form Handling

Here is a slightly more complex example. We are going to create an HTML form that asks the user to enter a name and select one or more interests from a selection box. We could do this in two files, where we separate the actual form from the data handling code, but instead, this example shows how it can be done in a single file:
<html><head><title>Form Example</title></head>
<body>
<h1>Form Example</h1>
<?
function show_form($first="", $last="", 
                   $interest="") {
 $options = array("Sports", "Business", "Travel", 
                  "Shopping", "Computers");
 if(!is_array($interest)) $interest = array( );
 ?>
 <form action="form.php" method="POST">
 First Name:
 <input type="text" name="first" 
        value="<?echo $first?>">
 <br />
 Last Name:
 <input type="text" name="last" 
        value="<?echo $last?>">
 <br />
 Interests:
 <select multiple name="interest[ ]">
 <?php
  foreach($options as $option) {
   echo "<option";
   if(in_array($option, $interest)) {
    echo " selected ";
   }
   echo "> $option</option>\n";
  }
 ?>
 </select><br />
 <input type=submit>
 </form>
<?php } // end of show_form( ) function

if($_SERVER['REQUEST_METHOD']!='POST') {
 show_form( );
} else {
 if(empty($_POST['first']) || 
    empty($_POST['last'])  ||
    empty($_POST['interest'])) {
  echo "<p>You did not fill in all the fields,";
  echo "please try again</p>\n";
  show_form($_POST['first'],$_POST['last'], 
            $_POST['interest']);
 }
 else {
  echo "<p>Thank you, $_POST[first] $_POST[last], you ";
  echo 'selected '. 
       join(' and ', $_POST['interest']);
  echo " as your interests.</p>\n";
 }
}
?>
</body></html>
There are a few things to study carefully in this example. First, we have isolated the display of the actual form to a PHP function called show_form(). This function is intelligent, in that it can take the default value for each of the form elements as an optional argument. If the user does not fill in all the form elements, we use this feature to redisplay the form with whatever values the user has already entered. This means the user only has to fill the fields he missed, which is much better than asking the user to hit the Back button or forcing him to reenter all the fields.
Related Posts:
  • How to create a thumbnail-PHP code To create a thumbnail, you pass the function PIPHP_MakeThumbnail()a GD image object and the maximum value of the greater dimension for the thumbnail. For example, the following code loads in the image in test.jpgusing the … Read More
  • PHP-HTTP and Sessions-Maintaining State HTTP has no mechanism to maintain state; thus HTTP is a context-free or stateless protocol. Individual requests aren't related to each other. The Web server and thus PHP can't easily distinguish between single users and… Read More
  • PHP-script-Related Variables PHP-script Related Variables PHP automatically creates variables for all the data it receives in an HTTP request. This can include GET data, POST data, cookie data, and environment variables. The variables are e… Read More
  • PHP-Array Functions -list(),each(), and count(). list() is sort of an operator, forming an lvalue (a value that can be used on the left side of an expression) out of a set of variables, which represents itself as a new entity similar to an element of a multidimension… Read More
  • Networking Functions-PHP When using the PHP binaries for Windows that are available from http://php.net/, the getprotobyname(), getprotobynumber(), getservbyport(), and getservbyname() may not function as anticipated under Windows 2000. D… Read More
  • php-Data Types Data Types PHP provides four primitive data types: integers, floating point numbers, strings, and booleans. In addition, there are two compound data types: arrays and objects.  Integers Integers are whole num… Read More
  • Make a PHP session code  Make a PHP session code session_start int session_start(void) Initializes a session. Returns: Always returns TRUE Description: Initializes a session. If a session ID is sent as a GET or in a cookie and is … Read More
  • PHP-Http Environment Variables A Web browser makes a request of a Web server, it sends along with  the request a list of extra variables. These are called environment  variables, and they can be very useful for displaying dynamic content or… Read More
  • PHP-Mail Functions PHP contains two dedicated mail functions, which are built into PHP by default. The mail() function allows for the sending of email directly from a script, and ezmlm_hash() provides a hash calculation useful for interf… Read More
  • PHP-max_execution_time php.ini Directives Related to the Connection-Handling Functions The following configuration directives can be used to control the behavior of the connection-handling functions. Directive Name Value Type Descri… Read More
  • CodeIgniter config.php The  config.php filecontains a series of configuration options all of them stored in a PHP array called, appropriately enough, $config) that CodeIgniter uses to keep track of your application ’ s  information and… Read More
  • Generating a PDF document-PHP Generating a PDF document <?php // These values are in points (1/72nd of an inch) $fontsize = 72; // 1 inch high letters $page_height = 612; // 8.5 inch high page $page_width = 792; // 11 inch wide page … Read More
  • What is an array? An array is a variablethat stores more than onepiece of related data in a single variable. Thinkof an array as a box of chocolateswith slotsinside. The box representsthe arrayitself whilethe spacescontaining chocolates rep… Read More
  • PHP Online Resources The major sites that use PHP, and a listing of all the books written on PHP. Not only does this site contain a plethora of resources, it also contains links to the other PHP sites, the latest news about all things PHP … Read More
  • PHP and Javascript Variables Variables To define a variable in PHP, you’d write:// PHP$n = 1;The equivalent in JavaScript is:// JavaScriptvar n = 1; There’s no dollar sign, just the name of the variable.  Like in PHP, you don’t define variable… Read More