Category Archives: General

Session vs Cookie or cookies and session in php

Complete guide of cookies and session in php




Many New PHP developers are often confused whether to use sessions or cookies for their websites. Both cookies and sessions have their advantages and drawbacks. PHP developer should first understand the differences between each so that he can choose better option according to need.


Sessions are PHP’s built in method for handing cookies. According to, sessions are “a way to preserve certain data across subsequent accesses.”


Whenever PHP creates a new session, it generates a sessionID (session_id())this session id is then either stored on the user’s computer as a cookie or in some cases, attaches itself to the end of each page’s URL as a query string.The actual information stored is not stored on the user’s computer or client machine.PHP stores the information in the session on the server in some kind of database or a text file( you can see this in phpinfo “session.save_handler“).In the background processes on the server, PHP runs a garbage collecting process that destroys all sessions that have been inactive for twenty-four minutes (in phpinfo “session.gc_maxlifetime“)thus, sessions are a way of storing client information on a server.


Every time on client request (values do not necessarily have to be changed) a session, the garbage collector resets its twenty-four minute countdown for deletion.Thus, a user cannot leave a site and come back in an hour or two (time set in php.ini file) and expect the session to still be alive.In addition, a user’s computer deletes all sessionIDs every time the user closes the browser.Thus, the only real advantage of using sessions is that they allow a PHP developer to hide what information is being stored from the users and hackers.However, hackers can hijack sessions with a cookie grabber, so one cannot argue that sessions are much more secure than cookies.

The only security advantage of sessions is that they hide information; thus, if a website stored a user’s (encrypted) password in a cookie and a hacker somehow obtained the cookie, the hacker could run a password cracker(bad software) on the encrypted password to get it, whereas a session hijacker would have only have access to the account, not the encrypted password.PHP developers should use sessions only for things that require the short-term preservation of data.Overall, sessions serve as a short-term method for preserving data across pages while hiding information from users and hackers.


COOKIE defines cookies as “a mechanism for storing data in the remote browser and thus tracking or identifying return users.”

Cookies maintain a set interval of time even if the user closes the browser (unless of course he clears his cookies or it expire). The only disadvantage to using a cookie is that the information is stored locally on the user’s computer in a text file. Therefore, hackers who use cookie stealer can access the information as well as anyone with physical or remote access to the computer’s files, this can be a security threat. However, a well-coded website prevents cookie grabbers from working, and thus eliminates most of the security threat. However, it is important to keep in mind that users can easily change the value of a cookie, so treat anything inside of a cookie as malicious user input. Therefore, PHP developers should use cookies as a long-term solution to preserve data across pages and sessions.


Overall, sessions serve as temporary information holder that can hide information, whereas cookies serve as both a temporary and long-term information holder. After the difference between sessions and cookies is clear, making the right choice for a website is rather simple. Though sessions may seem easier to use than cookies, never doubt the power and ease of using cookies.


Hope this will help 🙂


Do you want to know a quick answer of differences between session and cookie?


Configure dreamweaver for .thtml and .ctp files

Configure dreamweaver for .thtml and .ctp files

I prefer to use Dreamweaver to create code all of my websites, mainly because of the code highlighting and the way I organize my sites in separate folders. I realize that there are lots of text editors out there but Dreamweaver is what I’m comfortable with and it does the great job.


Other text editors are Netbeans, Ecplipse, Notepad++  etc  but these have limited features. Dreamweaver have lot of features almost all a developer need.Yeah, It is paid but by it you speedup of your coding affords and managing projects skills,I think lots of programmer like Dreamweaver.


In CakePHP I found out that Dreamweaver doesn’t open .THTML and .CTP extensions files automatically, But it is so easy to configure Dreamweaver for these type of file.
Here it is…


THTML and CTP Files
CakePHP 1.1 uses .THTML files and CakePHP 1.2 uses .CTP files. We will set both of these extensions so that we don’t have to fiddle around with Dreamweaver again.


Modifying the Necessary Dreamweaver files
There are 3 files that you need to edit. The first two are located in the folder that you have installed Dreamweaver, in my case It is in ‘C:\Program Files\Adobe\Adobe Dreamweaver CS3\configuration‘.

C:\Program Files\Macromedia\Dreamweaver 8\Configuration in case,if you are using Macromedia Dreamweaver 8


Open up ‘Extensions.txt’ and on the first line at the very end add THTML and CTP separated by commas, so the line should read
Similarly add these two extensions to the ‘:PHP Files’ line.


Next open the ‘DocumentTypes’ folder and edit the ‘MMDocumentTypes.xml’ file, just open it up using notepad/wordpad. Search for the line which has an id ‘PHP_MySQL’ and add the THTML/CTP file extensions to both the ‘winfileextension’ and ‘macfileextension’ so the line should read.




The final file is another version of the ‘Extensions.txt’ which is located in your ‘Documents and Settings’ Folder in my case this is ‘C:\Documents and Settings\Mahesh\Application Data\Adobe\Adobe Dreamweaver CS3\Configuration’ just add the very same things you inserted earlier.


If you are using Macromedia Dreamweaver 8, you need not to do this edit.



for windows 7 you need to edit one more configuration file:
C:\Users\\AppData\Roaming\Adobe\Dreamweaver CS3\en_US\Configuration\extensions.txt
edit just like the other extension files.


Hope this will make sense!!

What is the HTML DOM(Document Object Model)?

The HTML DOM views an HTML document as a tree-structure. The tree structure that is defined known as a node-tree.All nodes can be accessed through the tree. Their contents can be modified or deleted, and new elements can be created.


Programming Interface
In the DOM, HTML documents consist of a set of node objects. The nodes can be accessed with JavaScript (Client side programming language).
The programming interface of the DOM is defined by standard properties and methods.


HTML DOM Properties are
x known as HTML element or object of DOM
x.innerHTML – the text value of x
x.nodeName – the name of x
x.nodeValue – the value of x
x.parentNode – the parent node of x
x.childNodes – the child nodes of x
x.attributes – the attributes nodes of x


HTML DOM Methods
Some simple DOM methods are
x.getElementById(id) – get the element with a specified id
x.getElementsByTagName(name) – get all elements with a specified tag name
x.appendChild(node) – insert a child node to x
x.removeChild(node) – remove a child node from x


The innerHTML Property
The easiest way to get or modify the content of an element is by using the innerHTML property.innerHTML is not a part of the W3C DOM specification. However, it is supported by all major browser.


Every element on a web page has certain events which can trigger JavaScript functions. Events are normally used in combination with functions, and the function will not be executed before the event occurs!
Events examples
> A mouse click
> A web page or an image loading
> Mousing over a hot spot on the web page
> Selecting an input box in an HTML form
> Submitting an HTML form
> A keystroke



Document Object Model (DOM)

The Document Object Model (DOM) is a platform- and language-independent interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents.


We can say It is convention for representing and interacting with objects in HTML, XHTML and XML documents.Aspects of the DOM (such as its “Elements”) may be addressed and manipulated within the syntax of the programming language in use(JavaScript or others). The public interface of a DOM is specified in its application programming interface (API) called as client side programming.


The HTML DOM defines a standard way for accessing and manipulating HTML documents.


The DOM is a W3C (World Wide Web Consortium) standard.The DOM defines the objects and properties of all document elements, and the methods (interface) to access them.


In simple words : The HTML DOM is a standard for how to get, change, add, or delete HTML elements.


According to the DOM, everything in an HTML document (layout) is a node.


DOM says that:

  • The entire document is a document node
  • Every HTML element is an element node
  • The text in the HTML elements are text nodes
  • Every HTML attribute is an attribute node
  • Comments are comment nodes

Facebook Twitter and Google plus share and Like Button

Now a days in very busy life and in internet edge, social website play a very important role in friendship and social work and sharing views and idea, wishing Birthday and Anniversary.


Social websites now provide various API that can be implemented in websites(personal or commercial), It will provide live-streaming, sharing links etc.


Facebook twitter and Google plus along with LinkedIn provide us like/share button API, by implementing it, it will show a button on your webpage, the Like button lets a user share the page content with friends on social websites. When the user clicks the Like button on site, a related story appears in the user’s friends’ News Feed with a link back to website (from where a user clicked button).


This means when a user clicks a Like button on your page, a connection is made between webpage page and the user.


Facebook Like

There are two ways to Facebook Like button implementations.One is XFBML and second one is Iframe.

The XFBML dynamically re-sizes button’s height according to whether there are profile pictures to display,gives the ability to know in real time when a user clicks the Like button, and it gives the user the ability to add an optional comment to the like.


<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); = id;
js.src = "//";
fjs.parentNode.insertBefore(js, fjs);
}(document, ‘script’, ‘facebook-jssdk’));</script>
<div class="fb-like" data-send="true" data-width="450" data-show-faces="true"></div>


<iframe src="//<?=urlencode($yourpageurl)?>&amp;send=false&amp;layout=button_count&amp;width=450&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:21px;" allowTransparency="true"></iframe>


Twitter Share

<a href="" data-url="<?=$yourpageurl?>" data-count="horizontal">Tweet</a>
<script type="text/javascript" src="//"></script>


Google plus Share

<script type="text/javascript" src=""></script>
<div class="g-plusone" data-size="medium"></div>

These are the basic code, There are so many options available on these button, a compete implementation guide available here.