Introduction to PHP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Introduction to PHP

    What is PHP?
    PHP (recursive acronym for "PHP: Hypertext Preprocessor") is a widely-used Open Source general-purpose scripting language that is especially suited for Web/Wap development and can be embedded into HTML/WML.

    here is an exmple:
    HTML Example:
    Code:
    <html>
        <head>
            <title>Example</title>
        </head>
        <body>
    
            <?php 
            echo "Hi, I&#39;m a PHP script!"; 
            ?>
    
        </body>
    </html>
    Notice how this is different from a script written in other languages like Perl or C -- instead of writing a program with lots of commands to output HTML, you write an HTML script with some embedded code to do something (in this case, output some text). The PHP code is enclosed in special start and end tags that allow you to jump into and out of "PHP mode".
    The best things in using PHP are that it is extremely simple for a newcomer, but offers many advanced features for a professional programmer. Don&#39;t be afraid reading the long list of PHP&#39;s features. You can jump in, in a short time, and start writing simple scripts in a few hours.
    ---------------
    A simple tutotial: (Your First PHP Enabled page)

    Here we would like to show the very basics of PHP in a short, simple tutorial. This text only deals with dynamic webpage creation with PHP, though PHP is not only capable of creating webpages.
    PHP-enabled web pages are treated just like regular HTML/WML pages and you can create and edit them the same way you normally create regular HTML/WML pages.

    Our first php script hello.php
    HTML:
    Code:
    <html>
    <head>
      <title>PHP Test</title>
    </head>
    <body>
    <?php echo &#39;
    
    Hello World I am a PHP script</p>&#39;; ?>
    </body>
    </html>
    WML Example:
    Code:
    <?php
    {
    header (&#39;Content-Type: text/vnd.wap.wml&#39;);
    echo &#39;<xml version="1.0"?&#39;, &#39;>
    <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
    <wml>&#39;;
    }
    {
    echo &#39;
    <card id="main" title="phptest">
    
    
    Hello World I am a PHP script</p>
    </card>&#39;;
    }
    {
    echo &#39;
    </wml>&#39;;
    }
    ?>
    Note that this is not like a CGI script. The file does not need to be executable or special in any way. Think of it as a normal HTML/WML file which happens to have a set of special tags available to you that do a lot of interesting things. Wink
    If you tried this example and it did not output anything, it prompted for download, or you see the whole file as text, chances are that the server you are on does not have PHP enabled. Ask your administrator to enable it for you.
    The point of the example is to show the special PHP tag format. In this example we used <?php to indicate the start of a PHP tag. Then we put the PHP statement and left PHP mode by adding the closing tag, ?>. You may jump in and out of PHP mode in an HTML/WML file like this all you want.

    Now that you have successfully created a working PHP script, it is time to create the most famous PHP script! Make a call to the phpinfo() function and you will see a lot of useful information about your system and setup such as available predefined variables, loaded PHP modules, and configuration settings. Take some time and review this important information.
    -------
    Let us do something more useful now. We are going to check what sort of browser the visitor is using. For that, we check the user agent string the browser sends as part of the HTTP request. This information is stored in a variable. Variables always start with a dollar-sign in PHP. The variable we are interested in right now is $_SERVER[&#39;HTTP_USER_AGENT&#39;].

    Note: $_SERVER is a special reserved PHP variable that contains all web server information. It is known as an autoglobal (or superglobal). These special variables were introduced in PHP 4.1.0. Before this time, we used the older $HTTP_*_VARS arrays instead, such as $HTTP_SERVER_VARS. Although deprecated, these older variables still exist.

    To display this variable, you can simply do:
    Code:
    <?php 
    echo $_SERVER[&#39;HTTP_USER_AGENT&#39;];
    ?>
    A sample output of this script may be:
    Mozilla/4.0 (compatible;MSIE 5.01; Windows NT 5.0)

    There are many types of variables available in PHP. In the above example we printed an Array element. Arrays can be very useful.
    $_SERVER is just one variable that PHP automatically makes available to you.
    -------
    Show all predefined variables with phpinfo()
    Code:
    <?php
    phpinfo(); 
    ?>
    When you load up this file in your browser, you will see a page full of information about PHP along with a list of all the variables available to you.

    You can put multiple PHP statements inside a PHP tag and create little blocks of code that do more than just a single echo. For example, if you want to check for Internet Explorer you can do this:
    Using Control Stuctures and functions:
    Code:
    <?php
    if (strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;], &#39;MSIE&#39;) !== false) {
        echo &#39;You are using Internet Explorer
    &#39;;
    }
    ?>
    A sample output of this script may be:
    You are using Internet Explorer

    Here we introduce a couple of new concepts. We have an if statement. If you are familiar with the basic syntax used by the C language, this should look logical to you. Otherwise, you should probably pick up any introductory PHP book and read the first couple of chapters. You can find a list of php books at http://php.net/books.php

    The second concept we introduced was the strpos() function call. strpos() is a function built into PHP which searches a string for another string. In this case we are looking for &#39;MSIE&#39; (so-called needle) inside $_SERVER[&#39;HTTP_USER_AGENT&#39;] (so-called haystack). If the needle is found inside the haystack, the function returns the position of the needle relative to the start of the haystack. Otherwise, it returns FALSE. If it does not return FALSE, the if expression evaluates to TRUE and the code within its {braces} is executed. Otherwise, the code is not run. Feel free to create similar examples, with if, else, and other functions.
    ---------
    We can take this a step further and show how you can jump in and out of PHP mode even in the middle of a PHP block:

    Mixing HTML and PHP modes
    Code:
    <?php
    if (strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;], &#39;MSIE&#39;) !== false) {
    ?>
    <h3>strpos must have returned non-false</h3>
    <center>[b]You are using Internet Explorer[/b]</center>
    <?php
    } else {
    ?>
    <h3>strpos must have returned false</h3>
    <center>[b]You are not using Internet Explorer[/b]</center>
    <?php
    }
    ?>
    Instead of using a PHP echo statement to output something, we jumped out of PHP mode and just sent straight HTML. The important and powerful point to note here is that the logical flow of the script remains intact. Only one of the HTML blocks will end up getting sent to the viewer depending on the result of strpos(). In other words, it depends on whether the string MSIE was found or not.
    --------

    Thats it for now, i will get more into detail on php functions and jumping in and out of php...

    #2
    You can use the same code as above to redirect or to deny access to a certain page on your site. for example if you coding a wap site with downloads but you do not want web browsers, or pc uses to access that particular page, you can do this
    Code:
    if (strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;], &#39;Opera&#39;) !==false || strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;], &#39;MSIE&#39;) !==false || strpos($_SERVER[&#39;$HTTP_USER_AGENT&#39;], &#39;Mozilla&#39;) !==false || strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;], &#39;Go!Zilla&#39;) !==false || strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;], &#39;WinWap&#39;) !==false)
    { header(&#39;Location: [url]http://redirected_url&#39;[/url]); }
    I will continue later

    Comment


      #3
      Simple Quick Tutorial Sript

      <?php
      //SEND THE WML HEADERS
      header("Content-type: text/vnd.wap.wml");
      //SET THE CURRENT SERVER TIME
      $time = time();
      //START OF WML OUTPUT
      ?>
      <wml>
      <card id="card0" title="TITLE HERE">
      <p align="center">


      Today is:

      <?php
      //PRINT THE TIME AND DATE
      print "" . date("j of F Y, \a\\t g.i a", $time) . "";
      print "
      Your Browser Is:
      ";
      //PRINT THE BROWER INFO
      print "$HTTP_USER_AGENT
      ";
      print "Your Ip is:
      ";
      //PRINT REMOTE IP ADDRESS OF THE DEVICE
      print "$REMOTE_ADDR";
      ?>

      ------

      You Could Put Your Content Here

      Or maybe an enter link

      Enter

      [Sitename]
      </p>
      </card>
      </wml>

      Comment


        #4
        can u tell me where to download a php tutorial software?

        Comment


          #5
          Hi friends , now have a look at my custom functions tutorial.

          QUICK DEFINITIONS:

          VARIABLES:
          1.have a temporary value
          2.must not start with digits/numbers
          3.can be any value , numeric , chars
          4.starts with a dollar sign
          5.USAGE: $a="hello";echo $a; output:hello

          CONSTANTS:
          1.have permanent value
          2.must not start with digits/numbers
          3.can be any value , numeric , chars
          5.USAGE: system(&#39;a&#39;="hello&#39;);echo a; output:hello

          ARRAYS
          1.STORES IN A VARIABLE USUALLY
          2.HAVE MULTIPLE VALUES
          3.VALUES ARE TEMPORARY
          4.USAGE: $a=array(); $a[0]="h";$a[1]="e";$a[3]="ll";$a[4]="0"; echo $a[1]; output:h
          5.WISE USAGE: for($y=0;$a[$y]!=null;$y++){$c=$y-1;echo $a[$c];} output: hello

          6:THERE ARE 3 TYPES OF ARRAYS IN PHP,
          a.normal array
          b.associative array
          c.multidimensional array

          ASSOCIATIVE ARRAYS:
          4.USAGE: $a=array(); $a[a]="h";$a[-b]="e";$a[c]="ll";$a[d]="0"; echo $a[a]; output:h
          SAME AS NORMAL ARRAYS BUT THE ARRAY KEY , THE ONE IN BRACKETS ([1]) is a char.

          MULTIDIMENSIONAL ARRAYS:
          I&#39;ll come to you with this later...now only these are needed.

          OPERATORS:
          == equals
          if(1==1){//dosomething}

          != notequals
          if(1!=1){//dosomething}

          or
          if(1=="1"or(a==1)){//dosomething}

          || same as or
          if(1=="1"||a==1){//dosomething}

          && and
          if(1=="1"&&(a==1)){//dosomething}

          and and
          if(1=="1"and(a==1)){//dosomething}

          ++
          value+1 , example : $c=1;$c++;echo $c; output:2

          --
          value-1 , example : $c=2;$c--;echo $c; output:1

          -
          MINUS example : $c=2; $b=1; $d=$c-$b; echo $d; output:1

          +
          PLUS example : $c=2; $b=1; $d=$c+$b; echo $d; output:3

          *
          INTO example : $c=2; $b=1; $d=$c*$b; echo $d; output:2

          /
          DIVIDE example : $c=2; $b=1; $d=$c/$b; echo $d; output:2

          %
          MODULUS - DIVIDE ONE VALUE BY OTHER AND RETURN THE REMINDER:
          example : $c=2; $b=1; $d=$c%$b; echo $d; output:0 or null

          ===
          IDENTICAL TO

          !===
          NOT IDENTICAL TO

          <
          less than

          >greater than

          <=
          less than or equal to

          >=
          greater than or equal to

          !
          NOT

          Xor
          checks if only one of two statements are true

          +=
          ADD one value to other
          example: $a="1";$a+=1; echo $a; output:2

          -=
          SUB&#39;S one value to other
          example: $a="2";$a-=1; echo $a; output:1

          *=
          MULTIPLIES one value to other
          example: $a="1";$a*=1; echo $a; output:1

          /=
          DIVIDES one value to other
          example: $a="1";$a+/1; echo $a; output:1

          .=
          COMBINES TWO VALUES
          example $a="he"; $a.="llo"; echo $a; output:hello

          -------


          +=
          ADD one value to other
          example: $a="1";$a+=1; echo $a; output:2



          Creating functions
          ----
          *BASIC STATEMENTS:
          Php supports custom functions.
          These functions are executed in the script itself and doesent store in header files...etc..
          These functions are just defined in a PHP script.
          Functions cant process outside variables/constants but can be done with the global keyword.
          Functions can return a value.
          Functions are the simplest way to work a task though OOP is available.

          You can create a function with the function keyword and the function name.

          Code:
          <?
          
          //Creating functions , naveen.
          
          function myfunction()
          {
          //function programs-statements...
          }
          
          ?>
          Every function must end with a &#39)&#39;.
          Whether it is a custom defined function or supplied function.
          After you create a function , function statements must be created in the &#39;{}&#39; braces.

          Test a function.
          Code:
          <?
          //My first created function.
          function sample()
          {
          $a="Hello";
          $b="{$a} World!";
          $c=$b;
          echo $c;
          }
          //test the function
          sample();
          ?>
          ##########OUTPUT##########
          Hello World!
          #########################

          Now you know to create a function.
          Now lets pass arguments to our new function.

          ---

          Now have a deep look at this program...
          Code:
          <?
          //Test a var function.
          $test="Hello World!";
          function sample()
          {
          echo $test;
          }
          sample();
          unset($test);
          ?>
          You would have thought this would print the value of $test variable,
          As acidburn thought u all about variables , whats a varvalue, constant etc...
          but this THIS WILL NOT PRINT THE VALUE OF $test .
          Why?
          come&#39;on man , i&#39;ll explain it howdy...
          first we&#39;ll have a look what it&#39;ll gonna echo to the browser...


          Code:
          #########OUTPUT#########
          
          #######################
          Huh? wots wrong? there&#39;s nothing!
          Yeah cuz sample() doesnt know wot $test is.
          Thik that $test is in your house and ur neighbour dosent know about it.
          Then what would ur poor neighbour do? nothing right? cuz he doesent know anything abt it...
          Now lets tell our neighbour about $test with our global keyword.
          Code:
          <?
          //Test a var function.
          $test="Hello World!";
          function sample()
          {
          global $test;
          echo $test;
          }
          sample();
          unset($test);
          ?>
          ###########OUTPUT#######
          Hello World!
          #######################
          Yeah , now global keyword told our neighbour about $test and they printed value of it.
          Just remember when the script is executed , the var and its values are remembered till the entire script
          is executed.When entire script is executed , $test is remembered global enables the var name and value
          of $test.
          Echo prints $test.
          Thus , Hello World! is displayed.

          Unset??
          yeah , unset() is a function to delete a variable.
          Try this ,
          Code:
          <?
          //Test a var function.
          $test="Hello World!";
          function sample()
          {
          global $test;
          echo $test;
          }
          unset($test);
          sample();
          ?>
          Nothing will be printed cuz unset deleted $test before sample() was executed.
          These readymade functions are stored in php&#39;s header files,
          you can even create one yourself.

          For example strlen() functions prints the string length.
          Code:
          <?
          //My string length function usage
          $a="Hello";
          //test string 
          function teststring()
          {
          global $a;
          $b=strlen($a);
          echo "The string &#39;{$a}&#39; contains {$b} chars.";
          }
          teststring();
          ?>
          ########OUTPUT#########
          The string &#39;Hello&#39; contains 5 chars.
          ######################
          Although you can create it urself.
          Code:
          <?
          $a="Hello";
          function teststring()
          {
          global $a;
          //test each char in array $a
          for($y=0;$a[$y]!=null;$y++)
          {
          }
          //Now , 
          $stringlength=$y;
          echo "The string &#39;{$a}&#39; contains {$stringlength} chars.";
          }
          teststring();
          ?>
          First $a is denoted a value "Hello",
          Then function teststring is defined.
          Now , global has been done to know what $a is.
          Now through loop , $y has been defined as zero , as $a is an array of chars , array of $a with $y should
          not be null , if it is , loop is ended , if not $y is added once.
          This loop goes on until $y&#39;s value is 0 or null.
          Now as meaningful var rule , $stringlength has been duplicated as $y.
          Now , its printed in a meaningful manner.
          Thus , we have created our new custom teststring function.

          -----
          Now lets go to arguments.
          Each and everytime we cant denote a var value and retrive it through global,
          There&#39;s an easy and usefull way to do that and it is called as ARGUMENTS.
          Code:
          <?
          //my first argument function
          function sample($a)
          {
          if(isset($a))
          {
          echo "Your argument was {$a}";
          }
          else
          {
          echo "Argument value is null";
          }
          //end function
          }
          sample(&#39;Hello&#39;);
          ?>
          #########OUTPUT#######
          Your argument was Hello
          #####################
          Now i&#39;ll explain it.
          First we create a function called &#39;sample&#39;.
          Now , we place the argument value var as $a.
          Any arguments passed (ex. sample(&#39;abc&#39;))
          will b stored in $a variable.
          Now when we print a&#39;s value , the argument stored in that variable is printed.

          Now lets oppsitely test it...
          Code:
          <?
          //my first argument function
          function sample($a)
          {
          if(isset($a))
          {
          echo "Your argument was {$a}";
          }
          else
          {
          echo "Argument value is null";
          }
          //end function
          }
          sample();
          ?>
          Now if you have E_WARNING:on in the php.ini file ,
          the output would be something like this...
          ########OUTPUT#########
          [b]Warning:[/b] Missing argument 1 for sample() in /ur directories/sample.php on line 3
          Argument value is null
          ######################
          if it was E_WARNING:off in the php.ini file ,
          then the output would be..
          ########OUTPUT#########
          Argument value is null
          ######################
          Congrats! you have created your first argument function!

          ----Return a value from a function----

          Code:
          <?
          //create a return function
          function sample($a)
          {
          //test argument a is a set or not
          if(isset($a))
          {
          //if yes, assign b as..
           $b="Returned ! , value is {$a} ";
          }
          else
          {
          //assign b as...
          $b="Returned !, value is null.";
          }
          //return b
          return $b;
          }
          //store function return in c
          $c=sample(&#39;hello&#39;);
          //print c
          echo "$c";
          ?>
          ########OUTPUT########
          Returned!,value is Hello
          #####################
          Return returns the value/data if a variable/constant/function.
          Now the value of $a will be stored in $b.
          As a result , Hello will be echod.

          Lets test it in another way...
          Code:
          <?
          //create a return function
          function sample($a)
          {
          //test argument a is a set or not
          if(isset($a))
          {
          //if yes, assign b as..
           $b="Returned ! , value is {$a} ";
          }
          else
          {
          //assign b as...
          $b="Returned , value is null.";
          }
          //return b
          return $b;
          }
          //store function return in c
          $c=sample();
          //print c
          print "$c";
          ?>
          Now if you have E_WARNINGS:on in the php.ini file ,
          the output would be something like this...
          ##########OUTPUT##########
          Warning: Missing argument 1 for sample() in URDIRECTORIES/sample.php on line 2
          Returned , value is null.
          #########################

          Now , as far as u have learnt , we can create an all time string length tool.
          Code:
          <?
          <?
          $b=$_GET[&#39;word&#39;];
          $type=$_GET[&#39;type&#39;];
          //create a proper value
          $a=strtolower($b);
          function teststring($string)
          {
          //test each char in array $a
          for($y=0;$string[$y]!=null;$y++)
          {
          }
          //Now , 
          $stringlength=$y;
          echo "The string &#39;{$string}&#39; contains {$stringlength} chars.-TESTSTRING METHOD";
          }
          if(isset($a,$type))
          {
          if($type=="teststring")
          { 
          teststring($a);
          }
          elseif($type=="strlen")
          {
          $b=strlen($a);
          echo "The string &#39;{$a}&#39; has {$b} chars.-STRLEN METHOD";
          }
          else
          {
          echo "INCORRECT METHOD!
          Correct methods:
          1.strlen
          2.teststring
          ";
          }
          }
          else
          {
          echo"Please specify all details! You have not specified the word or the type or both.";
          } 
          ?>
          Now we have created our strlength finder script.
          Case can be capital or small or both , it&#39;ll be converted to lower with strtolower();
          It will run with a BOOLEAN statement.
          Functions are made.
          Value is given from GET METHOD.

          Now to test this , point this_filename.php?word=wordname&type=urtype .

          Lets move on...
          ------------------

          TIP: If you are creating a complex web/wap applications,
          Then u have to define many functions in a specific file and then have to include
          for furthor use. k:




          ------------------
          Next...
          -BASIC PHP
          -ARRAYS
          -WORKING WITH WML
          -FILESYSTEM
          -STRINGS
          -DATABASE-MySQL

          Should i continue with these?
          Please post / pm me ANYONE , or GUMSLONE....

          #########ANY ERRORS / BUGS / COMMENTS CAN BE POSTED OR PM&#39;D##############
          Thanks,
          naveen.

          Comment


            #6
            Web Techniques

            PHP was designed as a web scripting language and, although it is possible to use it in purely command-line and GUI scripts, the Web accounts for the vast majority of PHP uses. A dynamic web site may have forms, sessions, and sometimes redirection, and this explains how to implement those things in PHP. You&#39;ll learn how PHP provides access to form parameters and uploaded files, how to send cookies and redirect the browser, how to use PHP sessions, and more.

            HTTP Basics

            The web runs on HTTP, the HyperText Transfer Protocol. This protocol governs how web browsers request files from web servers and how the servers send the files back. To understand the various techniques we&#39;ll show you in this chapter, you need to have a basic understanding of HTTP. For a more thorough discussion of HTTP, see the HTTP Pocket Reference, by Clinton Wong (O&#39;Reilly).
            When a web browser requests a web page, it sends an HTTP request message to a web server. The request message always includes some header information, and it sometimes also includes a body. The web server responds with a reply message, which always includes header information and usually contains a body. The first line of an HTTP request looks like this:
            GET /index.html HTTP/1.1
            This line specifies an HTTP command, called a method , followed by the address of a document and the version of the HTTP protocol being used. In this case, the request is using the GET method to ask for the index.html document using HTTP 1.1. After this initial line, the request can contain optional header information that gives the server additional data about the request. For example:
            User-Agent: Mozilla/5.0 (Windows 2000; U) Opera 6.0 [en]
            Accept: image/gif, image/jpeg, text/*, */*
            The User-Agent header provides information about the web browser, while the Accept header specifies the MIME types that the browser accepts. After any headers, the request contains a blank line, to indicate the end of the header section. The request can also contain additional data, if that is appropriate for the method being used (e.g., with the POST method, as we&#39;ll discuss shortly). If the request doesn&#39;t contain any data, it ends with a blank line.
            The web server receives the request, processes it, and sends a response. The first line of an HTTP response looks like this:
            HTTP/1.1 200 OK
            This line specifies the protocol version, a status code, and a description of that code. In this case, the status code is "200", meaning that the request was successful (hence the description "OK"). After the status line, the response contains headers that give the client additional information about the response. For example:
            Date: Sat, 26 Jan 2002 20:25:12 GMT
            Server: Apache 1.3.22 (Unix) mod_perl/1.26 PHP/4.1.0
            Content-Type: text/html
            Content-Length: 141
            The Server header provides information about the web server software, while the Content-Type header specifies the MIME type of the data included in the response. After the headers, the response contains a blank line, followed by the requested data, if the request was successful.
            The two most common HTTP methods are GET and POST. The GET method is designed for retrieving information, such as a document, an image, or the results of a database query, from the server. The POST method is meant for posting information, such as a credit-card number or information that is to be stored in a database, to the server. The GET method is what a web browser uses when the user types in a URL or clicks on a link. When the user submits a form, either the GET or POST method can be used, as specified by the method attribute of the form tag.

            Variables

            Server configuration and request information?including form parameters and cookies?are accessible in three different ways from your PHP scripts, as described in this section. Collectively, this information is referred to as EGPCS (environment, GET, POST, cookies, and server).
            If the register_globals option in php.ini is enabled, PHP creates a separate global variable for every form parameter, every piece of request information, and every server configuration value. This functionality is convenient but dangerous, as it lets the browser provide initial values for any of the variables in your program.

            Regardless of the setting of register_globals, PHP creates six global arrays that contain the EGPCS information.
            The global arrays are:

            $HTTP_COOKIE_VARS
            Contains any cookie values passed as part of the request, where the keys of the array are the names of the cookies

            $HTTP_GET_VARS
            Contains any parameters that are part of a GET request, where the keys of the array are the names of the form parameters

            $HTTP_POST_VARS
            Contains any parameters that are part of a POST request, where the keys of the array are the names of the form parameters

            $HTTP_POST_FILES
            Contains information about any uploaded files

            $HTTP_SERVER_VARS
            Contains useful information about the web server, as described in the next section

            $HTTP_ENV_VARS
            Contains the values of any environment variables, where the keys of the array are the names of the environment variables
            Because names like $HTTP_GET_VARS are long and awkward to use, PHP provides shorter aliases: $_COOKIE, $_GET, $_POST, $_FILES, $_SERVER, and $_ENV. These variables are not only global, but also visible from within function definitions, unlike their longer counterparts. These short variables are the recommended way to access EGPCS values. The $_REQUEST array is also created by PHP if the register_globals option is on; however, there is no corresponding $HTTP_REQUEST_VARS array. The $_REQUEST array contains the elements of the $_GET, $_POST, and $_COOKIE arrays.
            PHP also creates a variable called $PHP_SELF, which holds the name of the current script, relative to the document root (e.g., /store/cart.php). This value is also accessible as $_SERVER[&#39;PHP_SELF&#39;].

            Server Information

            The $_SERVER array contains a lot of useful information from the web server. Much of this information comes from the environment variables required in the CGI specification (http://hoohoo.ncsa.uiuc.edu/cgi/env.html).
            Here is a complete list of the entries in $_SERVER that come from CGI:

            SERVER_SOFTWARE
            A string that identifies the server (e.g., "Apache/1.3.22 (Unix) mod_perl/1.26 PHP/4.1.0").

            SERVER_NAME
            The hostname, DNS alias, or IP address for self-referencing URLs (e.g., "www.example.com").

            GATEWAY_INTERFACE
            The version of the CGI standard being followed (e.g., "CGI/1.1").

            SERVER_PROTOCOL
            The name and revision of the request protocol (e.g., "HTTP/1.1").

            SERVER_PORT
            The server port number to which the request was sent (e.g., "80").

            REQUEST_METHOD
            The method the client used to fetch the document (e.g., "GET").

            PATH_INFO
            Extra path elements given by the client (e.g., "/list/users").

            PATH_TRANSLATED
            The value of PATH_INFO, translated by the server into a filename (e.g., "/home/httpd/htdocs/list/users").

            SCRIPT_NAME
            The URL path to the current page, which is useful for self-referencing scripts (e.g., "/~me/menu.php").

            QUERY_STRING
            Everything after the ? in the URL (e.g., "name=Fred+age=35").

            REMOTE_HOST
            The hostname of the machine that requested this page (e.g., "dialup-192-168-0-1.example.com"). If there&#39;s no DNS for the machine, this is blank and REMOTE_ADDR is the only information given.

            REMOTE_ADDR
            A string containing the IP address of the machine that requested this page (e.g., "192.168.0.250").

            AUTH_TYPE
            If the page is password-protected, this is the authentication method used to protect the page (e.g., "basic").

            REMOTE_USER
            If the page is password-protected, this is the username with which the client authenticated (e.g., "fred"). Note that there&#39;s no way to find out what password was used.

            REMOTE_IDENT
            If the server is configured to use identd (RFC 931) identification checks, this is the username fetched from the host that made the web request (e.g., "barney"). Do not use this string for authentication purposes, as it is easily spoofed.

            CONTENT_TYPE
            The content type of the information attached to queries such as PUT and POST (e.g., "x-url-encoded").

            CONTENT_LENGTH
            The length of the information attached to queries such as PUT and POST (e.g., 3952).
            The Apache server also creates entries in the $_SERVER array for each HTTP header in the request. For each key, the header name is converted to uppercase, hyphens (-) are turned into underscores (_), and the string "HTTP_" is prepended. For example, the entry for the User-Agent header has the key "HTTP_USER_AGENT". The two most common and useful headers are:

            HTTP_USER_AGENT
            The string the browser used to identify itself (e.g., "Mozilla/5.0 (Windows 2000; U) Opera 6.0 [en]")

            HTTP_REFERER
            The page the browser said it came from to get to the current page (e.g., "http://www.example.com/last_page.html")

            Comment


              #7
              /tease.gif" style="vertical-align:middle" emoid="" border="0" alt="tease.gif" />t;line-height:100%">Wirelessly posted (Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; Smartphone; 176x220; SPV C500; OpVer 4.1.10.3))

              Originally posted by AcidBurn@Oct 3rd 2005 00:17
              Code:
              <?php
              if (strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;], &#39;MSIE&#39;) !== false) {
              ?>
              <h3>strpos must have returned non-false</h3>
              <center>[b]You are using Internet ExplorerWhat is PHP?
              PHP (recursive acronym for "PHP: Hypertext Preprocessor") is a widely-used Open Source general-purpose scripting language that is especially suited for Web/Wap development and can be embedded into HTML/WML.
              
              here is an exmple:
              HTML Example:
              
              
              Code:
              <html>
                  <head>
                      <title>Example</title>
                  </head>
                  <body>
              
                      <?php 
                      echo "Hi, I&#39;m a PHP script!"; 
                      ?>
              
                  </body>
              </html>
              Notice how this is different from a script written in other languages like Perl or C -- instead of writing a program with lots of commands to output HTML, you write an HTML script with some embedded code to do something (in this case, output some text). The PHP code is enclosed in special start and end tags that allow you to jump into and out of "PHP mode". The best things in using PHP are that it is extremely simple for a newcomer, but offers many advanced features for a professional programmer. Don&#39;t be afraid reading the long list of PHP&#39;s features. You can jump in, in a short time, and start writing simple scripts in a few hours. --------------- A simple tutotial: (Your First PHP Enabled page) Here we would like to show the very basics of PHP in a short, simple tutorial. This text only deals with dynamic webpage creation with PHP, though PHP is not only capable of creating webpages. PHP-enabled web pages are treated just like regular HTML/WML pages and you can create and edit them the same way you normally create regular HTML/WML pages. Our first php script hello.php HTML:
              Code:
              <html>
              <head>
                <title>PHP Test</title>
              </head>
              <body>
              <?php echo &#39;
              
              Hello World I am a PHP script</p>&#39;; ?>
              </body>
              </html>
              WML Example:
              Code:
              <?php
              {
              header (&#39;Content-Type: text/vnd.wap.wml&#39;);
              echo &#39;<xml version="1.0"?&#39;, &#39;>
              <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
              <wml>&#39;;
              }
              {
              echo &#39;
              <card id="main" title="phptest">
              
              
              Hello World I am a PHP script</p>
              </card>&#39;;
              }
              {
              echo &#39;
              </wml>&#39;;
              }
              ?>
              Note that this is not like a CGI script. The file does not need to be executable or special in any way. Think of it as a normal HTML/WML file which happens to have a set of special tags available to you that do a lot of interesting things. Wink If you tried this example and it did not output anything, it prompted for download, or you see the whole file as text, chances are that the server you are on does not have PHP enabled. Ask your administrator to enable it for you. The point of the example is to show the special PHP tag format. In this example we used <?php to indicate the start of a PHP tag. Then we put the PHP statement and left PHP mode by adding the closing tag, ?>. You may jump in and out of PHP mode in an HTML/WML file like this all you want. Now that you have successfully created a working PHP script, it is time to create the most famous PHP script! Make a call to the phpinfo() function and you will see a lot of useful information about your system and setup such as available predefined variables, loaded PHP modules, and configuration settings. Take some time and review this important information. ------- Let us do something more useful now. We are going to check what sort of browser the visitor is using. For that, we check the user agent string the browser sends as part of the HTTP request. This information is stored in a variable. Variables always start with a dollar-sign in PHP. The variable we are interested in right now is $_SERVER[&#39;HTTP_USER_AGENT&#39;]. Note: $_SERVER is a special reserved PHP variable that contains all web server information. It is known as an autoglobal (or superglobal). These special variables were introduced in PHP 4.1.0. Before this time, we used the older $HTTP_*_VARS arrays instead, such as $HTTP_SERVER_VARS. Although deprecated, these older variables still exist. To display this variable, you can simply do:
              Code:
              <?php 
              echo $_SERVER[&#39;HTTP_USER_AGENT&#39;];
              ?>
              A sample output of this script may be: Mozilla/4.0 (compatible;MSIE 5.01; Windows NT 5.0) There are many types of variables available in PHP. In the above example we printed an Array element. Arrays can be very useful. $_SERVER is just one variable that PHP automatically makes available to you. ------- Show all predefined variables with phpinfo()
              Code:
              <?php
              phpinfo(); 
              ?>
              When you load up this file in your browser, you will see a page full of information about PHP along with a list of all the variables available to you. You can put multiple PHP statements inside a PHP tag and create little blocks of code that do more than just a single echo. For example, if you want to check for Internet Explorer you can do this: Using Control Stuctures and functions:
              Code:
              <?php
              if (strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;], &#39;MSIE&#39;) !== false) {
                  echo &#39;You are using Internet Explorer
              &#39;;
              }
              ?>
              A sample output of this script may be: You are using Internet Explorer Here we introduce a couple of new concepts. We have an if statement. If you are familiar with the basic syntax used by the C language, this should look logical to you. Otherwise, you should probably pick up any introductory PHP book and read the first couple of chapters. You can find a list of php books at http://php.net/books.php The second concept we introduced was the strpos() function call. strpos() is a function built into PHP which searches a string for another string. In this case we are looking for &#39;MSIE&#39; (so-called needle) inside $_SERVER[&#39;HTTP_USER_AGENT&#39;] (so-called haystack). If the needle is found inside the haystack, the function returns the position of the needle relative to the start of the haystack. Otherwise, it returns FALSE. If it does not return FALSE, the if expression evaluates to TRUE and the code within its {braces} is executed. Otherwise, the code is not run. Feel free to create similar examples, with if, else, and other functions. --------- We can take this a step further and show how you can jump in and out of PHP mode even in the middle of a PHP block: Mixing HTML and PHP modes
              </center> <?php } else { ?> <h3>strpos must have returned false</h3> <center>[b]You are not using Internet Explorer[/b]</center> <?php } ?>
              Instead of using a PHP echo statement to output something, we jumped out of PHP mode and just sent straight HTML. The important and powerful point to note here is that the logical flow of the script remains intact. Only one of the HTML blocks will end up getting sent to the viewer depending on the result of strpos(). In other words, it depends on whether the string MSIE was found or not.
              --------

              Thats it for now, i will get more into detail on php functions and jumping in and out of php... [/b]
              i dont undrstand any thing than's

              Comment


                #8
                /tease.gif" style="vertical-align:middle" emoid="" border="0" alt="tease.gif" />t;line-height:100%">Wirelessly posted (Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; Smartphone; 176x220; SPV C500; OpVer 4.1.10.3))
                i dont undrstand any thing than&#39;s[/b]
                Then jus keep quite.
                No need to specify that right?

                Comment

                Working...
                X