ImpressCMS proudly uses SourceForge
ImpressCMS on Ohloh.net
[Tutorials :: This page]  
Tags: Tutorials   SuperGlobal  

Super Globals

Part 7 - $_SUPER GLOBALS

Original link - http://www.kaotik.biz/tutorial/tutorial_part7.html How to use them in a Xoops Module Part 1 Building a simple module
Part 2 - using Smarty templates in a module
Part 3 - Building an AJAX module
Part 4 - Xoops Blocks
Part 5 - Guidelines for Module Development
Part 6 - Theme Development

About This Tutorial

These are called super globals because they always exist within a php page even in a function or class, unlike normal variables that need to be declared as a global to work within functions. Some parts of the following tutorial are directly copied from www.php.net. A good reference can also be found here. I will only be touching on a few of the super globals, the ones more commonly used. $_SERVER and $_ENV are also usefull so I would recommend reading them directly from the php site. Information taken directly from the php site will be in yellow boxes. As usual in my tutorials, all code will be in blue boxes.

$_POST

$_POST This is one method of passing information from the user back to the server. Using this form as an example:


You can then access the text field 'test' using $_POST['test']

$_GET

$_GET The second popular method is setting a var through a URL, such as www.xoops.org/index.php?myvar=3&test=hello In this case: $_GET[‘myvar’] will equal ‘3’ $_GET[‘test’] will equal ‘hello’ The correct way of building a URL is: http://www.website.com/yourwebpage.php?myvarOne=1&myvarTwo=2&myvarThree=3 etc

$_SESSION

$_SESSION Reference from php.net Use it when you need to pass info between several pages, or want to preserve that info while the user is on your webpage. $_SESSION is cleared as soon as the user closes his/her web browser. It’s also cleared when they log in or out of your Xoops site. This is because Xoops clears all sessions on login or off through session_start(). If you need to preserve info between these states (login/logoff) then use $_COOKIE. To store info in $_SESSION use: $_SESSION['favcolor'] = 'green'; $_SESSION['animal'] = 'cat'; $_SESSION['time'] = time(); The advantage of $_SESSION is that you have full control over it, unlike $_COOKIE, where users can have cookies turned off. You can also unset a session by using: unset($_SESSION['count']); Warning: Do NOT unset the whole $_SESSION with unset($_SESSION) as this will disable the registering of session variables through the $_SESSION superglobal. Warning: $_SESSION (as well as $_COOKIE) have security vulnerabilities so never store important information (such as passwords) unencripted inside them.

$_FILE

$_FILE Normally used in conjunction with $_POST, a file will usually be submitted within a form:

          Send this file: 

Note: Be sure your file upload form has attribute enctype="multipart/form-data" otherwise the file upload will not work. Files will, by default be stored in the server's default temporary directory, unless another location has been given with the upload_tmp_dir directive in php.ini. The server's default directory can be changed by setting the environment variable TMPDIR in the environment in which PHP runs. Setting it using putenv() from within a PHP script will not work. This environment variable can also be used to make sure that other operations are working on uploaded files, as well. After the user clicks on submit, we now need to deal with the information in this form, specificly with our file. $_FILE is now an array containing the following elements: $_FILES['userfile']['name'] The original name of the file on the client machine. $_FILES['userfile']['type'] The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted. $_FILES['userfile']['size'] The size, in bytes, of the uploaded file. $_FILES['userfile']['tmp_name'] The temporary filename of the file in which the uploaded file was stored on the server. $_FILES['userfile']['error'] The error code associated with this file upload. This element was added in PHP 4.2.0 You can now build a function that will store your file in it's correct place:

';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:'; print_r($_FILES);

print "

"; ?>

$_COOKIE

$_COOKIE To use a cookie you need to first create it using setcookie(). This has 3 arguments that need to be set; name, value (info contained in the cookie), and expiration. There are other values that can be set such as path, domain, (refer to php.net for more information) but it will work with these 3. setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace. Examples:


Once it's been created you can then access it's information using $_COOKIE["TestCookie"].

$_REQUEST

$_REQUEST A request contains all the info of post, get and cookie. As a shortcut some developers use request to access all these variables, that way they don’t have to worry how the information is getting passed. This however can pose a security problem. In my opinion it’s better to deal with the types of information acordingly. If information is being passed from a form using post, then use $_POST to access it, if it’s passed through a URL, then use $_GET, etc. <[Languages |Super Globals]> Category: Welcome Index Page

Last modified on 2010/11/14 by UnderDog
This page was created on 2010/11/11 by skenow
This page has been viewed 2048 time(s)

Comments
The comments are owned by the poster. We aren't responsible for their content.
ImpressCMS proudly uses SourceForge
ImpressCMS on Ohloh.net