Category: PHP & Java Script

PHP & Java Script

PHP – How to Filter/validate IP Address?

PHP – How to Filter/validate IP Address?

Introduction –

Many times, we need to validate an IP Address. Of course, and IP address may be of different formats for ipv4 and ipv6. An IP address may also need to be within a range of private or reserved ranges. The filter extension makes it possible to discern these differences and to validate an IP address to fit most needs. In its simplest form the validation of a URL will look like this….

// Valid IP address
$ip = “192.168.0.1”;

if(filter_var($ip, FILTER_VALIDATE_IP) === FALSE)
{
echo “$ip is not a valid IP”;
}
else
{
echo “$ip is valid”;
}
?>

As we have supplied the above with a valid IP address it validates and all is well. But now we may wish to validate an IPv6 address or an address with a private range. The IP Filter has several flag with which to validate an IP address with.

Flag are for IP Filter –

  • FILTER_FLAG_IPV4

  • FILTER_FLAG_IPV6

  • FILTER_FLAG_NO_PRIV_RANGE

  • FILTER_FLAG_NO_RES_RANGE

** Starting at the top we will check to see if an IP is a valid IPv4 address.

// Valid IP address
$ip = “192.168.0”;

// try to validate as IPV4 address
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === FALSE)
{
echo “$ip is not a valid IP”;
}
else
{
echo “$ip is valid”;
}
?>

In the above example the IP address has failed to validate as it is not a complete IPv4 address. It would need to be of the form of the example that preceded it, 192.168.0.1 to validate. This is fine, but growth of the net has seen us run out of IPv4 addresses and so we need to validate against IPv6 addresses also….

 

Thank you,

Santhosh Tirunahari

PHP4 and PHP5

PHP4 and PHP5

Introductions –

First starts with PHP4 features. PHP4 started up with object-oriented concepts like as c++. PHP4 got almost all the OOPs features. With little difference in functionality…

 

Below are PHP4 added features –

 

  1. class

  2. extends

  3. Constructors

  4. Scope Resolution Operator (::)

  5. parent

  6. Serializing objects – objects in sessions

  7. The magic functions __sleep and __wakeup

  8. References inside the constructor

  9. Comparing objects

 

PHP 5 now came up with new Object Model. Even php4 contains the object model functionalities but PHP5 handling of objects has been completely rewritten, allowing for better performance and more features.

Features Of PHP5 –

  1. Constructors and Destructor

  2. Visibility

  3. Scope Resolution Operator (::)

  4. Static Keyword

  5. Class Constants

  6. Class Abstraction

  7. Object Interfaces

  8. Overloading

  9. Object Iteration

  10. Patterns

  11. Magic Methods

  12. Final Keyword

  13. Object cloning

  14. Comparing objects

  15. Reflection

  16. Type Hinting

  17. Late Static Binding

 

Some of the above features are in PHP4. But still PHP5 has got improved functionalities on above features. Some of the syntaxes are changed.

Thank you,

Santhosh Tirunahari

PHP – Oracle Connectivity

PHP – Oracle Connectivity

Introductions –

Using PHP we can connect many databases like MySQL, Postgresql, DB2, Oracle and SQL server very easily. Now this article provides the information about database connectivity to Oracle using PHP

PHP Oracle functions allow you to access Oracle 10, Oracle 9, Oracle 8 and Oracle 7 databases using the Oracle Call Interface (OCI). They support binding of PHP variables to Oracle placeholders, have full LOB, FILE and ROWID support, and allow you to use user-supplied define variables.

Configuration part in windows php_oci8.dll is the connectivity dll in windows environment. In Linux we can compile php with oracle module

Once the configuration completes you will be able to see this configuration when you execure –

URL = http://localhost/phpinfo.php

<?php

phpinfo();

?>

php-oracle

** Basic example to connect ORACLE database and execute query –

root@arunbagul:~# cat /var/www/php_oracle.php

<?php
$conn = oci_connect(‘hr’, ‘hr’, ‘orcl’);
if (!$conn) {
$e = oci_error();
print htmlentities($e[‘message’]);
exit;
}

$query = ‘SELECT * FROM DEPARTMENTS’;

$stid = oci_parse($conn, $query);
if (!$stid) {
$e = oci_error($conn);
print htmlentities($e[‘message’]);
exit;
}

$r = oci_execute($stid, OCI_DEFAULT);
if (!$r) {
$e = oci_error($stid);
echo htmlentities($e[‘message’]);
exit;
}
root@arunbagul:~#

Other way to connect to oracle using simple Oracle functions. But these functions are deprecated. Now a day’s oci8 functions are in used.

Cheers,
Santhosh Tirunahari

PHP and FTP

PHP and FTP

Introductions –

PHP provides FTP library to connect to file server using File Transfer Protocol. No external libraries are needed to build this extension. In order to use FTP functions with your PHP configuration, you should add the –enable-ftp or –with-ftp option when installing PHP 5 and other versions. The windows version of PHP has built in support for this extension. You do not need to load any additional extension in order to use these functions…

** Once the installation is done you can check by executing the below script –

URL – http://localhost/phpinfo.php

<?php

phpinfo(); // show the information of php.ini

?>

phpinfo-ftp

Using PHP we can make web interface which will do the entire file upload, download and execute shell commands on remote server. We can make Web FTP client using PHP FTP library.

examples –

How to upload file using PHP-FTP function –

<?php
// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// check connection
if ((!$conn_id) || (!$login_result)) {
echo “FTP connection has failed!”;
echo “Attempted to connect to $ftp_server for user $ftp_user_name”;
exit;
} else {
echo “Connected to $ftp_server, for user $ftp_user_name”;
}

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);

// check upload status
if (!$upload) {
echo “FTP upload has failed!”;
} else {
echo “Uploaded $source_file to $ftp_server as $destination_file”;
}

// close the FTP stream
ftp_close($conn_id);
?>

Thank you,
Santhosh Tirunahari

Working with PHP and Excel

Working with PHP and Excel

Introductions –

PHP got libraries through which we can create or access the excel data. There are two possible ways of creating or accessing the excel data in PHP

1) PHP – Excel library provided by PEAR.
2) PHP using Excel COM component functions and libraries.

** In PHP – Excel libraries, Spreadsheet_Excel_Reader is the class through which we can read the data from excel file. Online documentation is available on following url … http://pear.php.net/package/Spreadsheet_Excel_Reader

Spreadsheet_Excel_Writer is the class to write the data in a excel file. Online documentation is available on following url … http://pear.php.net/package/Spreadsheet_Excel_Writer

** Using Excel – COM components we can create, read the data from excel file easily. Here is
The small code which uses COM components to read the excel data….

<?php

$exlObj = new COM(“Excel.Application”) or Die (“Did not connect”);
$exlObj->Workbooks->Open(“test.xls”);
$exlBook = $exlObj->ActiveWorkBook;
$exlSheets = $exlBook->Sheets;
$exlSheet = $exlBook->WorkSheets(1);
echo $exlSheets->Count;
echo $sheetName = $exlSheet->Name;
$cell = $exlSheet->Cells(1, 1);
echo $cell->Value;

?>

Using both libraries writing excel with all formatting is available. And comparatively PHP-Excel COM components function provides a lot of functionalities to work with excel sheet on the fly.

I have tried to modify the existing excel sheet but it is not happened with both PEAR excel library and Excel COM libraries. I think it is not possible to modify the excel on the fly with out disturbing any color combinations. If any one got the solution looking for their comments….

Thank you,
Santhosh Tirunahari