My Daily Coding updates
Tuesday, October 9, 2012
Thursday, June 28, 2012
Accordion menu - making a top level menu item work as a link
I was working on five-six level JQuery accordion menu to build a side navigation menu. However, i wanted to make labels with a single level menu to behave as normal links rather than the default behaviour of opening up a sub-menu on clicking the top level label.
For this, you need to capture the click event of the top level label to make it work as a normal link. Following is the solution:
The HTML code looks like:
<div id="accordion">
<h3 class="home">
<a href="/home" class="heading" >Home</a>
</h3>
<h3>
<a href="#custom" class="heading">Custom</a>
</h3>
<ul>
<li><a href="/home/mydata1">My Bio1</a></li>
<li><a href="/home/mydata2">My Bio2</a></li>
</ul>
<h3>
<a href="/home/Account" class="heading">Account</a>
</h3>
<ul>
<li><a href="/home/avatar">Avatar</a></li>
<li><a href="/home/Account">Account</a></li>
</ul>
<h3>
<a href="#custom2" class="heading">Custom2</a>
</h3>
<h3>
<a href="#custom3" class="heading">Custom3</a>
</h3>
<h3>
<a href="#custom4" class="heading">Custom4</a>
</h3>
<h3>
<a href="#custom5" class="heading">Custom5</a>
</h3>
</div>
The js code to set up Accordion with JQuery looks like:
$(document).ready(function(){
$('#accordion').accordion({
header: "h3.home",
navigation: true,
autoHeight: false,
clearStyle: true,
collapsible: true,
alwaysOpen: true,
animated: 'slide'
});
$('#accordion h3.home a').click(function(){
$('#accordion a.heading').accordion("activate", -1); window.location = $(this).attr('href');
return false;
});
});
The line: $('#accordion a.heading').accordion("activate", -1); closes every label/header that is open.
The line: window.location = $(this).attr('href'); sets the url to navigate to, to the href value.
Thursday, April 26, 2012
Sessions and Cookies:
Cookies Vs Sessions: The main difference between cookies and sessions is that cookies are stored on the client side while sessions are stored on the server side.
Cookies: A cookie is a file that is stored on the client computer when visiting a website. They are typically used to store user preferences e.g. Cookies store the order, in which users want their news to be printed etc. More often than not, they are used to store any non-sensitive data on client computer. When you create a cookie, you need to specify how long you want it to be valid, and once done, it will reside on the client computer till that date until it ‘expires.’ Once a cookie has been created, the visitor’s browser will automatically send it to the web server each time the visitor visits our site. PHP then reads this value into the superglobal array: $_COOKIE, which a developer can then use to read the concerned cookie’s value as $value = $_COOKIE[‘name’];
Usage:
Following is the syntax to create a cookie:
setcookie(name,value,expire,path,domain,secure,httponly);
where:
name => name of the cookie to be stored on the client’s computer.
value => value the cookie will hold.
expire => the expiry period of the cookie
path => the path on the server in which the cookie will be available on. Default value is the current directory that the cookie is being set in.
domain => the domain on which the cookie will be available.
secure => indicates that the cookie will be transmitted only over a secure http connection i.e. only over an https:// request.
httponly => This parameter was added in PHP 5.2 and above. When set to TRUE, the cookie will be accessible only via an http protocol. It cannot be accessed by Javascript anymore.
e.g. Let us set a cookie that will expire 2 days from now. It’s typical syntax will be:
$cookieValue = “test data”;
setcookie(‘testcookie’, $cookieValue,time()+172800,’/foo’,’example.com’,1);
The above declaration will create a cookie named “testcookie” with the given value and will expire after 2 days. 2 days = 24*60*60*2 = 172800 seconds. In addition, it will available within the directory /foo and all sub-directories such as /foo/test of the domain : example.com.
Please note: The secure parameter being set to 1, indicates that the cookie will be available only on a secured HTTP connection i.e. over an https:// request only.
Sessions: A session is nothing but a combination of a client-side cookie and server-side cookie. The client-side cookie contains a reference to the server side cookie which is stored on the web server. So, when a user visits a website, the client’s browser sends the reference code to the web server which in turn loads the corresponding data.
Advantages of such a combination of a client side and server side cookie representing a session are:
1. The server-side cookie can contain very large amounts of data with no hassle - client-side cookies are limited in size.
2. The client-side cookie contains nothing other than a small reference code - as this cookie is passed each time someone visits a page on your site, you are saving a lot of bandwidth by not transferring large client-side cookies around.
3. Session data is much more secure - only you are able to manipulate it, as opposed to client-side cookies which are editable by all.
Usage:
session_start();
$_SESSION[‘firstName’] = “Jim”;
print $_SESSION[‘firstName’];
unset($_SESSION[‘firstName’]);
session_destroy();
As shown above, when you call session_start(), PHP will check to see if the client computer’s browser has sent a session cookie, if it did, then PHP will load the session data. Otherwise, PHP will create a new session file on the server and send an ID back to the client computer to associate it with the new file. To remove a specific value from a session, use unset() function. It is important that you should unset only specific elements of the $_SESSION array, not the $_SESSION array itself, because doing so would leave you without any way to manipulate the session data at all.
Tuesday, December 27, 2011
Making a curl request behind a proxy
Saturday, August 28, 2010
PHP coding standards
While we create applications in any language, we tend to follow certain coding conventions. So does this apply to creating applications in PHP as well.
Here is a list of few coding standards that i follow:
1. Use camelCaps for Variable Names
2. Use lowercase for method / function names. For functions with multiple words, an underscore is used as a separator. For e.g. function get_display_text();
3. Use class constants instead of global constants wherever possible, by using the const modifier.
4. Use descriptive variable names in for loops as well.
For e.g. $count instead of $j
for($count=0;$count < 10;$count++){
your code here
}
and many more....
It would also be great if you could also pour in your thoughts on the coding conventions you follow while writing applications in php.

Here is a list of few coding standards that i follow:
1. Use camelCaps for Variable Names
2. Use lowercase for method / function names. For functions with multiple words, an underscore is used as a separator. For e.g. function get_display_text();
3. Use class constants instead of global constants wherever possible, by using the const modifier.
4. Use descriptive variable names in for loops as well.
For e.g. $count instead of $j
for($count=0;$count < 10;$count++){
your code here
}
and many more....
It would also be great if you could also pour in your thoughts on the coding conventions you follow while writing applications in php.
Friday, September 18, 2009
Executing PHP code from a .html file
You can execute PHP on a .html page by adding following line in your .htaccess file :
AddType application/x-httpd-php .html
for executing PHP on a .htm page add this line instead :
AddType application/x-httpd-php .htm
The above code will make php executable on all your .html or .htm pages
If you want to make PHP executable only on one page, you may add following line in your .htaccess file:
The above code would make PHP executable only on myhtmlpage.html and not on all html pages.
AddType application/x-httpd-php .html
for executing PHP on a .htm page add this line instead :
AddType application/x-httpd-php .htm
The above code will make php executable on all your .html or .htm pages
If you want to make PHP executable only on one page, you may add following line in your .htaccess file:
<Files myhtmlpage.html> AddType application/x-httpd-php .html
</Files>
The above code would make PHP executable only on myhtmlpage.html and not on all html pages.
Wednesday, April 22, 2009
Workaround for Maximum Script Execution time limit
We all know that the default execution time of a php script is set to 30 seconds, defined by the parameter max_execution_time in php.ini file.
A simple workaround to overcome the time limitation is a call to ini_set() as :
This causes the current script to be executed without any maximum time limit. I found this workaround helpful in case of sending newsletters where bulk mailing is required. Hope it is helpful to you.
A simple workaround to overcome the time limitation is a call to ini_set() as :
ini_set('max_execution_time','0') //0 = no limitThis causes the current script to be executed without any maximum time limit. I found this workaround helpful in case of sending newsletters where bulk mailing is required. Hope it is helpful to you.
Subscribe to:
Posts (Atom)