Tag: php

  • Why PHP is So Popular on Web?

    If you ever wondered why the hell PHP is so popular on the web when there are better and powerful languages available?

    Well, the answer is very simple, It is only language that allows even musicians to build sites for themselves.

    By the way that is also the reason why WordPress has become the king of CMS and my choice for starting a WordPress development team – WPoets.

  • How To Catch PHP Fatal Error In CodeIgniter

    My last few blog post’s are result of the project that I am currently working on, and this blog is also in that series.

    We needed to catch PHP Fatal Errors in the production environment and notify about it to developers, also at the same time, showing our beautiful fail whale page to user instead of ugly error or a white screen of death.

    Here I will show you how to do this in CodeIgniter, though credit for this idea goes to hipertracker.

    First of all you will need to setup a hook, so update your hooks.php file with following code

    	$hook['pre_system'][] = array(
    	    'class'    => 'PHPFatalError',
                'function' => 'setHandler',
                'filename' => 'PHPFatalError.php',
       	    'filepath' => 'hooks'
    );

    Now put the code shown below in PHPFatalError.php file in your applications hooks folder.

    This is the simplifed version of what I have done, so please update handleShutdown function as per your needs.

    Check out PHPCamp a place to share news, views and articles that are useful to PHP community.
  • Where is MySQL Gone Away?

    Last Sunday I got to work on a very interesting problem in WordPress which I initially thought could be solved in like 5 mins, but alas it took me almost 7 hours before I found and fixed the problem.

    Let me describe the problem, I was running a simple xml parsing script whose task was to parse the xml file and insert the content into WordPress database as a post, everything was working fine except the ‘INSERT’ statement was failing with out any errors. Basically everything would run but nothing would get inserted into database and no errors. We had used the ‘wp_insert_post’ function in  ‘post.php’ file to handle the insertion of post, which was returning ‘0’ instead.

    After lot’s of time spend checking and cross checking the sql statements for error and PHP code logic, i finally found the problem which was a small kinda cryptic error ‘MySQL server has gone away’ for every single query that was getting executed in the script.

    Well, a quick google search took me to MySQL manual page where it list bunch of possibilities on why the error might be coming.

    To me the most logical one were

      1. You tried to run a query after closing the connection to the server. This indicates a logic error in the application that should be corrected.
      2. A client application running on a different host does not have the necessary privileges to connect to the MySQL server from that host.
      3. You have encountered a timeout on the server side and the automatic reconnection in the client is disabled (the reconnect flag in the MYSQL structure is equal to 0).
      4. You can also get these errors if you send a query to the server that is incorrect or too large.
      5. You are using a Windows client and the server had dropped the connection (probably because wait_timeout expired) before the command was issued.

    I investigated each one but it turned out that because a query was taking a bit to long to execute MySQL closed the connection and refused all further request from the client.

    So you might be wondering what was the Solution to this problem.

    Well Rob of Rob’s notebook had the almost perfect solution for it. He created a replacement file for ‘wpdb.php’ which takes care of this problem, yeah it is temporary and you have to remember to replace this file every time you do an WordPress upgrade but it works.

    If you are facing this problem go download it and replace you ‘wpdb’ file and save yourself some time.

    Check out PHPCamp.net a article sharing website relevant to our own PHP community
  • Warning: headers already sent

    When I was learning PHP their were few things that had stumped me, I would usually take it hours to solve them if not days. Now I have grown up, and I realize that those were the basic things that should have been mentioned in the PHP manual, but they were not.

    Today I am going to write about one such annoying problem that almost always comes in the way of PHP developers at least once.

    Warning: Cannot modify header information – headers already sent by (output started at C:wampwwwaktesterror.php:2) in C:wampwwwaktesterror.php on line 4

    Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by (output started at C:wampwwwaktesterror.php:2) in C:wampwwwaktesterror.php on line 3

    This is “header already sent” warning message, that we get whenever we try to redirect a user to some other page or location, when we try to set a cookie or if we try to start a session. Once we get this warning we know that page won’t redirect, session won’t start or cookie’s sent to user, So we can’t even ignore them.

    Before we see the solutions to resolve this warning let’s see the source code of the error.php file that I used to generate this error.

    
    <?php
    session_start();
    header("location:http://localhost/");
    ?>

    Please notice an empty line before “<?php”, this is the cause of errors in his page.

    (more…)

  • How to create multicolor text image on the fly

    This tutorial explains how we can create dynamic images with multicolor text in PHP.

    We will need GD library and FreeType library to be enabled for this functionality to work.

    I have assumed that you already know PHP, and understand how to use it.

    Check the example below to see what we want to create.

    Amit Singh

    Before we start the tutorial, let’s check if GD library support is available on your system or not.

    Type phpinfo(); in a php file, and check the output.

    phpinfo_gd

    You should see something like that you can move forward, else goto php.ini and remove the comment from extension=php_gd2.dll. Now check phpinfo() again to verify that every thing is ok.

    Now, let see how we can create dynamic images using PHP.

    CODE

    Header ("Content-type: image/png");
    
    putenv('GDFONTPATH=' . realpath('.'));
    $font = 'nevis.ttf';
    $fontSize=20;//in point;
    
    $title1= 'Amit';
    
    $title2= 'Singh';
    
    $str=$title1.'   '.$title2;
    
    $onecharwidth  = imagefontwidth($font)*($fontSize/8);
    
    $totalwidth  = $onecharwidth * strlen($str);
    
    $height = (imagefontheight($font)*($fontSize/8))*2;
    
    $img_handle = imagecreatetruecolor($totalwidth, $height);
    
    $white = imagecolorallocate($img_handle, 255, 255, 255);
    
    imagefill($img_handle, 0, 0, $white);
    
    $black = imagecolorallocate ($img_handle, 0, 0, 0);
    
    $gray = imagecolorallocate ($img_handle, 100, 100, 100);
    
    imagettftext($img_handle, 20, 0, 10, 20, $black, $font, $title1);
    
    $posarr=imagettfbbox(20, 0,$font, $title1);
    
    imagettftext($img_handle, 20, 0, $posarr[2]+$onecharwidth, 20, $gray, $font, $title2);
    
    imagepng ($img_handle);
    
    imagedestroy ($img_handle);

    (more…)

  • New Zealand Daylight Savings Time (DST) has changed

    just wanted to post a quick note about this, originally it was supposed to finish on 16th march but it has being extended  to 3rd April.

    if your php script is giving you one hour late time then here is the solution

    just update your timezone database from  http://pecl.php.net/package/timezonedb

    you might need to compile it for your system, alternatively you can also update the php to it’s latest release.

    here is the link if you need more  info http://pecl.php.net/bugs/bug.php?id=12151 .

    Enjoy!!!

  • Enterprise PHP


     

     

     

     

     

     

     

     

    I will not say anything but let Ivo Jansch speak for us in this slide show.