Wednesday, 19 October 2011

How to calculate time difference in php

We can use time() function to calculate the difference between 2 dates in PHP. The time() function returns the current time as a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

The below function  returns the number of minutes, hours, days and months between 2 timestamp.

     * Minutes  =  60       seconds
     * Hour     =  3600     seconds
     * Day      =  86400    seconds 

     * Month    =  2592000  seconds

function timeAgo($time) {
   //get the time difference between current time and login time

   // month difference

   if (($timeDif/2592000)>=1) {
           return floor(($timeDif/2592000))." month";
       else {
          return floor(($timeDif/2592000))." months";
    //day difference
    elseif (($timeDif/86400)>=1) {
      if(floor(($timeDif/86400)) == 1){
         return floor(($timeDif/86400))." day";
         return floor(($timeDif/86400))." days";
    //hour difference
    elseif(($timeDif/3600)>=1) {
      if(floor(($timeDif/3600)) ==1){
         return floor(($timeDif/3600))." hour";
         return floor(($timeDif/3600))." hours";
    // minute differnece
    elseif(($timeDif/60)>=1) {
      if(floor(($timeDif/60)) ==1){
         return floor(($timeDif/60))." minutes";
         return floor(($timeDif/60))." minutes";
    }else {
    return "less than 1 minute";

Tuesday, 18 October 2011

How to decrypt the md5 encrypted password in PHP

md5 is supposed to be a one way encryption. But we can validate the password. First create an md5 hash of the password supplied by the user, and compare that with the encrypted password in the database.

This example shows how to change the md5 encrypted password in the database 

     $username = $_POST['user']; // get the username from the  form

     $currentpassword = $_POST['password']; // get the current password from
                                                                           the form
     $newpassword = $_POST['newpassword']; // get the new password from the
     $hash = md5($currentpassword); // encrypt the current password supplied
                                                            by the user

     $result = mysql_query("SELECT password from Users  where
     username='username' ");  // query the database for getting password for a
                                                   the user.

     $row = mysql_fetch_array($result);

     $p=$row[password]; // get the encrypted password from the database for the

           $result = mysql_query("UPDATE members set       
                           password=md5('newpassword') where username='username' ");
      } // update the new password for the user in the database in encrypted form,
            only if the encrypted current password and the password stored in the
            database are same

Monday, 17 October 2011

Need to import my database - Error 1046

Issue : I am trying to import a copy of a my website sql database(developed using mysql 5) . When I try to import the backup.sql file via phpmyadmin in xampp 1.7.4, I get the following error.

MySQL said: 

#1046 - No database selected


For the database import, before we attempt to import the "flat" file data in, we need to goto our phpAdmin and create new database and import tables to that database.



Thursday, 13 October 2011

Installing XAMPP on Linux

XAMPP is an easy to install Apache distribution containing  Apache web server, MySQL, PHP, Perl, a FTP server and phpMyAdmin. XAMPP is  very easy to install in Linux

Installation steps
1. Download the latest binary of XAMPP from the Apache Friends Web site
2. Copy the zip file (xampp-linux-1.7.4(2).tar.gz)in to a folder
3. Open the terminal within the folder.
4. Login as Root.
    cluster3@cluster3-desktop ~/Public/Lekshmi $ su
4. Untar it to /opt using the following command:

    tar xvzf xampp-linux-1.7.4.tar.gz -C  /opt

5. XAMPP is now installed in /opt/lampp folder.
6. Open the /opt/lampp folder.
7. Open the terminal in the lampp folder and login as root
8. Start the lampp server using the command

     /opt/lampp/lampp start
     cluster3@cluster3-desktop /opt/lampp $ su
     cluster3-desktop lampp # /opt/lampp/lampp start
     Starting XAMPP for Linux 1.7.4...
     XAMPP: Starting Apache with SSL (and PHP5)...
     XAMPP: Starting MySQL...
     XAMPP: Starting ProFTPD...
     XAMPP for Linux started.
9. XAMPP is now up and running.
10. Open a browser and type locahost in the address bar and hit the enter key.
      You should be redirected into Xampp home page.

Configure Xampp and Eclipse PDT in LINUX

On configuring Xampp on eclipse PDT , we can make our PHP development easier.
Here are the steps to be followed to configure Xampp on eclipse PDT.
step1. Download  Xampp
           Download xampp from Apache Friends Web site

           For Installation instruction click the below link

step2. Start the XAMPP Server by typing the command. 

     cluster3-desktop lampp # /opt/lampp/lampp start

           Open the browser and type in the url(http://localhost). You can see the
           XAMPP server page up.

step3. Download Eclipse PDT
           Download eclipse PDT from eclipse website

step4. Extract the downloaded file

     tar -xzvf eclipse-php-helios-linux-gtk-x86_64.tar.gz

           The ‘eclipse’ folder will be created in your current working directory
step5. Create a WorkSpace directory for the eclipse in the root directory
          of  Xampp
            When it asks you to select a Workspace browse to /opt/lampp/htdocs
            where   you installed XAMPP. The reason is that when you come to run
            your PHP program Eclipse will generate a URL and expect the web
            server to find the file and serve it to the browser.
      cluster3-desktop htdocs # mkdir /opt/lampp/htdocs/Demo

            This will create a "Demo" folder in htdocs folder of xampp server.
step6. Give full permission to the directory

           cluster3-desktop htdocs # sudo chmod -R 777 /opt/lampp

step7. Start Eclipse and select a Workspace as /opt/lampp/htdocs/Demo

step8. Now  Eclipse will open.
           Create a ‘New PHP Project’ and Create a New File in that with any name
          (For Example ‘demo.php’)
step9. Select your project directory in eclipse and then click on
step10. Navigate to the ‘PHP’ Tab and expand it on the left hand side of the ‘Preference’ window. 
step11. Click on ‘PHP Executable s’. Click on ‘Search’ and locate where the /opt/lampp is
installed and then click ‘OK’. 
step12. Run demo.php 
           Right click demo.php and select Run As -> PHP Web Page

XAMPP – “Another daemon is already running” issue

When you try to start the xampp1.7.4 server from the terminal it is showing

cluster3-desktop lampp # /opt/lampp/lampp start
Starting XAMPP for Linux 1.7.4...
XAMPP: Another web server daemon is already running.
XAMPP: Starting MySQL...
XAMPP: XAMPP-ProFTPD is already running.
XAMPP for Linux started.

To  solve this problem you only have to type: 
sudo /etc/init.d/apache2 stop

The web server will be stopped now.

cluster3-desktop lampp # sudo /etc/init.d/apache2 stop
 * Stopping web server apache2                                                  apache2: Could not reliably determine the server's fully qualified domain name, using for ServerName
 ... waiting
cluster3-desktop lampp # /opt/lampp/lampp start
Starting XAMPP for Linux 1.7.4...
XAMPP: Starting Apache with SSL (and PHP5)...
XAMPP: XAMPP-MySQL is already running.
XAMPP: XAMPP-ProFTPD is already running.
XAMPP for Linux started.