Category Archives: PHP

PHP

PHP OO tutorials

Best way to learn scripting / program is easy. Just write the code and see the result.

With the progress of OOP approach in field recently for our team make its ‘hard’ to adapt such change in mindset :

Procedural vs Object Oriented Programming

With case codeiginter,  we must learn the basic of OOP in PHP. Starting by dumping php4 and use php5 for production release.

Thanks to a lot of tutorial about OO in internet world, some of them are :

  1. Tutorialized
  2. Codewalkers
  3. Massassi
  4. KillerPHP, very good. With video too.

I myself more familiar with book, as I can carry it whenever I want and read it in my bed. When I feel I like to practice I’ll dive to my laptop and try to code.

Some books ( I ordered some in Amazon ) are :

  1. PHP 5 Objects, Patterns, and Practice PHP OO tutorials
  2. CodeIgniter for Rapid PHP Application Development PHP OO tutorials

From amazon description :

CodeIgniter (CI) is a powerful open-source PHP framework with a very small footprint, built for PHP coders who need a simple and elegant toolkit to create full-featured web applications. CodeIgniter is an MVC framework, similar in some ways to the Rails framework for Ruby, and is designed to enable, not overwhelm.

read more »

PHP

nl2br, new line to br

I just forget this function when my friend ask me about strangeness in his data he store in mysql.

He has address data in <textarea></textarea>, new line not converted to br as he thing. He just see all the input just store in single line.

After search for a while I remember this gold function :

nl2br () , new line to br

just make temporary variable before as :

$address=nl2br($address);

now $address will contain <br> tag in mysql database.

This post for remind me in future icon wink nl2br, new line to br

 

PHP

php explode() and wordwrap()

Whats the different of explode() and wordwrap() ?

Answer :

just see this example for explode()

<?php

$birthday=’1998-12-11′;

//we need to split the component to date, month and year

$day_parts=explode(‘-’,$birthday);

// we use – as tool to separate, now print

$date_component=$day_parts[2];

$month_component=$day_parts[1];

$year_component=$day_parts[0];

?>

Wordwrap will ‘wrapping’ user input, like formatting crazy user who type ‘aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa’ till he satisfied and brake our guestbook table.

Just make sure to add number bigger than 0 to enable wordwrap.

PHP

101 CodeIgniter Resources

CodeIgniter already attract my attention since version 1.5, we even try to build application based on it.

Beside its clear documentation, their user support awesome too.

I’ll try to collect every resource that might useful to share with others who want to learn codeigniter.

This post will be update regularly as soon as I get another resource icon smile 101 CodeIgniter Resources

CodeIgniter Main Page

1. Download site

2. User Guide

3. Wiki

4. Forum

CodeIgniter Video Tutorials

1. Hello world, introduction to CodeIgniter

2. Create a blog in 20 minutes

3. Derek Allard Video

CodeIgniter Books

1. CodeIgniter for Rapid PHP Application Development 101 CodeIgniter Resources

CodeIgniter Tutorials

1. Introduction to Code Igniter: Part 1 by Godbit, learn about CI structure. starting the case of making form.

2. Introduction to Code Igniter: Part 2 by Godbit, form continued.

3. Introduction to Code Igniter: Part 3 by Godbit, CI interact with database.

4. Pagination with Code Igniter by Godbit, make navigation easy with paging in CI.

5. Code Igniter Template Tutorial by Eric Barnes, separate logic with presentation.

6. Templating with CodeIgniter, templating study case.

7. Using Zend Framework components in Code Igniter, integration zend framework into CI.

8. Building an RSS Feed in Code Igniter, Derek Allard show howto build RSS with CI.

9. Using Oracle with PHP and CodeIgniter, Oracle and CI teamed together. I use oracle xe instead.

10. Forms in CodeIgniter Views, use don’t repeat yourself concept icon smile 101 CodeIgniter Resources

11. Checking CodeIgniter out of the Subversion Repository, Derek show how to use subversion of CI.

12. SimplePie RSS class and CodeIgniter, simplepie and CI work together.

13. Building a Complete CodeIgniter Application: Part 1, by Jim O’Halloran. Build feedignitation from scratch.

14. Building a Complete CodeIgniter Application: Part 2, by Jim O’Halloran.

15. Building a Complete CodeIgniter Application: Part 3, by Jim O’Halloran. Eliminate security problems.

Last Update : January 09 2008

Latest news : Code Igniter 1.6 released!, read more from DerekAllard blog.

PHP

Make lowercase followed by uppercase data

Question :

I have table A with field id, name and address. The data in name field seem not consistent, big letter name sometime found in record. How to update that field with uppercase in name field. For example :

                 id              name                                       address

                 1               FreAKHolicz  wORker            the moon

                 2              tHE atMOSFER bluEZ            nowhere

FreAKHolicz  wORker must change to Freakholicz Worker

Answers :

1. Looping

2. Use strtolower() function followed by ucwords() function.

example code :

<?

$query=mysql_query("select id,name from table A");

while(list($id,$name)=mysql_fetch_row($query))

{

//change to lower case

$name1=strtolower($name); // will make FreAKHolicz  wORker  into freakholicz worker

//change to uppercase

$name2=ucwords($name1); // will make freakholicz worker into Freakholicz  Worker

//now using $name2 as variable to update name field

$update=mysql_query("update A set name=’$name2′ where id=’$id’");

}

?>

since it’s looping, all good record also examined by scripts until no record left, using another conditional condition would bring another result.

P.S :

  • Use ucfirst() to change only first alfabet
  • Its possible using many function in one operation such as :  $name2=ucwords(strtolower($name));

Database PHP

Export picture from mysql database to file

[amazonify]032152599X:left[/amazonify]
As our data getting mobile and copying picture is annoying for me I decide to save the picture in the database (mysql).

Another challenge came when other division need the data exported in a directory with same dimension (135 x 180 pixel).

Structure for table ‘student_pic’  :

CREATE TABLE `student_pic` (
  `image_id` int(10) unsigned NOT NULL auto_increment,
  `username` varchar(50) NOT NULL default ”,
  `image_type` varchar(50) NOT NULL default ”,
  `image` longblob NOT NULL,
  `image_size` bigint(20) NOT NULL default ’0′,
  `image_name` varchar(255) NOT NULL default ”,
  `image_date` datetime NOT NULL default ’0000-00-00 00:00:00′,
  UNIQUE KEY `image_id` (`image_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3385 ;

Yes, I use mysql 5 ( 5.0.45)

read more »

PHP

PHP 5.2.5 Released, 7 security enhancements & fixes

From php.net

The PHP development team would like to announce the immediate availability of PHP 5.2.5. This release focuses on improving the stability of the PHP 5.2.x branch with over 60 bug fixes, several of which are security related. All users of PHP are encouraged to upgrade to this release.

Further details about the PHP 5.2.5 release can be found in the release announcement for 5.2.5, the full list of changes is available in the ChangeLog for PHP 5.

Security Enhancements and Fixes in PHP 5.2.5:

  • Fixed dl() to only accept filenames. Reported by Laurent Gaffie.
  • Fixed dl() to limit argument size to MAXPATHLEN (CVE-2007-4887). Reported by Laurent Gaffie.
  • Fixed htmlentities/htmlspecialchars not to accept partial multibyte sequences. Reported by Rasmus Lerdorf
  • Fixed possible triggering of buffer overflows inside glibc implementations of the fnmatch(), setlocale() and glob() functions. Reported by Laurent Gaffie.
  • Fixed "mail.force_extra_parameters" php.ini directive not to be modifiable in .htaccess due to the security implications. Reported by SecurityReason.
  • Fixed bug #42869 (automatic session id insertion adds sessions id to non-local forms).
  • Fixed bug #41561 (Values set with php_admin_* in httpd.conf can be overwritten with ini_set()).

For users upgrading to PHP 5.2 from PHP 5.0 and PHP 5.1, an upgrade guide is available here, detailing the changes between those releases and PHP 5.2.5.

Download now.

PHP

Create Paging with PHP : Link collection

Soon or later everyone who learn php will face this thing : ‘paging

blog, guest book, search result are using paging for displaying data. Make it easy to read and more friendly

some picture of paging implementation :

  Create Paging with PHP : Link collection

 Create Paging with PHP : Link collection

 Create Paging with PHP : Link collection

 Good paging code will survive when record in database or file system getting bigger.

many url in internet exist for such purpose, teach how to create paging with php.

I start to collect them in this page that might be useful for someone who like to implement paging in his guestbook, search result etc.

Some urls : 

 

Maybe you have found useful url that might help others?

I’ll update the list if I have other link.

Tell me please icon smile Create Paging with PHP : Link collection

PHP

Beware, wordpress exploit released, upgrade your wordpress!

From milw0rm exploits :

# Pwnpress implements multiple techniques, bugs and tricks for compromising
# WordPress-based blogs, combining the exploits in the necessary order for
# retrieving any necessary information to make the exploitation process as
# reliable as possible.
#
# Covertness capability is provided, dynamically adapting the payloads and
# operations to lower potential ‘noise’ on the wire. Fingerprinting deploys few
# methods able to detect all versions of WordPress in their default installation
# form without tampering of wp-includes/version.php
#
# Tested with WordPress 2.2, 2.2.2, 2.0.5, 2.0.6, 2.1, (…), PHP/5.2.4 for
# Apache 2.0.58 on Gentoo GNU/Linux. magic_quotes on and off for the different
# exploits.
#

Upgrade your wordpress to latest version right now!

PHP Unix

Multiple domains login in roundcube webmail

Roundcube is very good solution for webmail. I use squirrelmail for almost 4 years and I like it too.

In production server I install squirrelmail and roundcube as well for give other user different experience icon smile Multiple domains login in roundcube webmail

using mail and mail2 as prefix and multiple domains need special attention in roundcube.

After search in their forum I like this snippet :

 function getDomain() {

    $host=$_SERVER['HTTP_HOST'];

    $prefix=’mail’;

    $arr=explode(‘.’,$host);

    if ($arr[0]==$prefix) { unset($arr[0]); $host=implode(‘.’,$arr); }

    else { $host=”; }

    return $host;

}

plus this line :

$rcmail_config['username_domain'] = getDomain();

 put that in config/main.inc.php

for my case where I use mail and mail2 just edit the line :

if ($arr[0]==$prefix) { unset($arr[0]); $host=implode(‘.’,$arr); }

into

if ($arr[0]==$prefix or $arr[0]==’mail2′) {

unset($arr[0]); $host=implode(‘.’,$arr);

}

 Working  well for me icon smile Multiple domains login in roundcube webmail

PHP

date.timezone in php.ini for php5

My last post about glitch in time related function for php5 I use a function date_default_timezone_set

For global setting as always use date.timezone in php.ini

just change from :

;date.timezone =Asia/Jakarta

uncomment it first by remove semicolon mark into this :

date.timezone =Asia/Jakarta

save php.ini files and restart apache

try view using phpinfo()

 date.timezone in php.ini for php5

you’re done icon smile date.timezone in php.ini for php5