31 December 2010

How to increase php script execution time

Sometimes we need to execute large php scripts but it gives following error:
PHP Fatal error:  Maximum execution time of 30 seconds exceeded in /usr/local/apache/vhosts/abcd.com/htdocs/sites/all/modules/example.php on line 230

In such situations we can avoid this error by using one of the following methods:

Method 1

Place this code at the start of your php script.
ini_set('max_execution_time', 180); 

In the above code “180” is number of seconds.

180 seconds = 3 Minutes

Therefore the above code will increase the page execution time to 3 minutes.

Method 2

We can also use the following piece of code. But it won’t work in the safe mode.
set_time_limit(180);

180 is again the number of seconds. So the above code will also increase the page execution time to 3 minutes. If this value is set to zero then there will be no limit on the execution time.

How to block an IP using htaccess

Sometimes you need to block an IP which is doing DOS (Denial Of Service) or DDOS (Distributed Denial Of Service) attack on your site. For this purpose I am sharing a simple htaccess code. You just need to place this code in your htaccess file and the attacking IP will be blocked from your site.
##########BLOCKING AN IP################
< Limit GET HEAD POST>
order allow,deny
deny from XXX.XXX.XXX.XXX
allow from all
< /LIMIT>
########################################

For any queries and problems feel free to comment and don’t forget to replace “XXX.XXX.XXX.XXX” with the IP address that you want to block in the above code.

How to create rss feed for static website

What is RSS?

RSS is most commonly abbreviated as Really Simple Syndication. It is an xml document used to provide the summary of the site contents. RSS is used to send users notification about what’s new on the site since they have last visited it.

Syntax

For creating a RSS feed for your site you must be familiar with xml tags. Basically rss feed comprises of two parts first part is about your site and second part is about your content.

You have to place the following code on the beginning of your xml file. This will initialize the xml document as rss feed.

< ?xml version="1.0" ? >
< rss version="2.0">
< channel >

Then comes the first part of your rss feed which is about your site information. In this part you need to define information about your site. Place the following code immediately after the previous code in your xml file:

< title>Skills 2 Earn< /title>
< description>Knowledge for all< /description>
< link>http://skills2earn.com< /link>

In the above code “< title>” tag is used for setting the title of your site, “< description>” tag for description and “< link>” tag for the link of your site.

Now comes the second the most important part of your rss feed, in this part you need to define the information about your content. For this purpose “< item >” tags are used. If you want to put four topics in your feed then you need to use four “< item>” tags. Following is an example code to let you know how these item tags can be used:

< item>
< title>title of your topic< /title>
< description>description of your topic< /description>
< link>http://www.example.com< /link>
< /item>

In the above code “< title>” tag is used for setting the title of your topic, “< description>” tag for description and “< link>” tag for the link of your topic.

In the end you need to close all open tags. As I am doing in the code below:

< /channel>
< /rss>

Now in the last, rss files are like other xml files and they will be treated as text, so if you run your xml file on your local server or web server then the contents of the file will be displayed on the screen. So you need to tell apache to treat these xml files as php files. This can be done by adding following code in the .htaccess file:

AddType application/x-httpd-php .xml

That’s it guys, you have done it, you have created an rss feed for your website.

Complete Code

Complete code is mentioned below. You need to safe it in an xml file and you can name it as “rss.xml”.


< ?xml version="1.0" ? >
< rss version="2.0">
< channel>
< title>Skills 2 Earn< /title>
< description>Knowledge for all< /description>
< link>http://skills2earn.com< /link>
< item>
< title>title of your topic< /title>
< description>description of your topic< /description>
< link>http://www.example.com< /link>
< /item>
< item>
< title>title of your topic< /title>
< description>description of your topic< /description>
< link>http://www.example.com< /link>
< /item>
< /channel>
< /rss>


For any problems or queries, you may have, feel free to comment on this post. I would love to resolve your problems.

29 December 2010

How to write a robots.txt file

What is robots.txt?

Robots.txt file is used by the webmasters to give instructions to robots on how to crawl their site. This includes instructions like which directory to crawl which to not, which robot is allowed and which is not, and the location of sitemap etc.

Basic Configuration of robots.txt

To allow all bots to crawl your website the robots.txt file will look like this:

User-agent: *
Allow: /
To disallow all the robots from crawling your site you can use the following code:
User-agent: *
Disallow: /
To disallow all bots except google, you can use following configuration:
User-agent: *
Disallow: /
User-agent: Googlebot
Allow: /
To disallow bots from crawling some particular directories and pages you can use following code:
User-agent: *
Disallow: /admin/
Disallow: /css/
Disallow: /example.htm
You can also add the link of your sitemap in the robots.txt file for auto discovery using following syntax:
Sitemap: http://skills2earn.com/sitemap.xml
Reference

www.sitemaps.org

Php For Beginners (Lesson 1)

Definition
PHP: Hypertext Preprocessor is a widely used, general-purpose server side scripting language that was originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document.

Installation
In order to run php on your own machine you need to install following three things:
  • Apache Server
  • Php
  • MySQL
But to make this lesson easier I will not discuss explicit installation of all the three things, rather I would tell you guys a simpler way of doing this. For this purpose we can simply use WAMP/XAMPP Server which is an all in one package of apache, php & MySQL.

WAMP Server can be downloaded from the link below:
http://sourceforge.net/projects/wampserver/files/WampServer%202/WampServer%202.0/WampServer2.0i.exe/download

Or you can download XAMPP Server for windows from the below mentioned link:
http://sourceforge.net/projects/xampp/files/XAMPP%20Windows/xampp-win32-1.7.3.exe/download

After downloading simply follow the steps to install WAMP/XAMPP server on your PC. Once the installation is complete you can test it by typing http://localhost/ in the explorer. This should open a default page for the WAMP/XAMPP Server. If the default page does not come up then try restarting the WAMP/XAMPP Server.

How to Start
Now that php is successfully installed on your PC, we need to know where you should type the php code. Well you can use Notepad, Dreamweaver or any other editor to write in the php code. I would recommend that you use Dreamweaver because it’s a lot easier to use.

After writing the code, you need to safe the file with a .php extension. For example you can save your file as test.php in the respective server folder.

In case of WAMP Server you need to save your php files in wamp > www directory. The exact path of this directory is:
C:\wamp\www\test.php

Whereas if you are using XAMPP Server then you need to safe your php files in the XAMPP > htdocs directory. The exact path should be:
C:\xampp\htdocs\test.php

Now you can run your php file by typing http://localhost/test.php in your browser.

Syntax
A Php code will always be encapsulated between "<?php " and "?>" each instruction will be terminated by semicolon (;).

Example
<?php
some php code....;
some php code....;
some php code....;
?>
Hello World Example
In php if you want to display something on the browser’s screen, you need to use “echo” function.

Syntax
echo “anything you like to display on the screen”;

Example
<?php 
echo "Hello World!";
?> 
Ok guys that is all for the first lesson now you should practice and play with this code to get used to with the php syntax.

References
http://www.wikipedia.com
http://www.w3schools.com 


Next Lesson next