Its My Space

Cooooooooooool

Archive for the ‘Introduction’ Category

Introduction

PHP Superglobals

Posted by Ramkumar on February 12, 2009

PHP Superglobals : The PHP Superglobals are a handful of arrays that provide to a PHP script global access to data originating externally. Whereas PHP scripts contain variables that are local to that script and functions may have variables that are only accessible within that function, the PHP Superglobals represent data coming from URLs, HTML forms, cookies, sessions, and the Web server itself. $HTTP_GET_VARS, $HTTP_POST_VARS, etc., served these same purposes but the PHP superglobal variables are better in that they can also be accessed within any functions (i.e., they have global scope)

* $_GET The $_GET Superglobal represents data sent to the PHP script in a URL. This applies both to directly accessed URLs (e.g., http://www.example.com/page.php?id=2) and form submissions that use the GET method.

* $_POST The $_POST Superglobal represents data sent to the PHP script via HTTP POST. This is normally a form with a method of POST.

* $_COOKIE The $_COOKIE Superglobal represents data available to a PHP script via HTTP cookies.

* $_REQUEST The $_REQUEST Superglobal is a combination of $_GET, $_POST, and $_COOKIE.

* $_SESSION The $_SESSION Superglobal represents data available to a PHP script that has previously been stored in a session.

* $_SERVER The $_SERVER Superglobal represents data available to a PHP script from the Web server itself. Common uses of $_SERVER is to refer to the current PHP script ($_SERVER['PHP_SELF']), the path on the server to that script, the host name, and so on. (The image at right shows some sample $_SERVER values, which can be easily viewed in NuSphere’s PhpED’s Globals pane.)

* $_ENV The $_ENV Superglobal represents data available to a PHP script from the environment in which PHP is running.

* $_FILES The $_FILES Superglobal represents data available to a PHP script from HTTP POST file uploads. Using $_FILES is the currently preferred way to handle uploaded files in PHP.

Another PHP Superglobal, called $GLOBALS, stores every variable with global scope, which includes the above. Unlike the other Superglobals, $GLOBALS has been around since PHP 3.

Note : One key aspect of Web application security is referring to variables with precision, which is why relying upon register_globals is bad. For the same reason, one should not use $_REQUEST as it is less exact, and therefore less secure, than explicitly referring to $_GET, $_POST, or $_COOKIE. Furthermore, the order in which the GET, POST, and COOKIE data is loaded into the $_REQUEST array is dictated by PHP’s variables_orders configuration, so the same reference to $_REQUEST in a PHP script could behave differently on different servers.

Posted in Introduction, PHP | Leave a Comment »

Differences Between PHP 4 and 5

Posted by Ramkumar on January 29, 2009

Differences Between PHP 4 and 5

Language Features

  • PHP 5 allows limited type hinting. This allows you to specify that the parameter

to a function or class method can only be of a specific class (or one of its

subclasses), or an array. However, you may not specify any other scalar types.

  • The foreach construct now supports by-reference declaration of the value element.
  • A number of new functions, particularly for string and arraymanipulation, has

also been added to the core platform.

Objects

  • For all intents and purposes, all objects in PHP 5 are passed by reference. This

means that assigning an object to a variable will not create a copy of the former,

but simply creates another reference to it.

  • Constants, aswell as staticmethods and properties, can nowbe definedwithin

the scope of a class.

  • Class methods and properties now feature visibility, and can be declared as

public, private or protected. Classes and methods can also be declared as

final to prevent further inheritance.

  • Since all objects are assigned by reference, you now need a specialized mechanism

to copy objects. This is provided by the clone construct and the __clone()

magic method.

  • PHP 5 features unified constructors and destructors-all constructors should

now be named __construct(), and the new __destruct() magic method has

been added for object destruction.

  • With the addition of interfaces and abstract classes, PHP developers now have

far greater control over how they implement their object-oriented code. Interfaces

can be used to define common APIs, while abstract classes provide

models for class implementations that follow a specific blueprint.

  • Class definitions can now be loaded on demand by using the __autoload()

function.

MagicMethods

A multitude of new “magic” methods has been introduced in PHP 5:

  • __get() and __set() are called when accessing or assigning an undefined object

property, while __call() is executed when calling a non-existent method

of a class.

  • __isset() is called when passing an undefined property to the isset() construct.
  • __unset() is called when passing an undefined property to unset().
  • __toString() is called when trying to directly echo or print() an object.
  • __set_state() is inserted dynamically by var_export() to allow for reinitialization

on execution of var_export()’s output.

Selected New Extensions

  • SimpleXML allows easy access to XML data using object and array notation.
  • PHP 5 also introduces a DOMXML, DOMXSL and Sablotron replacement in

the formof the libxml2-based DOM and XSL extensions.

  • The PHP Data Objects (PDO) extension provides a unified database access extension

that allows access to many different types of database systems by using

a common interface. PDO is not an abstraction layer-except for prepared

queries, it does nothing to abstract the actual database code (SQL), itself.

  • The hash extension is a new replacement for the GPLed libmhash; it was added

to the PHP core starting with version 5.1.2. It can produce hashes using many

algorithms, including the familiarMD5and SHA1, aswell as some more secure

(albeit slower) algorithms, such as snefru.

  • The Standard PHP Library (SPL) provides numerous interfaces that enhance

the way classes interact with the PHP language, including the new Iterator

interfaces.

  • The new Reflection extension allows for runtime introspection of executing

PHP code.

ErrorManagement

  • Classes now support exceptions; the new set_exception_handler() function

allows you to define a script-wide exception handler.

  • The E_STRICT error reporting level has been added to the language to emit notices

when legacy or deprecated code is encountered.

Posted in Interview Questions, Introduction, PHP | Tagged: , | 1 Comment »

PHP and COM

Posted by Ramkumar on January 24, 2009

1. I have built a DLL to calculate something. Is there any way to run this DLL under PHP ?

If this is a simple DLL there is no way yet to run it from PHP. If the DLL contains a COM server you may be able to access it if it implements the IDispatch interface.

2. What does ‘Unsupported variant type: xxxx (0xxxxx)’ mean ?

There are dozens of VARIANT types and combinations of them. Most of them are already supported but a few still have to be implemented. Arrays are not completely supported. Only single dimensional indexed only arrays can be passed between PHP and COM. If you find other types that aren’t supported, please report them as a bug (if not already reported) and provide as much information as available.

3. Is it possible manipulate visual objects in PHP ?

Generally it is, but as PHP is mostly used as a web scripting language it runs in the web servers context, thus visual objects will never appear on the servers desktop. If you use PHP for application scripting e.g. in conjunction with PHP-GTK there is no limitation in accessing and manipulating visual objects through COM.

4. Can I store a COM object in a session ?

No, you can’t. COM instances are treated as resources and therefore they are only available in a single script’s context.

5. How can I trap COM errors ?

In PHP 5, the COM extension throws com_exception exceptions, which you can catch and then inspect the code member to determine what to do next.

In PHP 4 it’s not possible to trap COM errors beside the ways provided by PHP itself (@, track_errors, ..).

6. Can I generate DLL files from PHP scripts like i can in Perl ?

No, unfortunately there is no such tool available for PHP.

7. What does ‘Unable to obtain IDispatch interface for CLSID {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}’ mean ?

This error can have multiple reasons:

  • the CLSID is wrong
  • the requested DLL is missing
  • the requested component doesn’t implement the IDispatch interface

8. How can I run COM object from remote server ?

Exactly like you run local objects. You only have to pass the IP of the remote machine as second parameter to the COM constructor.

Make sure that you have set com.allow_dcom =TRUE in your php.ini.

9. I get ‘DCOM is disabled in C:\path…\scriptname.php on line 6′, what can I do ?

Edit your php.ini and set com.allow_dcom =TRUE.

10. Is it possible to load/manipulate an ActiveX object in a page with PHP ?

This has nothing to do with PHP. ActiveX objects are loaded on client side if they are requested by the HTML document. There is no relation to the PHP script and therefore there is no direct server side interaction possible.

11. Is it possible to get a running instance of a component ?

This is possible with the help of monikers. If you want to get multiple references to the same word instance you can create that instance like shown:

<?php
$word = new COM("C:\docs\word.doc");
?>

This will create a new instance if there is no running instance available or it will return a handle to the running instance, if available.

12. Is there a way to handle an event sent from COM object ?

You can define an event sink and bind it using com_event_sink(). You can use com_print_typeinfo() to have PHP generate a skeleton for the event sink class.

13. I’m having problems when trying to invoke a method of a COM object which exposes more than one interface. What can I do ?

The answer is as simple as unsatisfying. I don’t know exactly but i think you can do nothing. If someone has specific information about this, please let me know :)

14. So PHP works with COM, how about COM+ ?

COM+ extends COM by a framework for managing components through MTS and MSMQ but there is nothing special that PHP has to support to use such components.

15. If PHP can manipulate COM objects, can we imagine to use MTS to manage components resources, in conjunction with PHP ?

PHP itself doesn’t handle transactions yet. Thus if an error occurs no rollback is initiated. If you use components that support transactions you will have to implement the transaction management yourself.

Posted in Interview Questions, Introduction, PHP | Leave a Comment »

About PHP

Posted by Ramkumar on January 21, 2009

PHP is a powerful server-side scripting language for creating dynamic and interactive websites.

PHP is the widely-used, free, and efficient alternative to competitors such as Microsoft’s ASP. PHP is perfectly suited for Web development and can be embedded directly into the HTML code.

The PHP syntax is very similar to Perl and C. PHP is often used together with Apache (web server) on various operating systems. It also supports ISAPI and can be used with Microsoft’s IIS on Windows.

Posted in Introduction | Tagged: , | Comments Off