Results 1 to 3 of 3
-
3rd Feb 2010, 05:33 AM #1OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comTutorial [Creating a PHP Framework] {advanced} (PART 2)
PART 1: http://www.besthostingforums.com/showthread.php?t=23883
Ok heres part 2 of the framework tutorial!
In the last part we discussed benefits of the framwork and started the main base!
in this part were going to create the Input object and attach it to the framework registry
About the input object:
The input obect is a class that you will use to get any data sent via the user such as GET and POST, the reason we use a class to grab these slices of data is so we can perform sanitization and stuff to make them safe before you use them in your application!
so an overview of the input and what the code below is doing:
Were going to grab all the seperate globals that hold the user input and individually place them in an array after they have been sanitized
so lets begin
Create a file called Input.php
Heres the code:
PHP Code:<?php
if(!defined('BASE_PATH')){exit;} /*REMEMBER this should be in every file thats included by startup.php*/
class Input{
//Set up the place holders
private $globals = array();
function __construct()
{
$this->globals['get'] = $this->sanitize($_GET,true);
$this->globals['post'] = $this->sanitize($_POST,true);
$this->globals['files'] = $this->sanitize($_FILES,true);
$this->globals['cookie'] = $this->sanitize($_COOKIE,true);
}
/*This function is what you will use to get the sanitized data*/
public function get($name,$section,$default = false)
{
return (isset($this->globals[$section][$name]) ? $this->globals[$section][$name] : $default);
}//Usage:: $object->get('username','post',false);
public function sanitize($input,$keys)
{
switch(gettype($input))
{
case 'array':
//Set up a blank array to hold the new data
$holder = array();
foreach($input as $item => $value)
{
if($keys === true)
{
//We make sure that the keys is valid string else exit
if(!preg_match("/^[a-z0-9_\-]+$/i",$item))
{
die('Invalid keys found within input, please contact owner if this persists');
}
//Now we sanitize the value recursivly
$holder[$item] = $this->sanitize($value,true);
/*
Running the sanitize function within the sanitize funtion creates a recursive pattern
so you sanitize all sub arrays of the array!
*/
}
}
break;
case 'string':
//Ok so here we check to see if the value of bar=foo is numeric, if so then we will make it an interger instead of a string
if(is_numeric($input))
{
return (int)$input;
}
//Here im just gonna check wether its a string of true or false and set it to a bool if it is
if($input == 'true' || $input == 'false')
{
return (bool)$input;
}
/*
ENT_QUOTES will covert both ' and " to '
IMPORTANT NOTE:
This will take care of post entities but you MUST still escape for database
*/
return htmlentities($input, ENT_QUOTES, 'UTF-8');
break;
default:
//This leaves it to objects or resources etc wich we wont need to sanitize
return $input;
break;
}
}
}
?>
SAVING THE FILE:
Save the file system/engine/Input.php -> with a capital I
now open up your startup.php file and file the section where it says
PHP Code:/*
** here we will include all framework files
*/
PHP Code:include SYSTEM_BASE_PATH . '/engine/Input.php';
so find
PHP Code:/*
** here we will load all classes into the registry
*/
PHP Code:Registry::set('Input', new Input()); //Clean eh
PLEASE DO NOT START THIS CODE UNTILL I HAVE FINISHED ALL SECTIONS
In the next part we will be doing:
- Output (headers,cookies) compression / gzip
- Binding the output with registry (link the input)
- A little talk about the output
Part 3: http://www.besthostingforums.com/sho...d.php?p=223286litewarez Reviewed by litewarez on . Tutorial [Creating a PHP Framework] {advanced} (PART 2) PART 1: http://www.besthostingforums.com/showthread.php?t=23883 Ok heres part 2 of the framework tutorial! In the last part we discussed benefits of the framwork and started the main base! in this part were going to create the Input object and attach it to the framework registry About the input object: The input obect is a class that you will use to get any data sent via the user such as GET and POST, the reason we use a class to grab these slices of data is so we can perform Rating: 5Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
-
3rd Feb 2010, 05:35 AM #2Respected DeveloperWebsite's:
X4B.org
-
3rd Feb 2010, 05:37 AM #3OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comSplitice this is not for mysql, this is soly input only (when do you insert string::'true' into the db lol its always intbools 0 / 1)
if a user goes to an url of
login.php?redirect=true
then with the get function it will return a bool instead og you doing == 'true' and checking as string!
Just basic type casting turing numerics into ints and bool strings into bools
PHP Code:if(Registry::get('Input')->get('redirect','get',false)){}/*aslong as your expecting true or false*/
Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Tutorial [Creating a PHP Framework] {advanced} (PART 6) Smarty Engine
By litewarez in forum Tutorials and GuidesReplies: 5Last Post: 19th Feb 2010, 06:38 PM -
Tutorial [Creating a PHP Framework] {advanced} (PART 5) - Sessions
By litewarez in forum Tutorials and GuidesReplies: 1Last Post: 12th Feb 2010, 03:06 PM -
Tutorial [Creating a PHP Framework] {advanced} (PART 4)
By litewarez in forum Tutorials and GuidesReplies: 1Last Post: 9th Feb 2010, 04:24 AM -
Tutorial [Creating a PHP Framework] {advanced} (PART 3)
By litewarez in forum Tutorials and GuidesReplies: 1Last Post: 4th Feb 2010, 02:12 AM -
Tutorial [Creating a PHP Framework] {advanced} (PART 1)
By litewarez in forum Tutorials and GuidesReplies: 1Last Post: 30th Jan 2010, 03:37 PM
themaManager - edit and manage...
Version 4.04 released. Open older version (or...