Archive for the 'web' Category

Creating Action Controller in PHP

We could handle actions in PHP like this…

<?php

$actions = $_GET['action'];

switch( $actions ) {

case ‘hello’:

say_hello();

break;

case ‘bonjour’:

say_bonjour();

break;

default:

echo ‘Unknown action ‘.$actions;

}

?>

But this way is hard to extend and hard to maintain. How about adding say_konichiwa() action? We need to add a function and need to add a case in switch statement. For database management system, we need at least 6 actions. Show List View, Add New, Show, Edit, Update and Delete. If we put all those actions to one page like about example, it will be ugly and hard to read. And, we definitely will need to add another action.

We could make Ruby on Rails style Action Controller in easy way. First we need to create .htaccess for RESTful url.

.htaccess

RewriteEngine on
RewriteRule ^action/([a-z0-9\-\_]*)$ index.php?do=$1 [NC]
RewriteRule ^action/([a-z0-9\-\_]*)/$ index.php?do=$1 [NC]

Then, we need to create three scripts. index.php, actions.php and action_controller.php.

index.php

<?php include(‘actions.php’); ?>
<html>

<head><title>Index</title></head>

<body><?php actions(); ?></body>

</html>

actions.php

<?php

include(‘action_controller.php’);

function say_hello(){

echo ‘Hello World!’;

}

function say_bonjour(){

echo ‘BONJOUR !!!’;

}

?>

action_controller.php (the trick)

<?php

function actions(){

$action = $_GET['do'];

if($action == ){

if(function_exists(‘index’)){

index();

} else {

echo ‘There is no index action !’;

}

} else {

if(function_exists($action)){

//calling function same name with $action

$action();

} else {

echo ‘Unknown Action ‘.$action;

}

}

}

?>

That all…

Now, we can access our say_hello actions with…

http://domain/project-dir/action/say_hello/

If we want to add say_konichiwa action, we can simply put say_konichiwa function to actions.php and we can call our action from browser with ( http://domain/project-dir/action/say_konichiwa ). That is easy to maintain, easy to extend and nice (i think :D).

Have a nice day…

Differences between SESSION and COOKIE

In my previous post, I wrote about breaking the rule in using requests. How many things we are not clearly understood in web standard? This time, I try to learn another standard. What is difference between SESSION and COOKIE? I thought I knew it, but actually not. I made some Googling. And here are the results…

HTTP is a stateless protocol

HTTP is stateless protocol. A stateless server is a serve that treats each request as an independent transaction that is unrelated to any previous request.

That mean, the request you make doesn’t associate in any way with the previous one. So, how about the request we want to make frequently, like user name or id? As you know, we could store our data in COOKIE. When we store data in COOKIE, the browser will send the cookie data to server for each request. We already could use SESSION for this kind of task. So, what is difference between SESSION and COOKIE?

COOKIE
A cookie is a text-only string that takes a place in the memory of user’s browser. If the lifetime of the cookie is set to be longer than the time user spends at that site, then this string is saved to file for future reference. User could be disabled the cookie in their browser setting.

SESSION
Session values are store in server side not in user’s machine. A session is available as long as the browser is opened. User couldn’t be disabled the session. We could store not only strings but also objects in session.

The Differences
We got three differences in general. The key difference would be cookies are stored in client side and sessions are stored in server side. The second difference would be cookies can only store strings. We can store our objects in sessions. Storing objects in sessions were really useful according to my experience. Another difference was that we could be save cookie for future reference, but session couldn’t. When users close their browser, they also lost the session.

The Problem with GET Requests

Many web developer using a link to trigger actions (like this <a href=”delete.php?id=123″>). I already knew that kind of GET request link is dangerous. But I didn’t clearly understand the usage of POST and GET. Yesterday, while I continue my reading in “Agile Development with Rails”, I found out an interesting chapter “The Problem with GET Requests”.

Almost since HTTP was invented, it was recognized that there is a fundamental difference between HTTP GET and HTTP POST requests. Tim Berners-Lee wrote about it back in 1996. Use GET requests to retrieve information from the server, and use POST requests to request a change of state on the server. The problem is that this rule has been widely ignored by web developers.

See, we shouldn’t use GET request to changing a state on server. How many time do we breaking that rules? We should only use GET request to retrieving data from server. We already using GET request for many occasions in changing request such as deleting, adding and updating database or creating files on server. According to the about rule, we shouldn’t use GET for those kind of requests. A GET request link can cause easily user error. We should used form and button rather than link. Could we possible to use form for every state changing request? I don’t think so. But, if we want to do a dangerous action like “Firing Missile”, we really shouldn’t use the GET request link. We could use confirmation page with form. We will have advantages by using form. When user refresh the browser, the browser will warn that there was a POST request. When we can’t use a form, should create a GET request link with effective confirmation and we should certain that the reference page can’t refresh by user.