Friday, 24 October 2014
Sunday, 19 October 2014
txtweb sample hosting app
LET'S START!
The blog will be split into two parts, in this part we will achieve requirement 1 and 2, and publish it on txtWeb.
STEP 1:
Create a file joke.php with the following content:
Upload it to server, and access the file on browser http://yourserver.com/joke.php this verifies that everything is set up properly.
(By server I mean the local server you have installed. If you don’t have a local set up you can upload directly to your hosting server)
STEP 2:
Register a keyword on txtWeb.com and get it’s app-key.
In this case app-key = f125f2b5-2567-4123-9b7f-07aae823c8be
STEP 3:
Add app-key to joke.php. Now the file looks as below:
STEP 4:
Move your application to hosting provider, Link application to keyword on txtWeb.com and access it via mobile!
- Sign up an account in any free hosting provider.
Go to http://www.freewebhostingarea.com and create your account.
- Now sign in – http://orgfree.com
- Get your ftp detail (On the right side of page)
- Now upload the file joke.php using the given ftp details.
You can use a native ftp client or the web client here:
http://orgfree.freewebhostingarea.com/ftp/
(Login, Click UPLOAD, choose file – submit! Note: Upload to root “/” directory)
- Link keyword to your app url and click save!
- Send @myjoke to txtWeb and get “HELLO” as response
STEP 5:
Before writing the application code, let’s store the set of jokes in a MySQL table.
This table has only one column named joke. (We shall alter the table as and when required. For now lets keep it simple!)
To create this table, you can use phpmyadmin.
On orgfree.com, login and click on manage-db / create-db button to get Database details:
On orgfree.com, login and click on manage-db / create-db button to get Database details:
You get DB username, Password and host
Database Name= 44XXXX
DB username= 44XXXX
DB password = account password
DB host = localhost
(We will use these details later)
Login into phpmyadmin : http://jokeapp.orgfree.com/phpmyadmin
Using username = 44XXXX and account password
Create table named `joke` with a column named `joke` varchar of size 900.
Refer http://php.about.com/od/learnmysql/ss/create_tables.htm if you find phpmyadmin difficult to use.
Refer http://php.about.com/od/learnmysql/ss/create_tables.htm if you find phpmyadmin difficult to use.
Now search for some jokes add to the table.
STEP 6:
Now we have a working app and a set of jokes in Database. Lets’ send a joke instead of default HELLO message when user access our app.
To connect app to Database:
Edit joke.php so that it looks as file below and upload to hosting server.
Edit joke.php so that it looks as file below and upload to hosting server.
Function db_connect – connect to database. Edit $user to database usename, $password to your account password and $database to database name. (We got these details in Step 5)
We generate a random number between 1 to max and print joke corresponding to that number.
Now if a user accesses @myjoke, user will get a random joke every time.
STEP 7:
Now, since we are sending a random joke, the user might get same joke again and again, to prevent that we will give a link for next joke along with the joke.
Edit joke.php to as shown below:
Now, we send a link to the same file parameter 'jokenum' along indocating next joke number from the database. Also, while fetching joke we test is a jokenum was sent to this file , if so then it was accessed by link and we use jokenum instead of generating a random number.
Now access @myjoke, you get a link alongwith the joke
So, if a user sends @myjoke he gets a random joke. because @myjoke is mapped tohttp://jokeapp.orgfree.com/joke.php and on accessing @myjoke 'jokenum' parameter is not passed and a random number is generated.
If the user sends the link in response, the link would map to http://jokeapp.orgfree.com/joke.php?jokenum=3(or come number) .
Thus user will not get a repeated joke.
G is the link for next joke in the above message.
STEP 8:
Sooner or later we will exhaust all the jokes and it will start repeating. So how do we get new jokes? Well, We can ask our users to send jokes and we can share the joke with all other users!
Edit joke.php as follows:
Now along with link, we also print a form to receive joke from user.
Note that form have class = ‘txtweb-form’ this is very important!
Note that form have class = ‘txtweb-form’ this is very important!
Also, note that the input field for joke has name ‘userjoke’ and the form is posting data back to same app joke.php.
Hence, the logic is if ‘userjoke’ parameter is set – then save the joke to database (function insert_joke saves joke to database), else continue with old flow where we print a joke, link to next joke and a form to receive joke.
Also, we added a condition that joke submitted by user must be greater that 10 character to avoid spam. You may need to put more conditions to prevent spam and also you must check if a joke already exists in database.
Send H for above message to submit joke
On sending :
We get:
That’s it!! We can now get jokes from users. Since we are storing them in same table as the initial jokes, they will be sent along with all other jokes!!
Now we have a working app! We can go ahead and publish it on txtWeb.com!
While building this app, we learnt the usage of links, forms and how to handle the responses from user via links and forms.
In the next post we will get into our next requirements:
- Allows users to rate joke
- Allows access of jokes by categories like rating, latest etc.
- Never repeats the same joke.
- Performance optimization.
If you want to the code for this app you can download the joke.zip file here.
(Note: This article has been contributed by Manu C, Software Engineer at Intuit.)
Subscribe to:
Comments (Atom)