cookies – apache mod expires

 

Setting up Cookies

A cookie is a small text file which is generally used to identify a visitor as first time/ returned visitor or member/non-member visitor. When a user request a page from your web server the server may place/read this text file on a user’s hard disk. You as an SEO must know how cookies are created and set up.

The setcookie() function is used to create a cookie. This function must appear BEFORE the <html> tag.

Syntax: setcookie(name, value, expire, path, domain,secure);

Here,
Name’ is the name of the cookie.
Value’ is the value associated with the cookie.
Expire’ is the time when the cookie should expire. For e.g. time()+3600*24*30 will set the cookie to expire in 30 days. If this parameter is not set then the cookie will expire at the end of the session (when the browser closes).

Path’ is where the cookie should be available. For e.g. within a particular directory (like /test/) or entire domain (like /)
Domain’ is the domain name of the cookie like www.example.com
‘Secure’ is whether or not the cookie should be set over a secure HTTP connection. Its default value is ‘False’ which means cookie should be set up. The value of ‘True’ means cookie should be set up only if a secure connection exist.

Note: Only ‘name’ and ‘value’ fields are required in the setcookie() function. All other fields are optional.

Example 1

In the example below, we will create a cookie named “seomoz” and assign the value “Rand Fishkin” to it. We will also specify that the cookie should expire after one hour:

<?php
setcookie(“seomoz”, “Rand Fishkin”, time()+3600);
?>

<html>
…..
……
……

Example 2

In the example below, we will create a cookie named “seomoz” and assign the value “Rand Fishkin” to it. We will also specify that the cookie should expire after one month:

<?php
setcookie(“seomoz”, “Rand Fishkin”, time()+3600*24*30);
?>

<html>
…..
……
……

Note: In the example above the expiration time is set like this: 1 hr(3600 seconds) * 1 day(24hrs)*1 month(30 days). So what I have specified is the total number of seconds in a month.

Viewing Cookie Information

Once you have setup cookie, you can verify by it through ‘web developers’ toolbar. Install web developers toolbar (https://addons.mozilla.org/en-US/firefox/addon/web-developer/). Click on ‘view cookie information’ under the ‘cookies’ menu. You may see a long list of cookies. One of the cookie will be the one I just set up:

I set up this cookie on my own website eventeducation.com. That’s why the host name is www.eventeducation.com. Note this cookie will expire after one month. We can also set this cookie to expire after 6 months or 6 years.

 

Viewing HTTP response header

A browser and a web server communicate with each other through HTTP request and response headers. HTTP request headers are sent by a browser whenever it request a resource (like image file) from a web server. HTTP response headers are send by a web server in response to the request made by the web browser. You as an SEO must know what are HTTP headers, how they are setup and how they can be viewed. To view HTTP response header click on ‘view response headers’ under the ‘information’ menu on the web developers toolbar. Here is what i got:

PHP header() Function

This function is used to send a raw HTTP response header to a browser and must be called before an actual output is sent (in PHP 4 and above).

Syntax: header(string,replace,http_response_code);

Here,

String‘ is the header string to send. It is made up of colon separated name-value pairs. For. e.g. “Cache-Control: no-cache”. This field is required.

replace‘ specifies whether the header should replace previous header or add a second header. Default is TRUE (will replace). FALSE (allows multiple headers of the same type). This field is optional.

http_response_code‘ forces the HTTP response code to the specified value (only available in PHP 4.3 and higher).

Example 1:

Send status code 404

Place the following code on the page which should return a 404 response code

<?php
// Send 404 File not found response code
header(“HTTP/1.0 400”);
?>

<html>
<body>

=========================

Example 2: Send status code 301

Place the following code on the page which should return a 301 response code

<?php
// Send 301 Moved permanently response code
header(“HTTP/1.0 301”);
?>

<html>
<body>

Congratulations! you now know how to send HTTP status codes. Now you must be able to handle soft 404 issues. Click here to see the complete list of HTTP status codes.

Example 3: Prevent page caching:

Place the following code on the page which you don’t want to be cached.

<?php
// Date in the past
header(“Expires: Mon, 20 Jul 2001 12:00:00 GMT”);
header(“Cache-Control: no-cache”);
header(“Pragma: no-cache”);
?>

<html>
<body>


Example-4: Setting up rel=”canonical” HTTP Header

Place the following code on the page which is near duplicate to the page white-paper.php:

<?php
//Link is used to express a typed relationship with another resource
header(“Link: <https://www.eventeducation.com/white-paper.php>; rel=\”canonical\””);

?>

<html>
<body>


Example-5: Other uses of the header() function

<?php
header(“Cache-Control: max-age=3600”);
header(“Last-Modified: Tue, 15 Nov 2010 12:45:26 GMT”);
header(“If-Modified-Since: Sat, 29 Oct 2010 19:43:31 GMT”);
header(“Content-Type: text/html; charset=utf-8”);

?>

<html>
<body>


 

Apache Module mod_expires

You need to have basic knowledge of this module if you want to want to make static resources (like CSS, image files etc) cacheable. Through this module you can add Expires and Cache-Control HTTP headers to static resources and thus make them cacheable.

ExpiresActive Directive
This directive enables or disable generation of Expires headers and cache-control headers for a resource like images, CSS files etc.

Syntax: ExpiresActive On|Off

Example: ExpiresActive On

========================

ExpiresByType Directive
This directive defines the value of the Expires header and the max-age directive of the Cache-Control header generated for documents of the specified type (e.g., text/html).

Syntax: ExpiresByType MIME-type <code>seconds

Example: ExpiresByType image/gif A31104000

The second argument sets the number of seconds that will be added to a base time to construct the expiration date.

The base time is either the last modification time of the file, or the time of the client’s access to the document. Which should be used is specified by the <code> field; M means that the file’s last modification time should be used as the base time, and A means the client’s access time should be used. In the example above all GIF image files will expire after 31104000 seconds (i.e. after year) from the time of the client’s access to the document.

The Cache-Control: max-age is calculated by subtracting the request time from the expiration date and expressing the result in seconds.

ExpiresDefault Directive

This directive sets the default algorithm for calculating the expiration time for all documents in the affected realm. It can be overridden on a type-by-type basis by the ExpiresByType directive.

Syntax: ExpiresDefault <code>seconds

Example: ExpiresDefault A0

The directive in the example above set the default algorithm for calculating the expiration time for all documents from the time of the client’s access to the document to zero.

Source: https://httpd.apache.org/docs/2.0/mod/mod_expires.html

 

Most Popular E-Books from OptimizeSmart

Learn to read e-commerce reports book banner

How to learn and master Web Analytics and Google Analytics?

Take the Course

Check out my best selling books on Web Analytics and Conversion Optimization on Amazon

How to get lot more useful information?

I share lot more useful information on Web Analytics and Google Analytics on LinkedIn then I can via any other medium. So there is really an incentive for you, to follow me there.

Himanshu Sharma

Certified web analyst and founder of OptimizeSmart.com

My name is Himanshu Sharma and I help businesses find and fix their Google Analytics and conversion issues. If you have any questions or comments please contact me.

  • Over twelve years' experience in SEO, PPC and web analytics
  • Google Analytics certified
  • Google AdWords certified
  • Nominated for Digital Analytics Association Award for Excellence
  • Bachelors degree in Internet Science
  • Founder of OptimizeSmart.com and EventEducation.com

I am also the author of four books:

error: Alert: Content is protected !!