Knowledge is Power

November 22, 2012

Checking port is open with telnet

Filed under: Uncategorized — krishnabhaya @ 5:08 pm
Tags: , ,

Telnet/port/ssl

I was trying to connect/fetch mail using imap – imap_open does not work with port 143 for gmail on my localhost so have to use port 993 which is (imap+ssl)

To check port is open and working, I used telnet command.


telnet imap.gmail.com 143

It returns “unable to connect to remote host”.

Then I checked it for port 993


telnet imap.gmail.com 993

and it connected.

Note: If telnet fails, everything else will fail, ssl or not
ssl is negociated ONLY AFTER the connection has been established.

function defination-declaration

Filed under: PHP — krishnabhaya @ 4:58 pm
Tags: , ,

Here’s a very basic test case to check return value of function where return value is not specified explicitly. I used php code here.

function a()
{
b();
}

function b()
{
return true;
}
echo a()? "return value of a" :"null";
echo "
";
echo b()? "return value of b":"null";

and the result is:

1st : null
2nd :return value of b

firebug lite

Filed under: Uncategorized — krishnabhaya @ 4:55 pm
Tags: , ,

Copy paste the following code in IE, opera, safari address bar, wait for it to load:

javascript:(function(F,i,r,e,b,u,g,L,I,T,E){if(F.getElementById(b))return;E=F[i+’NS’]&&F.documentElement.namespaceURI;E=E?F[i+’NS’](E,’script’):F[i](‘script’);E[r](‘id’,b);E[r](‘src’,I+g+T);E[r](b,u);(F[e](‘head’)[0]||F[e](‘body’)[0]).appendChild(E);E=new%20Image;E[r](‘src’,I+L);})(document,’createElement’,’setAttribute’,’getElementsByTagName’,’FirebugLite’,’4′,’firebug-lite.js’,’releases/lite/latest/skin/xp/sprite.png’,’https://getfirebug.com/’,’#startOpened’);

May 19, 2011

How to validate json object in php

Filed under: JS — krishnabhaya @ 11:07 am
Tags: , ,

Here is the example code for json object validation in php
It can be used in other languages to just the concern is language should support regular expression
and json encode/decode functions.

if(preg_match('/^\{.*\}$/',$message)){
        $objs = json_decode( stripslashes($message));
        if($objs){
            foreach ($objs as $ob)
            {
                if($ob)	{
                    $is_json = true;
                    break;
                }
            }
        }
        //json but empty values or null values so treat them as normal text
        //$is_json = false;
    }

November 8, 2010

Using Regular expressions in MySQL

Filed under: Mysql — krishnabhaya @ 5:10 am

We ( developers ) are so used to work with Regular expression that we want it to use everywhere.
It’s a great feature rather should say technique and specially very powerful. It’s supported by all languages and used wordwide.

I was not aware of MySQL support for regular expression using REGEXP operator. This post is to all people who might not aware of usage of regular expressions in MySQL.

MySQL has quite different syntax to handle it in sql query.It’s like this

SELECT * FROM tbl_name WHERE field_name REGEXP ‘[[:digit:]]{3}$’;

Explaination: It would select records where field value has mininum 3 digits( 0-9 ) and REGEXP is the reserved word.

similar query with some special character as regular expression pattern

SELECT * FROM tbl_name WHERE field_name REGEXP ‘[[.vertical-line.]][[.vertical-line.]][[:digit:]]{3}’ ;

Explaination: It would select records where field value has mininum 3 digits( 0-9 ), prefixed with ‘||’ .
exmple value : ‘||100’

You can find more details for the same under MySQL documentation : Regular Expressions section

June 8, 2009

Mysql LOCATE and SUBSTRING

Filed under: Mysql — krishnabhaya @ 11:09 am
Tags:

hello friends, mysql provides us many inbuilt functions to get accurate result according to our requirement. Migrating old data with new fields and structure is quite cumbersome and time consuming. Sometimes, we need to update data to adjust it with our new requirement, in that case mysql functions helps a lot. We can do specific operation on lots of existing records by a single query.

As per requirement, I need to add a new field for my table with username initials. In this case, we have already 500 users, and their transaction records. I want to update records for existing user records. I got solution using mysql inbuilt function. Solution is like this

Table name: Users
Fields: userid, username, user_initial (newly added field)

username includes “firstname lastname” combination
i.e. “xyz pqr”, it should update record with initials “xp”

Query:
UPDATE Users set user_initial= concat( substring(username,1,1), substring(username, locate(‘ ‘,username)+1, 1) )

here, it wil take first letter of string and concate it with second initial, where for finding second initial it will first locate position of space in string and then get first letter of it using substring.

Using input_in_place_editor_tag of symfony

Filed under: symfony — krishnabhaya @ 10:52 am
Tags:

input_in_place_editor_tag of symfony is one of the nice feature of ajax. But we have to take care to pass parameters, set values to use it. It contains following parameters:

  1. name of element
  2. URI of an action that is called when update done
  3. cols and rows to show editable area (textarea)
  4. loadTextURL: The URI of an action that is called to display the text to edit.

example:

echo input_in_place_editor_tag(‘price’, ‘item/updateOrder?id=’.$id.’, array(‘size=>’5’, ‘maxlength’=>’10’, ‘highlightcolor’=>’#B0E0E6’,
‘loadTextURL’=>url_for(‘item/getURLValue?id=’.$id.’&type=price’)
));

Please note here, i have used ‘url_for’ here for loadTextURL because, i have faced problem of getting value, in case of your application is full ajax application, and it may contain different URL in address bar of browser. If your browser has some other url with sub modules and action, and that time specifying simple module/action will not work as it will search for that module/action according to url in browser (module in other module). To solve this problem, you have to make it independent of browser url and that can be done using ‘url_for’. We will get full URL using it so there is no chance of confusion with browser url during ajax call.

May 22, 2009

Get list of libraries installed in Perl

Filed under: Perl — krishnabhaya @ 10:16 am
Tags: ,

CPAN provides very nice facility for installing libraries from terminal only. It shows library is up to date, if we try to re-install the same library. Though we sometimes require to get list of libraries installed on server as list, as checking each library individually is cubersome. I came to know about two command provided by perl, which shows list of libraries.

1.perldoc perlmodlib

It will show list of libraries which are part of perl installation and default installed.

2.perldoc perllocal

It will show list of libraries installed and which are not part of default libraries included at time of perl installation.

Can’t locate installed library in Perl

Filed under: Perl — krishnabhaya @ 10:03 am
Tags:

Sometimes it happens that we installed any library/module (in case of mod_perl) successfully, but then also, internal server error comes (500) and shows, that server can’t locate that library. At that time first check the server log, for missing library name and also check location it is searching the library. Normally, server searchs in @INC, default location where all libraries stored. But some libraries, due to some version problem or any reason not located on same location at time of installation. To use the same library in our script file, we have to explicitly tell server to check other locations also for locating libraries. That can be done like as follows:

* assuming you are having root access on server. so can run commands on terminal.

You will get library name and path from error log (in case of internal server error)

Example:
I want to use XML::Writer library.
It’s located in /usr/local/lib/perl5/site_perl/5.8.9/XML/Writer.pm

Server Error like :

Can’t locate XML/Writer.pm in @INC (@INC contains: /usr/local/apache/perl

1. Get exact location of library.
command :
locate XML/Writer.pm

result :
/root/.cpan/build/XML-Writer-0.606-85Lgu7/Writer.pm
/root/.cpan/build/XML-Writer-0.606-85Lgu7/blib/lib/XML/Writer.pm
/usr/lib/perl5/vendor_perl/5.8.8/XML/Handler/CanonXMLWriter.pm
/usr/lib/perl5/vendor_perl/5.8.8/XML/Handler/XMLWriter.pm
/usr/local/lib/perl5/site_perl/5.8.9/XML/Writer.pm

2. Check the result of command select the path exactly matching with your library. Then include it’s parent directory to your file, so server will search for library in that directory also.

example: .pm ,.pl file

start.pm
use lib qw(/usr/local/lib/perl5/site_perl/5.8.9)
use XML::Writer;

3. Now, server will search “/usr/local/lib/perl5/site_perl/5.8.9” directory also (including default @INC) to locate libraries, so it can find and locate particular library.

May 11, 2009

Redirect from HTTPS to HTTP

Filed under: Uncategorized — krishnabhaya @ 6:28 am
Tags:

This post is related to my old post. We do not require “https” for all pages. So there is a stage where we need to redirect page from secure to unsecure page. Here is a PHP code to get request detail and redirect it to http request.

$encport =’443′;

if($_SERVER[‘SERVER_PORT’] !== $encport || $_SERVER[‘HTTPS’] !== “on”)
{
header(“Location: https://”.$_SERVER[‘SERVER_NAME’].$_SERVER[‘SCRIPT_NAME’]);exit;
}

Next Page »