Monday, March 28, 2011

Facebook canvas application and cookie issue with Internet Explorer

The Internet Explorer does not allow cross domain cookies for iframe. This causes the Facebook iframe application to not be able to write Facebook authentication cookie on the client side and it tries to authenticate each time you try to access any page of the Facebook app. This problem does not exist for Firefox.
This can be easily solved using P3P header. Here is how this can be done in PHP
header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
use this code wherever you write some cookie. This is harmless for Firefox.

Zypper/Yast: Download all RPM packages first and then install

Yast is the package management tool in open SuSE and its back-end is zypper. One of frequent issue that I have faced with zypper is that it installs a RPM package as soon as it downloads. It does not download the dependent RPMs before installing a RPM package. Sometime this leads to broken installs if there is some network problem while downloading the dependent RPMs. The problem becomes even worse if there is some network problem while the system update is going on. Once I had a broken update for KDE and I was not able to log into the GUI.
The behavior of zypper can be changed by config file. But I do not understand why the Open SuSE comes with behavior by default. The other popular Linux destro Ubuntu does not have this problem.
Anyway, I here is the solution to the probelm:-
Search for the line
commit.downloadMode
in 
/etc/zypp/zypp.conf
By default this line is commented. Uncomment this line and change it to  
commit.downloadMode = DownloadInAdvance



Tuesday, February 22, 2011

Creating facebook test user account for testing your facebook application

If you are developing some Facebook application then you will need to test it with different user accounts. You will even need network of user account meaning accounts with friends. You would not like to do the testing with your real Facebook account because while testing you might be posting lot of junk data to your wall and this will get published to your friends news feed. You even might require adding new friend and removing friends for the testing purpose.
One option is to create fake accounts. But this is not clean way of doing. You will have to create so many fake email account and you will have to fill lot of fake user info. And another thing is that Facebook deletes fake account once they find it as fake.
There is other clean way of doing. Facebook provide API to create test accounts. These accounts will not be deleted by Facebook. Unfortunately Facebook currently does not provide any GUI to create test account. You will have to use Facebook API to achieve this. 
Here are the things that will be required to achieve this:-
1) A web browser (preferably firefox, I have not tested it on other browsers)
2) wget command line utility. This is freely available and most of the Linux distro has this by default. ( other tools like curl can be used but I am describing here with the help of wget). The reason we use wget is that we need HTTP post method.

Step 1: Get the application access token.
Type this URL in web browser.
https://graph.facebook.com/oauth/access_token?client_id=XXXXXX&client_secret=XXXXXX&grant_type=client_credentials

Replace XXXXX appropriately with your app ID and app secret.

This will give you access token. In the browser you see content like
access_token=XXXXX
Please note down this access token.

Step 2: Create test accounts.
We will need wget command line utility for this. Run this command line utility to create a test user
wget --post-data "installed=true&permissions=read_stream&access_token=TOKEN" https://graph.facebook.com/APP_ID/accounts/test-users
Replace TOKEN with what you got in the step 1 as access token and replace APP_ID with your Facebook application id.
Run this command as many times as many users you want to create.

Step 3: Get the login URL for each users:
Type this URL in you browser
https://graph.facebook.com/APP_ID/accounts/test-users?access_token=TOKEN
Replace TOKEN with what you got as access_token in step 1 and replace APP_ID with your Facebook application ID.

This will give you Info for each test account created. The info also contains login URL. Copy and paste the URL in browser to login into the test user account.

Few more things:
- The login URL is valid for one session only. You will have to repeat the step 3 to get new login URL.
- Access token is gets expired after some time. repeat the step 1 to get fresh access token.
-  Refer the facebook documentation here for more detail
http://developers.facebook.com/docs/test_users/



Monday, February 7, 2011

MySql reseting root password

I was experimenting MySql yesterday and set some password for the root account. But today I forgot. Doing little bit of search on the net, I came across a simple way to reset the password. For this the root access of the OS is needed. Here are the steps:
- Log in as root on the SqlServer box
- stop the MySql server. command: /etc/init.d/mysqld stop
- start mySql in safe mode. command: mysqld_safe --skip-grant-tables
- The previous command will not free the terminal. So from other terminal (command prompt) do these-
mysql --user=root mysql
update user set Password=PASSWORD('new-password') where user='root';
flush privileges;
exit


- Kill the mySql daemon. Command: killall mysqld

- start the mysql daemon. Command: /etc/init.d/mysqld start

Thursday, January 13, 2011

Selecting random record in MySQL

The random number generators are often used in almost all  kinds of application. The random numbers are generated using well known random number generator API. But what about selecting a random record from a database. There are cases we need this. for example- if you want to suggest a random picture to user in a web application. Pictures are stored in database. Fortunately databases provide this kind of feature but unfortunately different databases have different query to achieve this. Here is how this is achieved in MySQL  - 
SELECT columnName FROM tableName
ORDER BY RAND()
LIMIT 1

The value of limit can be changed to number of records to fetch.

Tuesday, January 11, 2011

Scripting langauges

I have always been working with the compiled and statically typed languages like C, C++, Java, C#. I used to avoid scripting languages and also used to hate little bit these scripting languages. But recently started exploring PHP and JavaScript and it changed my perception. Now I started loving these languages. Programming in the these languages needs little bit of different mind set. It takes some time to think in terms of loosely typed coding style.

There are, of course, pro and cons. Here is what I have started observing about the scripting languages -

- You dont need to think in terms of types of data. Most of the time this is very good but some time this is very problematic, especially when you are dealing with string and integer together. In such case you need to explicitly cast to other type. Here is one PHP code which confused me at first glance -
$var = "false";
if ($var) {
echo "This is true";
} else {
echo "This is false";
}

First time when I was dealing with similar code, I was expecting it to print "false". But this prints true. I was thinking, as this is loosely typed language, it will convert the string to boolean automatically. But later found that the string is converted to boolean on the basis of null check. There are other instances, which are potentially confusing. This proves that the these languages are not completely type-less. Sometime you are forced to think in terms of their type and and some complicated rules about the conversion from one type to another.

- You dont need to compile! good or bad?? As I am coming form the background of complied languages, I am feeling this is as a bad thing. The compiler catches lot of error at the compile time but here I have to wait for runtime even for common syntax errors.

- Scripting languages saves you from writing lot of code that you end up writing in languages like Java or C#. Particularly writing the interfaces and thinking what class will implement what interface. As the function name and variable name resolution happens at run time, you can pass around object of any class provided that object
has that particular method or variable with that.

- The scripting language makes your life easier for most frequent operation on string, array, pattern matching etc.

Sunday, December 12, 2010

Translating C to assembly

When you compile a C code, the c compiler translates the c code into assembly language. Then the assembler translates the assembly to machine instruction code.
During my college time (B.Tech from IIIT - Allahabad), I did some reverse engineering of how a C code is translated into assembly by looking into the generated assembly code by gcc compiler.
Now I have put all those things in a form of online book.
Here is the link to the book.