Hidden Frame Pattern / Pre-Ajax

December 31, 2009

This is one of the knows patterns in the web devs. The idea would be having two – or possibly more – frames and making one the front one for interaction while would be HIDDEN.
Why Hidden?
The visible frame would pass make a request to the hidden frame to make the server side interaction behalf of it. Doing so would make the visible frame to remain loaded. The hidden would play the rest of the game by consulting the server and grabbing all the required data. Of course, how do they talk? well, Mr. Javascript will be the ‘man in the middle’ – don’t wanna mention the security flaw known by this name ;)

Let’s see a quick example: the example will show how to interact with the server side script without page reload. For demo purpose, A page will accept first name and last name and will display the concatenated full name. Though the logic done is is too easy to accomplish, one has to bear in mind that any complex logic can be done in the same way.

First create a simple index file containing the frames

<html>
	<head><title>Lab01</title>
		<frameset rows="100%, 0" style="border: 0px">
			<frame name="visibleframe" src="enterName.html" noresize="noresize"></frame>
			<frame name="hiddenframe" src="about:blank" noresize="noresize"></frame>
		</frameset>
	</head>
</html>

Making the frameset row 100% will assure the full visibility.
Then have the page for first and last name.
enterName.html.


<script>
	//get full name by delegating the task to the php
	function getFullName(){
		var firstName = document.getElementById("txtFirstName").value;
		var lastName = document.getElementById("txtLastName").value;
		top.frames["hiddenframe"].location = "concateName.php?firstName="+firstName+"&lastName="+lastName;
	}
	//display it on the front page
	function showFullName(fullName){
		showdiv = document.getElementById("fullName");
		showdiv.innerHTML = fullName;
	}
</script>
Your First Name <input type="text" id = "txtFirstName" name="txtFirstName" /> <br>
YOur Last Name<input type="text" id="txtLastName" name="txtLastName" /> <br>

<input type="submit" name="subGetFullName" value = "concate" onClick="getFullName();" /> <br>
<textArea id="fullName" name="fullName"></textArea>

This file contains two javascripts: one for sending the form values to the hidden frame and the other to accept and display the result from the hidden frame.
Since the hidden frame had no source initially, the getFullName() function would be responsible for assigning the file to it.

Now lets build the server side script which will combine and return the full name
concateName.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head><title></title>
    <script>
    window.onload = function(){
       var fullNameDiv = document.getElementById("fullName");
       top.frames["visibleframe"].showFullName(fullNameDiv.innerHTML);
    };
    </script>
    <?php
        $firstName = $_GET['firstName'];
        $lastName = $_GET['lastName'];
        $fullName = $firstName . $lastName;
    ?>
    </head>
        <body><div id="fullName"><?php echo "FULL name is " . $fullName ?>
        </div></body>
</html>

Now we are done. :)
The concateName.php would get the parameters from the enterName.html through hiddenframe. Once it access the values, it will concatenate them and will deploy the result in the “fullName” div.
The defined window.onload would wait until things are in place and it will fire the result back to the “visibleframe” through javascript.
Now – the user has no what is going on behind he scene – as she will not see the page being reloaded on refreshed.

reference: Professional Ajax 2nd edition Wrox


Using SSH in Ubuntu

December 30, 2009

The idea behind using SSH (secure shell) falls when one is interested in sending files over the network in secure way. There are existing methods which will accomplish the task, but SSH is a cool one when one wants real security.
Here is the step to send files securely from your machine to the other.

1. install ssh client and ssh server using
sudo apt-get install openssh-client openssh-server

2. log into the server using the command
ssh username@server
where username is the user at the server. This can be checked on local machine as ssh user@localhost

3. Now we can share files using the tool scp
to upload file use:
scp path/to/file/to/upload user@server:/path/to/host/directory
Say, to upload the file test.txt from local machine to server named someserver.com into directory /home/remote, then
scp /home/user/test.txt user@someserver.com:/home/remote

And to download file from the same server to local machine
scp user@someserver.com:/home/remote/test.txt /home/user/downloads

This one required logging every time with subsequent request of password. To setup a secure file transfer without password the way to go would be using public private combination between server and client.

Generating and using public private key
1. Creating Public Private key can be done using:
ssh-keygen -t dsa – or if you want to make rsa: ssh-keygen -t rsa
2. it will prompt you to where to put the file and the passkey for the files. take default file path. This would allow the creation of id_dsa (private key) and id_dsa.pub (public key) into the ssh directory. -> The directory would be into /home/user/.ssh/. The private key would retain with the generating server while the public key would be uploaded to the server where you would create the connection.
3. Make sure the following are available and not commented in /etc/ssh/ssh_config
IdentityFile ~/.ssh/identity
IdentityFile ~/.ssh/id_rsa
IdentityFile ~/.ssh/id_dsa
4. Make sure you have id_dsa, id_dsa.pub or rsa versions on .ssh folder
5. Copy the public key to the remote server using
scp /path/to/publickey user@server:/home/user/.ssh/ – where user is the authenticated user on the remote server
6. You will be asked for one last time your password for copying the file.
7. The file will now be added to the remote server’s authorized_keys. This will give you an access from your machine to the remote server without password – unless you want it to.

reference:

http://www.techotopia.com/index.php/Configuring_Ubuntu_Linux_Remote_Access_using_SSH

http://outhereinthefield.wordpress.com/2007/10/31/passwordless-scp-authentication-for-unattendedbatch-process/

http://www.debuntu.org/ssh-key-based-authentication


403 Forbidden You don’t have permission to access .. on windows pc

November 22, 2009

Was trying to access my PHP web project from my Vista on my Ubunut and end up running error 403 Forbidden You don’t have permission to access on this server …
Well this has a very easy remedy:
BTW – I am using Uniserver – http://www.uniformserver.com/ – [ By the way it is the coolest development environment on windows as you would get PHP, MySQL, and PhpMyAdmin residing all at one place. But it doesn't matter if you are using other tool.
1. Go to where your www folder
2. open .htaccess file using your favorite text editor - note pad is more than enough for this.
3. Under Allow From 127.0.0.1 - or whatever it is allowed Add Allow From [your ip address of the computer from which you want to access the server]

A walk in the park! :P


Inserting data to table reading from another table in mysql

November 20, 2009

OK, we can use INSERT INTO statement to insert fresh data to mysql. But how about if the need lies on reading data from another table?
Here is how we do it in mysql.
Let us create the tables for illustration:
Log into your mysql and create the following queries
Creating database
CREATE database testdb;
USE testdb;

Creating tables

CREATE TABLE testtbl1 (id int(10) auto_increment, fname varchar(20), lname(20), primary key(id));
CREATE TABLE testtbl2 (id int(10) auto_increment, firstname varchar(20), lastname(20), age int, primary key(id));

Lets add some data to table 2 now
INSERT INTO testtbl2 (firstname, lastname, age)
VALUES
(‘ftest1′, ‘ftest1′, 35),
(‘ftest2, ‘ftest2′, 21),
(‘ftest3, ‘ftest3′, 25),
(‘ftest4′, ‘ftest4′, 38)

Now lets add data to the first table filtering persons with age less than 30

INSERT INTO testtbl1 (fname, lname) SELECT firstname, lastname FROM testtbl2 WHERE age <= 30;


Can’t use method return value in write context – php Error

November 14, 2009

Yup, just to share – as usual.
While working on one of my projects I run this error. Here is the snippet where the error occurs:

....
        $carPrice = $this->getCarPrice(); // this would return object of CarPrice
        if (empty($carPrice->getCarPriceId()))
        {.....

Where $carPrice is an object to be returned after passing an array of data ($new_price) to the controller.
And on the next line, I was trying to if priceId of the car is not empty.
The problem is caused on the second operand of the if statement (empty($carPrice->getCarPriceId()) -
Here is how I get fool around..

...
$car_price_id = $carPrice->getCarPriceId();
if (empty($car_price_id))
{
...

It happens that function empty() [and even isset() ] do expect a variable to be checked otherwise, parse error will happen.


Adding Java Project to new Java web application on Eclipse

September 10, 2009

I just wanted to add my existing Java project to the current java web project and here is the simple step actually [of course from Eclipse :) ]

1. Right click on the web project and select Properties
2. Select J2EE Module Dependencies
3. Here either you can select the project which resides in the eclipse workspace or you can import additional external jars.
4. Apply OK
5. Right click web project and select Build Path -> configure
6. On the projects tab, select the project tab
7. Click Add and select the project that you would like to add
8. OK.
You are done! It is advisable if you clean your project and build a new one

To to do this:

Go to project -> clean and select your project
Select start a build immediately
Select build only the selected projects

After this reboot your Application Server – and enjoy with your imported project.


Networking Vista and Ubuntu

July 19, 2009

I was having some issues with Ubuntu and I wanted to re-install it. The thing is I have a lot of files there and wanted to use networking to move the data to my Vista box.
I have gone through the following steps to do so:
Assume you have a folder named data_ubuntu where all your folders and files are residing.
** in my case the computers are using wireless network
1. Go to System->Synaptic Package Manager and select and install samba from the list if it hasn’t been done already.
2. Make sure the samba version is above 3.2 and if not upgrade it.
3. Then log out and log in
4. Go to your data_ubuntu folder right click on it
5. Select sharing option and do accordingly. In my case I have set share this folder and guest for easy access.
6. Go to terminal [Applications->Accessories->Terminal] and set access permission to the shared file for others:
sudo chmod -R 775 /path/to/file/to/share/
7. This is all regarding the ubuntu part.
8 Go to your network browser from your vista machine [ Go to start menu and select Network from the right pane] and you should be able to see the name of your ubuntu machine. It may take some minutes before loading your ubuntu machine.
All set to go!


Adding auto_increment after table creation

May 18, 2009

Say the table is created like this,

mysql> CREATE TABLE customer (id int, fname varchar(20));

The above table would be created without having an index. If there is a need to make id primary key, then

mysql> ALTER TABLE customer ADD PRIMARY KEY (id);

Then adding auto_increment would be as:

mysql> ALTER TABLE customer CHANGE id id int NOT NULL AUTO_INCREMENT;

Trying to assign auto_increment before making it a primary key – if it is not already done during creation, might produce an error. Actually trying to drop the primary key before neutralizing the auto_increment would also might create an error.
The following query would take out the auto_increment part from the table

ALTER TABLE customer CHANGE id id int not null;

And, of course, after this it is possible to drop primary key.


Renaming Tables and Columns in SQL Server

May 8, 2009

Changing table or colum in SQL Server is as follows,
To change the name of the table:
sp_RENAME ‘tbloldname’, ‘tblnewname’;
To chagne the name of the column:
sp_RENAME ‘tbl.columnoldname’,'columnnewname’,'COLUMN’;

** You might face a problem on changing names of enforced (integrity) columns.


Reading CHM files in Ubuntu

May 3, 2009

There are a bunch of tools for reading CHM (Microsoft compiled help files) on Ubuntu-Linux.
A number of them are listed on the site http://www.ubuntugeek.com/how-to-view-chm-microsoft-compiled-html-help-files-in-ubuntu.html
I have tried a number of them but I like xCHM one as it keeps the tree navigation easier.
The whole installation stuff is clearly listed on the given link.