Tuesday, May 17, 2011

Cookies in PHP

There are two types of Cookies present in PHP. One is Persistant Cookie and the other one is Temporary Cookie.
By default, cookies are created as temporary cookies which stored only in the browser's memory. A persistent cookie is a cookie which is stored in a cookie file permanently on the browser's computer. When the browser is closed, temporary cookies will be erased. You should decide when to use temporary cookies and when to use persistent cookies based on their differences:
  • Temporary cookies can not be used for tracking long-term information.
  • Persistent cookies can be used for tracking long-term information.
  • Temporary cookies are safer because no programs other than the browser can access them.
  • Persistent cookies are less secure because users can open cookie files see the cookie values

What is MVC Architecture

Model-view-controller (MVC) is a design pattern used in software engineering. In complex computer applications that present lots of data to the user, one often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface do not impact the data handling, and that the data can be reorganized without changing the user interface. The model-view-controller design pattern solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller.

Monday, May 2, 2011

File Uploading with out using Ajax

We can't upload file using normal Ajax. For that we should use advanced javascript frameworks or JSON for passing file content. Also this javascript will not work if it is disabled from the browser.
But we can get this Ajax effect without using ajax or javascript by simply depending on target attribute of the form tag.
Below is the sample script to achive this.



File name: form.html
<iframe src="http://www.genengnews.com/app_themes/genconnect/images/default_profile.jpg" id="targetFrame" name="targetFrame" style="border: 0; height:230px; width: 230px;"></iframe>
<form action="upload.php" target="targetFrame" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="upload" />
</form>


File name: upload.php
<?php
if(isset($_FILES["file"])) {
 // You can add file uploading script here
 echo "Temporary file location is: " . $_FILES["file"]["tmp_name"] . "<br />";
 if(substr($_FILES["file"]["type"], 0, 5) == "image") {
  echo '<img src="' . $_FILES["file"]["tmp_name"] . '" alt="" />';
 }
?>
<?php
}
?>