User Login    
 + Register
  • Main navigation
Login
Username:

Password:

Remember me



Lost Password?

Register now!
Who's Online
81 user(s) are online (8 user(s) are browsing XoopsWiki)

Members: 2
Guests: 79

caek9, mikem333, more...
[Main Page]

$ SUPER GLOBALS

From XOOPS Project

Main Page | Recent changes | Edit this page | Page history | Switch to MediaWiki mode

Printable version | Disclaimers | Privacy policy

TO: Forum Pool

Contents

Kaotik Index

Part 7 - $_SUPER GLOBALS

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:

<form id="form1" name="form1" method="post" action="">
<input type="text" name="test" id="test" />
<input type="submit" name="submit" id="submit" value="Submit" />
</form


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:

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="index.php" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>


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:

<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
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 "</pre>"; 
?>


$_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 <html> and <head> tags as well as any whitespace.


Examples:

<?php
$value = 'something from somewhere';

setcookie("TestCookie", $value);
setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */
setcookie("TestCookie", $value, time()+3600, "/~rasmus/", ".example.com", 1);
?>


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.

Retrieved from "http://www.xoops.org/modules/mediawiki/index.php/%24_SUPER_GLOBALS"

This page has been accessed 775 times. This page was last modified 23:40, 16 December 2007. Content is available under XOOPS Project.


Developers for Hire
Developers for Hire
Local Support Sites
Make a donation
Please select an amount to donate


Do you want your username revealed with your donation?
Yes - List me as a Generous Donor
No - List my donation as from an Anonymous Donor


Powered by
XOOPS Code hosted on SourceForge

Powered by PHP

PHP 5

Powered by MySQL

Powered by Smarty

OSI certified

GPL

All content on this site is subject to the Creative Commons License
Top Tags
Theme (5) news (2) security (2) sport (1) Arabic (1) wiki (1) Christmas (1) jQuery (1) tag (1) module (1) SEO (1) comments (1)
Advertisement