We use PHP in the majority of our web development projects. It is open source, allows the quick development of complex websites and is very flexible. One of the simplest improvements PHP offers over sticking to basic HTML is the use of include files. These are files containing code which is repeated throughout the site. It may be the header, footer, navigation or a particular 'widget' which appears in several parts of the website. If the site is built in HTML then the code for this section would need to be repeated every time it appears, leading to major headaches if anything needs to be changed - if an extra link needs adding to the navigation for example. Building in PHP and using include files means that the code only needs to be written once, it can then be 'included' in every page it is required with a single line like:
<? include_once "include/navigation.php" ?>
Thus saving time, minimising code replication and making future updates much easier to handle.
Monday, 21 June 2010
Friday, 11 June 2010
Subversion add multiple files
One of the reasons we started this blog was to record any tricks we find that help us with our web development work. We recently took on a project which involves working with subversion and found the following very helpful for adding multiple files to the repository. Simply get into your terminal as usual and enter the following command:
svn add `svn status | grep "^?" | awk '{print $2 }'`
This basically runs the status command to find all files with a '?' status (meaning they have not been added to the repository) and inserts that filename after the add command, so its essentially the same as typing
svn add file1 file2 ...
svn add `svn status | grep "^?" | awk '{print $2 }'`
This basically runs the status command to find all files with a '?' status (meaning they have not been added to the repository) and inserts that filename after the add command, so its essentially the same as typing
svn add file1 file2 ...
Sunday, 6 June 2010
PHP and Javascript cookies
If you want to store data on your website visitors computers then you'll need to use cookies. They are a very useful way of improving the user friendliness of your sites by, for example, keeping your users logged in over multiple sessions on the same PC, or recording which language, text size or colour options they prefer to view your site in.
Cookies can be accessed in several ways by all sorts of different coding languages, but as we do alot of work with PHP and javascript we will show how we access and manipulate cookies with those languages.
PHP:
There is an inbuilt function in PHP called setcookie() which can be used for all your cookie related fun in PHP. Its full details can be found here: http://php.net/manual/en/function.setcookie.php but we have given you the basics below:
setcookie("cookie name", "cookie value", "expiry time (secs)", "path", "domain");
In essence all you need to do to store a cookie of 'language'='eng' for 1 month which is valid across your entire domain is to put the following code somewhere on your php page:
setcookie("language", "eng", time()+(3600*24*30), "/");
It should go above any html output as there can be header issues, but apart from that it really is that simple! To retrieve your cookie value in PHP use the $_COOKIE array.
Javascript:
We have created some javascript functions to simplify the process of cookie manipulation in javascript. These are provided below for your use.
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i <>
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
Cookies can be accessed in several ways by all sorts of different coding languages, but as we do alot of work with PHP and javascript we will show how we access and manipulate cookies with those languages.
PHP:
There is an inbuilt function in PHP called setcookie() which can be used for all your cookie related fun in PHP. Its full details can be found here: http://php.net/manual/en/function.setcookie.php but we have given you the basics below:
setcookie("cookie name", "cookie value", "expiry time (secs)", "path", "domain");
In essence all you need to do to store a cookie of 'language'='eng' for 1 month which is valid across your entire domain is to put the following code somewhere on your php page:
setcookie("language", "eng", time()+(3600*24*30), "/");
It should go above any html output as there can be header issues, but apart from that it really is that simple! To retrieve your cookie value in PHP use the $_COOKIE array.
Javascript:
We have created some javascript functions to simplify the process of cookie manipulation in javascript. These are provided below for your use.
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i <>
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
Subscribe to:
Posts (Atom)