Web Programming with PHP 3 Superglobals Form File
Web Programming with PHP (3) Superglobals, Form & File processing
PHP Superglobals Several predefined variables in PHP are "superglobals", which means they are available in all scopes throughout a script. There is no need to do global $variable; to access them within functions or methods. By default, all of the superglobals are available but there are directives that affect this availability. For further information, refer to the documentation for variables_order.
PHP Superglobals (2) 1. $GLOBALS : An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array. 1. $_SERVER : $_SERVER is an array containing information such as headers, paths, and script locations. 1. $_GET : An associative array of variables passed to the current script via the URL parameters. 1. $_POST - An associative array of variables passed to the current script via the HTTP POST method. 1. $_FILES - An associative array of items uploaded to the current script via the HTTP POST method. 1. $_COOKIE - An associative array of variables passed to the current script via HTTP Cookies. 1. $_SESSION - An associative array containing session variables available to the current script. 1. $_REQUEST - An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE. 1. $_ENV - An associative array of variables passed to the current script via the environment method.
$_GET Note that the print_r() inbuilt function is used to print out an array with all it’s value Program 1: $_GET_example. php <? php print_r($_GET); ? > Run : $_GET_example. php Output? Run with parameters : $_GET_example. php? name=prakash&subject=isd 3
$_POST <html> <head><title>$_POST test form</title></head> <body> <form method = "post" action = "$_POST_example. php"> <p> First Name : <input type = "text" name = "fname" /> <input type = "submit" value = "Submit" /> </p> </form> </body> </html> Saved as : form_example 1. html Program 2: $_POST_example. php <? php print_r($_POST); ? >
In class example • pull user submitted values from a form • validate form input • write / save it to a txt file
PHP Resources • PHP Home : www. php. net • Learning PHP : www. w 3 schools. com/php • PHP Extension & Application Repository (PEAR) : pear. php. net • PHP Resource Index : php. resourceindex. com • PHP Builder : www. phpbuilder. com • Installing Apache/PHP/My. SQL (xampp) : www. apachefriends. org/en/xamppwindows. html
- Slides: 7