Recent News

Developing web application with PHP-MySql-Apache in Ubuntu Linux

Posted by admin on July 5th, 2008

I have started using Ubuntu 8.04 in few days ago and found it Great. So, now I’ve moved my development environment from windows and working in Ubuntu. Here I am just explaining what steps I had to take for this jump. Also have a listing of some development related softwares/tools which I am using as replacement of windows applications.

Installing Apache, Php5, MySql in Ubuntu:

You can install Apache, Php, Mysql and other applications from Synaptic Package Manager. Go to System > Administration > Synaptic Package Manager and search with your desired application name. When found, double-click on package name. The package will be marked for installation along with it’s dependences. Now click on apply button to install the package. In this post, the red+bold texts are the name of software package which can be installed from Synaptic Package Manager.

To make your system ready for PHP development, you have to install more or less this packages: apache2.2-common, apache2-utils, php5, mysql-server-5.0, mysql-client-5.0, mysql-query-browser etc. You may also require some common php extensions which may not installed automatically with PHP installation. Most of them also will be found in Synaptic Package Manager as php5-gd, php5-curl, php5-mysql, php-pear etc.

Paths of important directories

One of the major differences I find here is the file system. All the directory paths I’ve been using in windows are changed here. I m listing here some PHP-MySql related paths here. But they may differ on your installation.

  • Apache configation files: /etc/apache2/
  • localhost root directory: /var/www/
  • PHP ini file: /etc/php5/apache2/
  • PHP extensions configuration files : /etc/php5/conf.d
  • MySql Data files: /var/lib/mysql/mysql
  • MySql Configuration files: /etc/mysql/

PHP Editors for Ubuntu

Gedit is the default text editor of Ubuntu and it support syntax coloring for many languages including PHP. But If you want an advanced editor for programming, you can try Geany or Screem. But, I am sure, who’ve been using advanced IDEs like PHPEd or PHPDesigner in windows, none of this can satisfy him. Don’t worry, the great open source IDE eclips has PHP-Development-Tool. It’s surely one of the best PHP IDE.

Subversion in Ubuntu

Subversion maintains current and historical versions of source code and documentation. It’s very important for distributed application development. RapidSVN is a graphical client for the subversion revision control system (svn) for linux. If you want to use svn from command line, install subversion.

File Difference and Merging Tools for Ubuntu

I have been using WinMerge in windows as file comparison and merging tool along with TortoiseSVN. Now using TkDiff as it’s alternative in linux. TkDiff has advanced functionalities and graphical interface. It provides file-merge and change-summary facilities, line number toggling (for easier cut & paste) and support for Subversion, RCS, CVS and SCCS. To use from command line, diff is may be the simplest file comparison tool.

Ftp and SSH

Ubuntu installs its default programs for FTP, SSH and many other internet applications. But, I’ve liked gFTP as FTP application because of it’s interface is similar to SmartFTP. And for working with SSH, required nothing. Just open the terminal and use ssh command. Write something like ssh username@example.com and press ENTER.

Testing in IE

One of the important (and painful to me) issue of web development is testing the output in IE. Though IE has no version for Linux, you can install it through wine. Wine is a application that crates a virtual environment for installing windows applications in Linux. After installing wine, you can install any available version of IE from http://www.tatanka.com.br/ies4linux/page/Installation:Ubuntu.

When witting this post, my aim was to help a developer who is thinking to switch to Ubuntu Linux or a newcomer here. You are welcome to add your valuable comment/suggestion for the same purpose.

PHP UUID generator function

Posted by admin on February 6th, 2008

Universally Unique Identifier is an identifier standard which is used in a varieties of software construction. When I was writing the RSS Writer class for Orchid, I needed to generate UUID to implement with ATOM id. I searched the web for a simple solution, but didn’t find any that suffices my need. Then I wrote the following function to generate it. I’ve use the standard of canonical format here.

Let’s take a look what Wikipedia has to say about the format of UUID :

A UUID is a 16-byte (128-bit) number. The number of theoretically possible UUIDs is therefore 216*8 = 2128 = 25616 or about 3.4 × 1038. This means that 1 trillion UUIDs would have to be created every nanosecond for 10 billion years to exhaust the number of UUIDs.

In its canonical form, a UUID consists of 32 hexadecimal digits, displayed in 5 groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters.

Enough said. Here is the function.

/**
  * Generates an UUID
  * 
  * @author     Anis uddin Ahmad <admin@ajaxray.com>
  * @param      string  an optional prefix
  * @return     string  the formatted uuid
  */
  function uuid($prefix = ) 
  {
    $chars = md5(uniqid(rand()));
    $uuid  = substr($chars,0,8) . ‘-’;
    $uuid .= substr($chars,8,4) . ‘-’;
    $uuid .= substr($chars,12,4) . ‘-’;
    $uuid .= substr($chars,16,4) . ‘-’;
    $uuid .= substr($chars,20,12);
    
    return $prefix . $uuid;
  }

Example of using the function -

//Using without prefix.
echo uuid(); //Returns like ‘1225c695-cfb8-4ebb-aaaa-80da344e8352′ 
 
//Using with prefix
echo uuid(‘urn:uuid:’);//Returns like ‘urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344e8352′
 

See you.

UPDATE : The PHP Universal Feed Generator class, for which I wrote this function is released.

How to avoid POSTDATA resend warning

Posted by admin on January 12th, 2008

"POSTDATA resend" warning is a common problem when developing web applications. Before discussing about the solution, let’s know what is the problem actually and when it arises.

About the problem :

We see this warning when we try to revisit a page that has accepted POSTDATA using browser’s history mechanism. Usually we do it by browser’s BACK button or refreshing a page. When a page in browser history requested with POST method, the browser thinks this POSTDATA is important to process this page. So, it asks the user if he wants to send the POSTDATA again or not. Different browsers show this warning differently. For example, the images below shows how Firefox and Internet Explorer show this warning.

POSTDATA_resend_mozilla

POSTDATA resend warning in Mozilla Firefox

POSTDATA_resend_ie

POSTDATA resend warning in Internet Explorer

 

Now, If the user accepts the confirmation, the page is reloaded with previously sent data using POST method; otherwise some browsers don’t take any action and some shows a "Webpage has expired" message. In some situations, this re-submitting is useful, but most of the times it’s not expected. It’s more harmful when user accepts it without understanding and causes multiple entries of previously entered data in database.

The easy solution :

The easy solution is simply redirecting. When we redirect a page, the target page is loaded without any POST data that comes with request.

Let’s solve this problem by hand. Say we have 2 pages - A and B. A has a feedback form which submits data by POST request method to B. B inserts the information into database and shows a success message.

Now, if the user try to reload the page by refresh, back button or any other way, browser will show the confirmation message. If user accepts this, the message will inserted again. But after inserting to database, If we redirect to another page C and C shows the success message, the problem will not occur. Because, in the final output to the browser will come from C which didn’t use any POST request data. The code should be something like this:

<?php
if($_POST)
{
  /* Form validation, Inserting to database or anything you want goes here */
  
  /* Now redirect to another page */
  header(‘Location: http://yoursite.com/success.php’);
}
?>

 

Extending the solution :

The solution I explained above can show just an static message like "Thanks for your feedback" or "Registration completed successfully" etc. But usually we like to include some information about sender or submitted information in the success of failure message. For example:

Hi Andrew, your registration is successfully completed. An email is sent to you at andy001@yahoo.com with more information.

In the above message, the purple colored information has to be taken from POST data. But the POST data will be lost when redirecting. So, it’s not possible by the above solution.

What we can do to solve this is that we can use SESSION to store those data which we want to display with message in redirected page. Just like this:

<?php
session_start();
 
if($_POST)
{
  /* Form validatio, Inserting to database or anything you want goes here */
  
  //Store data to session
  $_SESSION[‘name’]   = $_POST[‘name’];
  $_SESSION[‘email’]  = $_POST[‘email’];
  
  //Now redirect to another page 
  header(‘Location: http://yoursite.com/success.php’);
}
?>

 
In the target page, when showing the success message, we can get the information from SESSION easily.
 
Hope this will solve your problem. Please tell me if any confusion or question with this article.  See you again.

Interactive character limit for textarea using Jquery

Posted by admin on November 9th, 2007

For a textbox, character of a field can easily be limited with maxlength attribute. But maxlength doesn’t work for  a textarea. So, It needs an alternate way. For one of my project, I wrote a function for this purpose where I used jQuery. Here I am explaining how I did it.

Let’s look an example of this interactive solution here

OK. Now we will make this in 2 easy steps.

1. Import your jQuery file and write the function "limitChars(textid, limit, infodiv)" in the head section of your page.

This function takes 3 parameters. They are:

  • textid : (string) The ID of your textarea.
  • limit : (num) The number of character you allow to write.
  • infodiv : (string) The ID of a div, in which limit information will be shown .

  Here is the function:

 1: <script language="javascript" src="Jquery.js"></script>
 2: <script language="javascript">
 3: function limitChars(textid, limit, infodiv)
 4: {
 5: var text = $(‘#’+textid).val(); 
 6: var textlength = text.length;
 7: if(textlength > limit)
 8: {
 9: $(‘#’ + infodiv).html(‘You cannot write more then ‘+limit+‘ characters!’);
 10: $(‘#’+textid).val(text.substr(0,limit));
 11: return false;
 12: }
 13: else
 14: {
 15: $(‘#’ + infodiv).html(‘You have ‘+ (limit - textlength) +‘ characters left.’);
 16: return true;
 17: }
 18: }
 19: </script>

Remember, the script is using jQuery. So be careful about the path of jquery at first line of this script.
 
2. Bind the function to the keyup event of your textarea. Do this in jQuery’s ready event of document. Just like this:
 1: $(function(){
 2: $(‘#comment’).keyup(function(){
 3: limitChars(‘comment’, 20, ‘charlimitinfo’);
 4: })
 5: });

Here my textarea’s id is ‘comment’ , limit of characters is 20 and limit information will shown in a div whose id is ‘charlimitinfo’. That’s all, we have made an "Interactive character limiter" for our textarea using Jquery.
 
If you are not using jQuery for your site, it will not be wise to include it only for this script. You can get the same functionality from javascript without jquery. Look at this example. It looks like and works 100% as the previous though not using jquery.
Here you need just 2 changes. First, change the function "limitChars" as follows :
 1: function limitChars(textarea, limit, infodiv)
 2: {
 3: var text = textarea.value; 
 4: var textlength = text.length;
 5: var info = document.getElementById(infodiv);
 6:  
 7: if(textlength > limit)
 8: {
 9: info.innerHTML = ‘You cannot write more then ‘+limit+‘ characters!’;
 10: textarea.value = text.substr(0,limit);
 11: return false;
 12: }
 13: else
 14: {
 15: info.innerHTML = ‘You have ‘+ (limit - textlength) +‘ characters left.’;
 16: return true;
 17: }
 18: }

 
And finally, call the function manually on keyup event of your textarea. Use "this" keyword for textarea instead of id when calling the function. example:
 1: <textarea name="comment" id="comment" onkeyup="limitChars(this, 20, ‘charlimitinfo’)">
 2: </textarea>

Finished. We made the solution again without dependency of any javascript framework.

Please let me know if you have an idea to make it better. 

jQuery controlled Dependent (or Cascading) Select List 2

Posted by admin on November 8th, 2007

Thank you all. Thanx for using, commenting and visiting my script jQuery controlled Dependent (or Cascading) Select List from my old bolg http://php4bd.wordpress.com and here.

I am getting a lots of request and questions from visitors about adding some features and some other problems. Sometimes I faced some problem when using this script myself. So, I have made some changes here. I hope, this change will help you to overcome some problems of previous version of this script. The main features added in this version are:

  1. It will keep in child list box only sub items of selected parent value when initializing.
  2. Added a 4th parameter to input a selected value for child list box.
  3. When "isSubselectOptional" is true, add a default value ‘none’ for ‘– Select –’ option of child list box.
  4. Automatically focus the child list box when a value is selected from parent list box.
  5. More effective for multilevel association.

Click here to see a demo of multilevel association using this function.

Here is the modified function:

 1: function makeSublist(parent,child,isSubselectOptional,childVal)
 2: {
 3: $("body").append("<select style=’display:none’ id=’"+parent+child+"’></select>");
 4: $(‘#’+parent+child).html($("#"+child+" option"));
 5: 
 6: var parentValue = $(‘#’+parent).attr(‘value’);
 7: $(‘#’+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
 8: 
 9: childVal = (typeof childVal == "undefined")? "" : childVal ;
 10: $("#"+child+‘ option[@value="’+ childVal +‘"]’).attr(’selected’,’selected’);
 11: 
 12: $(‘#’+parent).change( 
 13: function()
 14: {
 15: var parentValue = $(‘#’+parent).attr(‘value’);
 16: $(‘#’+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
 17: if(isSubselectOptional) 
 18: $(‘#’+child).prepend("<option value=’none’> — Select — </option>");
 19: $(‘#’+child).trigger("change"); 
 20: $(‘#’+child).focus();
 21: }
 22: );
 23: }

And initialize the association on ‘$(document).ready’, as following example:

 1: $(document).ready(function()
 2: {
 3: makeSublist(’parentID’,‘childID’, true, ‘selected_val_of_child’);
 4: });

If you want to create multilevel association, start the initialization from child most order. such as:

 1: $(document).ready(function()
 2: {
 3: makeSublist(‘child’,‘grandson’, true, ); 
 4: makeSublist(‘parent’,‘child’, false, ‘3′); 
 5: });

For details instruction and example of using this script, please visit the previous version. Feel free to ask me if any problem with using this script or any bug you found.

Windows Live Writer : Blogger’s life made easy

Posted by admin on October 1st, 2007

Windows says, "Windows Live Writer Beta is a desktop application that makes it easy to publish rich content to your blog". Really that. I have been using it recently and found it as an exclusive tool for blogging. But remember, a blogger can use windows live writer only when his blog is build on an organized blog system like WordPress, Blogger, LiveJournal, TypePad, Moveable Type, Community Server, and many other weblog services.

image   image

Windows Live focus themselves on five feature of this writer. They are:

  1. Compatible with your blog service
  2. WYSIWYG editing
  3. Rich media publishing
  4. Powerful editing features and
  5. Offline editing

Ok. Now I am going to tell you what features of this software impressed me most. First I should mention that, before using Windows Live Writer, I have been using WordPress and I was satisfied on It’s editing options. But now, I get the powerful yet easiest application for writing blog and I’m going to stay on it. Here goes my share of the praise:

  1. Offline control on everything: It has powerful editor to write/edit blogs, manage my categories etc on offline. So, it’s fast and easy. Only one thing I have to do is, just tell the writer to publish it. Then it will connect to the blog over online and automatically publish it. Moreover, it lets me see the editing post in different views which helps me to visualize how it will look like at online on my installed theme.
  2. Working with images is easiest than ever:  I get rid from the terrible duty of uploading and managing images to use them in a blog post by using writer. It lets me use image in a post and edit them just like MS Word. When an image containing post published, it manages everything.
  3. More control on Html elements: The web’s WYSIWYG editors are simply nice. But not comparable with writer’s editing facilities. It’s more than a web editor and near about a word processor. It offers smooth and flexible visual building.  
  4. Special inserting tools: Writer’s inserting tools are really rich. It can insert and smoothly manage tables, images, videos, maps(from virtual earth), colorize code snippet(need a free plug-in) etc.
  5. Rich and free Plug-ins: Though Windows Live Writer is at beta version yet, it has a large number of free plug-ins. They may solve more than a blogger needs. You can get the plug-ins here:
  6. http://gallery.live.com/results.aspx?c=0&bt=9&pl=8&st=5

Moreover, who are blogging on hosted version of WordPress, Blogger etc blogging site, they have some limitations of using plug-ins there. Writer may be a great solution for them. It also provides the ping functionality which is very important for promoting a blog.

See more details here: http://get.live.com/en-us/betas/writer_betas

You can download it from here: http://windowslivewriter.spaces.live.com/

“How to add archive functionality to your PHP app”

Posted by admin on September 26th, 2007

“An archive refers to a collection of records, and also refers to the location in which these records are kept. Archives are made up of records which have been created during the course of an individual or organization’s life.” - Wikipedia.

Archive is an essential feature of a well defined blog system. If you have a blog in blogger or wordpress, you’ll get built-in archive feature there. But if you are making a blog yourself or any similar application, you might need to add this function yourself. These are some ideal example of archives:

Omar Al Zabir www.phpfour.com somewherein bangla blog

I’ve done it for one of my app and here I am telling you the time saving tip: creating an archive function in 3 easy steps.

1. Generate organized archive data: Generally, archives are created on create date of contents. Let’s say, your blogs table has a field named create_date which holds the timestamp of the post. Now, you can get your archive data in very organized format with this simple yet powerful query (it’s in MySQL):

SELECT COUNT(id) total, MONTHNAME(create_date) month, YEAR(create_date) year

FROM blogs

GROUP BY MONTH(create_date), YEAR(create_date)

ORDER BY create_date

Note that you’ll need to use where clauses if there are any condition. The output of your query should look like this:

Query to retrieve archive data The result
image image

 

2. Use the archive data: After you have retrieved the archive data, you can display them in any sidebar of your application. A sample code can be as follows:

 

 1: <h4>Older Entries</h4>
 2: <ul>
 3: <?php foreach ($archive as $month): ?>
 4: <li>
 5: <a href="<?php echo $month[’year’] . "/" . $month[‘month’] ?>">
 6: <?php echo $month[‘month’] . " " . $month[‘ year’] ."(" . $month[‘total’] .")" ?>
 7: </a>
 8: </li>
 9: <?php endforeach; ?>
 10: </ul>

So, what’s done here? Well, it displays the archive sets with an appropriate link. Note that the link are created in “clean URL” style, i.e. yourdomain.com/2007/july – if you are using an MVC framework like CodeIgniter or CakePHP, you may find it easy to integrate. You can of course create it in the old style, by changing the above code in this way:

 1: <li>
 2: <a href="blog.php?year=<?php echo $month[‘year’] ?>&month=<?php echo $month[‘month’] ?>>
 3: <?php echo $month[month‘] . " " . $month[’ year ‘] ."(" . $month[’total‘] .")" ?>
 4: </a>
 5: </li>

3. Display related posts: And now you’ll need to handle the fetching of appropriate posts based on the passed archive information (year and month), but that’s something that only you can figure out, as I don’t know what you have in there.

Happy Blogging…Happy Archiving :)

Recent Comments | Recent Posts


designed by: Website Builder | Coded by: Blog Directory | Provided by: Wedding photojournalism chicago
bottom