rel="nofollow" attribute in hyperlinks to prevent comment spam and others

0 comments
I faced this problem while working on a site that, a client wanted a page of his site not to be crawled and added in google's search result.
Google or other search engines, while crawling a submitted site's / existing site's page for new changes or others, they also follow links in that page and in this way the crawler notices a new page in your site.

But suppose, there is a page in your website you don't want to be crawled. Now, you may be wondering why may someone want that ? Here is an example scenario.


Problem scenario 1 :

In my specific case, the requirement was, the client had a product review module and in every product details page, there were links to "add review" like,
<a href="http://www.client_website.com/store/add_review.php?product_id=23" >Add review </a>
 All the pages with different product id had the almost 99.99% the same contents in it.
And the problem was, it is causing to have like 200 pages in google with different links but the same duplicated contents. This duplicate content issue may have negative impact on the ranking of the site as it may seem that the webmaster is trying to duplicate contents and have more pages listed in search engine.


Problem scenario 2 : 

I got this from here. Some spammers try to make their site more visible to search engine by spamming their site's links to the comment section of different related Blogs and Forums. You may have seen this kind of comments. This kind of SEO techniques are known as black hat techniques and if somehow the search engine understand about that a black hat technique has been used to gain a better search engine position, they even demote a site's ranking.
You may want to prevent these spamming and for doing this, all you have to do is to ensure that the spammers doesn't get benefited by their spams in your site and eventually after trying for some time, they will stop trying.

Solution : 

When search engines see the attribute (rel="nofollow") on hyperlinks, those links won't get any credit when we rank websites in the search results.This isn't a negative vote for the site where the comment was posted; it's just a way to make sure that spammers get no benefit from abusing public areas like blog comments, trackbacks, and referrer lists.

About rel="nofollow"


"Nofollow" provides a way for webmasters to tell search engines "Don't follow this specific link"
or "Don't follow links on this page".

Originally, nofollow attribute appeared in the page-level meta tag, and instructed search engines not to follow / crawl any outgoing links on the page. For example:

        <meta name="robots" content="nofollow" />

When nofollow was used on individual links, preventing robots from following individual links on a page required a great deal of effort (for example, redirecting the link to a URL blocked in robots.txt).
That's why the nofollow attribute value of the rel attribute was created. This gives webmasters more granular control: instead of telling search engines and bots not to follow any links on the page, it lets you easily instruct robots not to crawl a specific link. For example:

        <a href="signin.php" rel="nofollow">sign in</a>


Although, this solution was quite simple It is a very important tag to use by the webmaters. That's why,  I thought to share about this in my blog.

For more details and the source I used, 


    reading an xml file in php and convert it into an associative array

    1 comments
    Today, I will write about How to read an xml file from a source url/address and convert it into an array finally so that we can parse and use it. The code is so simple if you are using php 5.0 or higher. If you don't have php 5.0 or higher, this solution below is not for you. It has another solution which involves a few more lines of code.

    Let's start with an url of a xml feed. Here is the link of BBC News Front page World Edition.

                http://newsrss.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml

    This page shows the latest world news in an xml feed. Now, if you want to read that front page using a php script and use those news in your website, what you should do ?



    I know, there may be lots of brilliant widgets to show the latest world news to a website, But i am just taking this as an example of how to parse that xml feed and I am considering you one of the future coder of those widgets.

    Here is the code.
    $SimpleXML_object = simplexml_load_file("http://newsrss.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml");   
    print_r($SimpleXML_object);
    echo $SimpleXML_object->channel->title ;


    The above code will output something like this. This was the output at the moment I was writing this.

    SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [version] => 2.0
            )
    
        [channel] => SimpleXMLElement Object
            (
                [title] => BBC News | News Front Page | World Edition
                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/default.stm
                [description] => Get the latest BBC World news: international news, features and analysis from Africa, Americas, South Asia, Asia-Pacific, Europe and the Middle East.
                [language] => en-gb
                [lastBuildDate] => Wed, 10 Mar 2010 01:18:42 GMT
                [copyright] => Copyright: (C) British Broadcasting Corporation, see http://news.bbc.co.uk/2/hi/help/rss/4498287.stm for terms and conditions of reuse
                [docs] => http://www.bbc.co.uk/syndication/
                [ttl] => 15
                [image] => SimpleXMLElement Object
                    (
                        [title] => BBC News
                        [url] => http://news.bbc.co.uk/nol/shared/img/bbc_news_120x60.gif
                        [link] => http://news.bbc.co.uk/go/rss/-/2/hi/default.stm
                    )
    
                [item] => Array
                    (
                        [0] => SimpleXMLElement Object
                            (
                                [title] => US attacks East Jerusalem plans
                                [description] => US Vice-President Joe Biden condemns Israel's approval of 1,600 new homes for ultra-Orthodox Jews in East Jerusalem.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/middle_east/8558850.stm
                                [guid] => http://news.bbc.co.uk/2/hi/middle_east/8558850.stm
                                [pubDate] => Tue, 09 Mar 2010 22:03:10 GMT
                                [category] => Middle East
                            )
    
                        [1] => SimpleXMLElement Object
                            (
                                [title] => Dutch bishops order abuse inquiry
                                [description] => Dutch bishops order an inquiry into alleged sexual abuses of children by Catholic priests, threatening a new scandal.
    
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/europe/8558311.stm
                                [guid] => http://news.bbc.co.uk/2/hi/europe/8558311.stm
                                [pubDate] => Tue, 09 Mar 2010 18:05:57 GMT
                                [category] => Europe
                            )
    
                        [2] => SimpleXMLElement Object
                            (
                                [title] => US apology for Gaddafi comments
                                [description] => The US apologises for comments made after Libya's Col Gaddafi called for a holy war against Switzerland.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/africa/8558764.stm
                                [guid] => http://news.bbc.co.uk/2/hi/africa/8558764.stm
                                [pubDate] => Tue, 09 Mar 2010 20:24:35 GMT
                                [category] => Africa
                            )
    
                        [3] => SimpleXMLElement Object
                            (
                                [title] => N Ireland backs policing switch
                                [description] => Northern Ireland's assembly backs a key measure to devolve the highly sensitive issues of policing and justice from London.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/uk_news/northern_ireland/8558466.stm
                                [guid] => http://news.bbc.co.uk/2/hi/uk_news/northern_ireland/8558466.stm
                                [pubDate] => Tue, 09 Mar 2010 19:50:22 GMT
                                [category] => Northern Ireland
                            )
    
                        [4] => SimpleXMLElement Object
                            (
                                [title] => Ban honours UN's Haiti 'heroes'
                                [description] => Ban Ki-moon pays tribute to the 101 UN staff who died in the Haiti quake, as President Preval seeks US support on boosting the economy.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/americas/8558880.stm
                                [guid] => http://news.bbc.co.uk/2/hi/americas/8558880.stm
                                [pubDate] => Wed, 10 Mar 2010 00:12:44 GMT
                                [category] => Americas
                            )
    
                        [5] => SimpleXMLElement Object
                            (
                                [title] => Arrests in Cyprus body theft case
                                [description] => Three men are arrested in connection with the theft of the corpse of Cyprus's ex-President Papadopoulos, officials say.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/europe/8558979.stm
                                [guid] => http://news.bbc.co.uk/2/hi/europe/8558979.stm
                                [pubDate] => Wed, 10 Mar 2010 00:07:16 GMT
                                [category] => Europe
                            )
    
                        [6] => SimpleXMLElement Object
                            (
                                [title] => Nigeria 'ignored attack warnings'
                                [description] => A Nigerian governor accuses the army of ignoring warnings of attacks, as communal tension remains high near Jos.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/africa/8558246.stm
                                [guid] => http://news.bbc.co.uk/2/hi/africa/8558246.stm
                                [pubDate] => Tue, 09 Mar 2010 22:50:04 GMT
                                [category] => Africa
                            )
    
                        [7] => SimpleXMLElement Object
                            (
                                [title] => US woman on 'jihad terror' charge
                                [description] => A US woman from Pennsylvania faces charges of using the internet to recruit female militant fighters for deadly attacks abroad.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/americas/8558864.stm
                                [guid] => http://news.bbc.co.uk/2/hi/americas/8558864.stm
                                [pubDate] => Tue, 09 Mar 2010 23:56:49 GMT
                                [category] => Americas
                            )
    
                        [8] => SimpleXMLElement Object
                            (
                                [title] => Producer admits Letterman blackmail
                                [description] => A US TV producer pleads guilty to attempting to blackmail US chat show host David Letterman over his affairs.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/entertainment/8558781.stm
                                [guid] => http://news.bbc.co.uk/2/hi/entertainment/8558781.stm
                                [pubDate] => Tue, 09 Mar 2010 21:38:35 GMT
                                [category] => Entertainment
                            )
    
                        [9] => SimpleXMLElement Object
                            (
                                [title] => Good egg - DNA of extinct birds found in ancient shells
                                [description] => The eggshells of long-dead and extinct species are a particularly good source to find preserved DNA, researchers say.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/science/nature/8558683.stm
                                [guid] => http://news.bbc.co.uk/2/hi/science/nature/8558683.stm
                                [pubDate] => Wed, 10 Mar 2010 00:22:57 GMT
                                [category] => Science & Environment
                            )
    
                        [10] => SimpleXMLElement Object
                            (
                                [title] => UK gives condom aid ahead of World Cup 'festivities'
                                [description] => The UK donates £1m ($1.5m) to South Africa to buy 42m condoms, as the nation builds up to the football World Cup.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/africa/8557582.stm
                                [guid] => http://news.bbc.co.uk/2/hi/africa/8557582.stm
                                [pubDate] => Tue, 09 Mar 2010 16:21:57 GMT
                                [category] => Africa
                            )
    
                        [11] => SimpleXMLElement Object
                            (
                                [title] => Bendtner shines as Arsenal progress
                                [description] => Nicklas Bendtner silences his recent critics with a hat-trick as Arsenal see off Porto in emphatic fashion to reach the Champions League quarter-finals.
                                [link] => http://news.bbc.co.uk/go/rss/-/sport2/hi/football/europe/8555855.stm
                                [guid] => http://news.bbc.co.uk/sport2/hi/football/europe/8555855.stm
                                [pubDate] => Tue, 09 Mar 2010 21:41:03 GMT
                                [category] => Europe
                            )
    
                        [12] => SimpleXMLElement Object
                            (
                                [title] => Portsmouth 1-2 Birmingham
                                [description] => Two goals from Cameron Jerome inspire Birmingham to a battling win against bottom-club Portsmouth at Fratton Park.
                                [link] => http://news.bbc.co.uk/go/rss/-/sport2/hi/football/eng_prem/8549395.stm
                                [guid] => http://news.bbc.co.uk/sport2/hi/football/eng_prem/8549395.stm
                                [pubDate] => Tue, 09 Mar 2010 21:41:37 GMT
                                [category] => Premier League
                            )
    
                        [13] => SimpleXMLElement Object
                            (
                                [title] => Can US broker Middle East peace?
                                [description] => Joe Biden is in the region to encourage talks between the Palestinians and Israel. What can be achieved?
                                [link] => http://www.bbc.co.uk/go/rss/-/blogs/haveyoursay/2010/03/can_the_us_vicepresident_broke.html
                                [guid] => http://www.bbc.co.uk/blogs/haveyoursay/2010/03/can_the_us_vicepresident_broke.html
                                [pubDate] => Tue, 09 Mar 2010 12:24:35 GMT
                                [category] => Middle East
                            )
    
                        [14] => SimpleXMLElement Object
                            (
                                [title] => BBC World News
                                [description] => SimpleXMLElement Object
                                    (
                                    )
    
                                [link] => http://www.bbcworldnews.com/Pages/default.aspx
                                [guid] => http://www.bbcworldnews.com/Pages/default.aspx
                                [pubDate] => Tue, 29 Sep 2009 11:16:12 GMT
                                [category] => Programmes
                            )
    
                        [15] => SimpleXMLElement Object
                            (
                                [title] => Tracked down
                                [description] => Paedophile priest left untouched by Church and state
                                [link] => http://news.bbc.co.uk/go/rss/-/1/hi/programmes/newsnight/8556659.stm
                                [guid] => http://news.bbc.co.uk/1/hi/programmes/newsnight/8556659.stm
                                [pubDate] => Tue, 09 Mar 2010 13:32:53 GMT
                                [category] => Newsnight
                            )
    
                        [16] => SimpleXMLElement Object
                            (
                                [title] => Galapagos tension
                                [description] => Can all species live side by side in unique ecosystem? 
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/americas/8549901.stm
                                [guid] => http://news.bbc.co.uk/2/hi/americas/8549901.stm
                                [pubDate] => Wed, 10 Mar 2010 00:31:38 GMT
                                [category] => Americas
                            )
    
                        [17] => SimpleXMLElement Object
                            (
                                [title] => Biswas on India
                                [description] => Women's quotas - historic moment for largest democracy
                                [link] => http://www.bbc.co.uk/go/rss/-/blogs/thereporters/soutikbiswas/
                                [guid] => http://www.bbc.co.uk/blogs/thereporters/soutikbiswas/
                                [pubDate] => Tue, 09 Mar 2010 09:00:47 GMT
                                [category] => promo
                            )
    
                        [18] => SimpleXMLElement Object
                            (
                                [title] => Earth Watch
                                [description] => Whales and tuna tied up in Eurotangle
                                [link] => http://www.bbc.co.uk/go/rss/-/blogs/thereporters/richardblack/
                                [guid] => http://www.bbc.co.uk/blogs/thereporters/richardblack/
                                [pubDate] => Wed, 20 Jan 2010 10:36:28 GMT
                                [category] => UK Politics
                            )
    
                        [19] => SimpleXMLElement Object
                            (
                                [title] => Thin red line
                                [description] => Lone stand of anti-Taliban militia in Pakistan
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/south_asia/8537127.stm
                                [guid] => http://news.bbc.co.uk/2/hi/south_asia/8537127.stm
                                [pubDate] => Tue, 09 Mar 2010 13:08:02 GMT
                                [category] => South Asia
                            )
    
                        [20] => SimpleXMLElement Object
                            (
                                [title] => Separation fear
                                [description] => Families fight 'racist' Israeli marriage and citizenship law
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/middle_east/8552816.stm
                                [guid] => http://news.bbc.co.uk/2/hi/middle_east/8552816.stm
                                [pubDate] => Tue, 09 Mar 2010 09:05:16 GMT
                                [category] => Middle East
                            )
    
                        [21] => SimpleXMLElement Object
                            (
                                [title] => Obama backs Greece on speculators
                                [description] => President Barack Obama has 'responded positively' to calls to clamp down on market speculators, says the Greek PM after talks.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/business/8558233.stm
                                [guid] => http://news.bbc.co.uk/2/hi/business/8558233.stm
                                [pubDate] => Tue, 09 Mar 2010 21:27:13 GMT
                                [category] => Business
                            )
    
                        [22] => SimpleXMLElement Object
                            (
                                [title] => EU concern over end of tanker bid
                                [description] => Brussels says it hopes European aerospace group EADS was not prevented from fairly bidding for a major US defence deal.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/business/8557769.stm
                                [guid] => http://news.bbc.co.uk/2/hi/business/8557769.stm
                                [pubDate] => Tue, 09 Mar 2010 22:44:03 GMT
                                [category] => Business
                            )
    
                        [23] => SimpleXMLElement Object
                            (
                                [title] => Aer Lingus to make 670 job cuts
                                [description] => Aer Lingus announces plans to lay off 670 staff, including nearly a quarter of its cabin crew, as part of restructuring plans.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/business/8558653.stm
                                [guid] => http://news.bbc.co.uk/2/hi/business/8558653.stm
                                [pubDate] => Tue, 09 Mar 2010 18:47:28 GMT
                                [category] => Business
                            )
    
                        [24] => SimpleXMLElement Object
                            (
                                [title] => Hard drive evolution could hit XP
                                [description] => Hard drives are about to undergo one of the biggest format shifts in 30 years but it  could cause problems for Windows XP users.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/technology/8557144.stm
                                [guid] => http://news.bbc.co.uk/2/hi/technology/8557144.stm
                                [pubDate] => Tue, 09 Mar 2010 11:29:38 GMT
                                [category] => Technology
                            )
    
                        [25] => SimpleXMLElement Object
                            (
                                [title] => Porn net domain name plan revived
                                [description] => A plan to create a .xxx net domain for adult content will be revisited three years after it was rejected by internet regulators.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/technology/8556364.stm
                                [guid] => http://news.bbc.co.uk/2/hi/technology/8556364.stm
                                [pubDate] => Tue, 09 Mar 2010 11:51:07 GMT
                                [category] => Technology
                            )
    
                        [26] => SimpleXMLElement Object
                            (
                                [title] => Nanotech 'fuse' for novel battery
                                [description] => A never-before-seen reaction in nanotubes could make for batteries that pack a mighty punch, say researchers.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/science/nature/8556656.stm
                                [guid] => http://news.bbc.co.uk/2/hi/science/nature/8556656.stm
                                [pubDate] => Tue, 09 Mar 2010 17:17:28 GMT
                                [category] => Science & Environment
                            )
    
                        [27] => SimpleXMLElement Object
                            (
                                [title] => Pink Floyd take EMI to court
                                [description] => Pink Floyd launch legal action in the High Court against EMI over payment of online royalties and marketing of their music.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/entertainment/8558480.stm
                                [guid] => http://news.bbc.co.uk/2/hi/entertainment/8558480.stm
                                [pubDate] => Tue, 09 Mar 2010 17:31:30 GMT
                                [category] => Entertainment
                            )
    
                        [28] => SimpleXMLElement Object
                            (
                                [title] => Oscars 'most watched since 2005'
                                [description] => This year's Oscars ceremony in Los Angeles was the most watched in the US for five years, early figures suggest.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/entertainment/8557089.stm
                                [guid] => http://news.bbc.co.uk/2/hi/entertainment/8557089.stm
                                [pubDate] => Tue, 09 Mar 2010 11:10:05 GMT
                                [category] => Entertainment
                            )
    
                        [29] => SimpleXMLElement Object
                            (
                                [title] => LHC fault forces 2011 shutdown 
                                [description] => The Large Hadron Collider must be shut down for a year starting in late 2011 to address design flaws, the BBC has learned.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/science/nature/8556621.stm
                                [guid] => http://news.bbc.co.uk/2/hi/science/nature/8556621.stm
                                [pubDate] => Wed, 10 Mar 2010 00:19:18 GMT
                                [category] => Science & Environment
                            )
    
                        [30] => SimpleXMLElement Object
                            (
                                [title] => Third of EU emissions 'imported'
                                [description] => Research shows some EU countries "import" about a third of their carbon emissions from developing countries.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/science/nature/8557461.stm
                                [guid] => http://news.bbc.co.uk/2/hi/science/nature/8557461.stm
                                [pubDate] => Tue, 09 Mar 2010 12:46:03 GMT
                                [category] => Science & Environment
                            )
    
                        [31] => SimpleXMLElement Object
                            (
                                [title] => 'No proof' IVF aided by acupuncture
                                [description] => There is no evidence acupuncture or Chinese herbal medicine boost the chance of IVF success, fertility experts warn.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/health/8558527.stm
                                [guid] => http://news.bbc.co.uk/2/hi/health/8558527.stm
                                [pubDate] => Wed, 10 Mar 2010 00:17:10 GMT
                                [category] => Health
                            )
    
                        [32] => SimpleXMLElement Object
                            (
                                [title] => Somali hope for Chandlers release
                                [description] => Somali President Sharif Sheikh Ahmed says efforts are being made to release a UK couple held by pirates as soon as possible.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/uk_news/england/kent/8557865.stm
                                [guid] => http://news.bbc.co.uk/2/hi/uk_news/england/kent/8557865.stm
                                [pubDate] => Tue, 09 Mar 2010 17:14:19 GMT
                                [category] => Kent
                            )
    
                        [33] => SimpleXMLElement Object
                            (
                                [title] => Row over exhuming Rwanda leader
                                [description] => The son of Rwanda's first President, Dominique Mbonyumutwa, protests at orders to exhume his father's remains.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/africa/8557085.stm
                                [guid] => http://news.bbc.co.uk/2/hi/africa/8557085.stm
                                [pubDate] => Tue, 09 Mar 2010 15:25:11 GMT
                                [category] => Africa
                            )
    
                        [34] => SimpleXMLElement Object
                            (
                                [title] => Brazil slaps trade sanctions on US
                                [description] => Brazil is backed by the WTO as it slaps trade sanctions against US imports in retaliation for illegal subsidies to cotton farmers.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/americas/8556920.stm
                                [guid] => http://news.bbc.co.uk/2/hi/americas/8556920.stm
                                [pubDate] => Tue, 09 Mar 2010 08:31:50 GMT
                                [category] => Americas
                            )
    
                        [35] => SimpleXMLElement Object
                            (
                                [title] => Greece asks US for its assistance
                                [description] => Greece's prime minister asks the US to crack down on speculators he blames for worsening his country's debt woes.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/business/8556944.stm
                                [guid] => http://news.bbc.co.uk/2/hi/business/8556944.stm
                                [pubDate] => Tue, 09 Mar 2010 12:37:41 GMT
                                [category] => Business
                            )
    
                        [36] => SimpleXMLElement Object
                            (
                                [title] => Raids 'kill Indonesia militants'
                                [description] => Indonesian security forces kill three alleged militants and investigate whether one is key Bali bomb suspect Dulmatin.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/asia-pacific/8556964.stm
                                [guid] => http://news.bbc.co.uk/2/hi/asia-pacific/8556964.stm
                                [pubDate] => Tue, 09 Mar 2010 16:24:45 GMT
                                [category] => Asia-Pacific
                            )
    
                        [37] => SimpleXMLElement Object
                            (
                                [title] => Japan confirms secret nuclear pact
                                [description] => Japan confirms the existence of a secret Cold War pact allowing nuclear-armed US vessels to call at its ports. 
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/asia-pacific/8557346.stm
                                [guid] => http://news.bbc.co.uk/2/hi/asia-pacific/8557346.stm
                                [pubDate] => Tue, 09 Mar 2010 11:00:06 GMT
                                [category] => Asia-Pacific
                            )
    
                        [38] => SimpleXMLElement Object
                            (
                                [title] => Irish 'plot to kill cartoonist'
                                [description] => Seven people are arrested in the Irish Republic over an alleged plot to kill a Swedish cartoonist for depicting the Prophet Muhammad.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/europe/8558022.stm
                                [guid] => http://news.bbc.co.uk/2/hi/europe/8558022.stm
                                [pubDate] => Tue, 09 Mar 2010 18:24:33 GMT
                                [category] => Europe
                            )
    
                        [39] => SimpleXMLElement Object
                            (
                                [title] => Alcohol ban means Irish rugby fans face long Good Friday
                                [description] => Row as Irish crunch match Munster v Leinster is a mouth-watering clash for any rugby fan, but most drinkers in the Irish Republic wanting to toast the Magners League fixture in a bar next month will have to leave the country.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/europe/8557818.stm
                                [guid] => http://news.bbc.co.uk/2/hi/europe/8557818.stm
                                [pubDate] => Tue, 09 Mar 2010 16:57:16 GMT
                                [category] => Europe
                            )
    
                        [40] => SimpleXMLElement Object
                            (
                                [title] => Israeli 'spymaster' dies aged 82
                                [description] => A former Israeli spy who played a key role in Africa, the Middle East and the 1980s Iran-Contra scandal has died. 
    
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/middle_east/8558332.stm
                                [guid] => http://news.bbc.co.uk/2/hi/middle_east/8558332.stm
                                [pubDate] => Tue, 09 Mar 2010 17:11:52 GMT
                                [category] => Middle East
                            )
    
                        [41] => SimpleXMLElement Object
                            (
                                [title] => Israel and Syria in nuclear bids
                                [description] => Israel and Syria both tell a conference in Paris they want to use nuclear power to generate electricity.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/middle_east/8558160.stm
                                [guid] => http://news.bbc.co.uk/2/hi/middle_east/8558160.stm
                                [pubDate] => Tue, 09 Mar 2010 16:01:25 GMT
                                [category] => Middle East
                            )
    
                        [42] => SimpleXMLElement Object
                            (
                                [title] => Indian MPs approve women's bill
                                [description] => India's upper house sees a second day of uproar as it backs a bill to reserve a third of all parliamentary seats for women.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/south_asia/8557237.stm
                                [guid] => http://news.bbc.co.uk/2/hi/south_asia/8557237.stm
                                [pubDate] => Tue, 09 Mar 2010 18:02:49 GMT
                                [category] => South Asia
                            )
    
                        [43] => SimpleXMLElement Object
                            (
                                [title] => UK seeks Afghan political drive
                                [description] => Foreign Secretary David Miliband is to urge the Afghan president to seek a political solution to the conflict with the Taliban.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/uk_news/8559034.stm
                                [guid] => http://news.bbc.co.uk/2/hi/uk_news/8559034.stm
                                [pubDate] => Wed, 10 Mar 2010 01:04:10 GMT
                                [category] => UK
                            )
    
                        [44] => SimpleXMLElement Object
                            (
                                [title] => US 'hid terror suspect treatment'
                                [description] => A former head of MI5 says she did not know US intelligence services were mistreating terror suspects until after she retired.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/uk_news/8558958.stm
                                [guid] => http://news.bbc.co.uk/2/hi/uk_news/8558958.stm
                                [pubDate] => Wed, 10 Mar 2010 00:01:57 GMT
                                [category] => UK
                            )
    
                        [45] => SimpleXMLElement Object
                            (
                                [title] => Baby P rules 'may increase risks'
                                [description] => Rules to improve child protection after the Baby P case may leave children more vulnerable to harm, council leaders warn.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/uk_news/8558463.stm
                                [guid] => http://news.bbc.co.uk/2/hi/uk_news/8558463.stm
                                [pubDate] => Wed, 10 Mar 2010 01:16:16 GMT
                                [category] => UK
                            )
    
                        [46] => SimpleXMLElement Object
                            (
                                [title] => Banking fraud 'moves to internet'
                                [description] => Fraudsters are continuing their switch from traditional card fraud to raiding online bank accounts, new research shows.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/business/8558535.stm
                                [guid] => http://news.bbc.co.uk/2/hi/business/8558535.stm
                                [pubDate] => Wed, 10 Mar 2010 00:00:44 GMT
                                [category] => Business
                            )
    
                        [47] => SimpleXMLElement Object
                            (
                                [title] => Social care deal 'dead in water'
                                [description] => Attempts to reach a cross-party deal on funding social care appear to be dead in the water ahead of a key summit.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/health/8557775.stm
                                [guid] => http://news.bbc.co.uk/2/hi/health/8557775.stm
                                [pubDate] => Wed, 10 Mar 2010 00:36:06 GMT
                                [category] => Health
                            )
    
                        [48] => SimpleXMLElement Object
                            (
                                [title] => MPs query coalfields revival plan
                                [description] => A £1bn government scheme to regenerate former coalmines has been poorly co-ordinated and lacks vision, MPs argue.
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/uk_news/politics/8558647.stm
                                [guid] => http://news.bbc.co.uk/2/hi/uk_news/politics/8558647.stm
                                [pubDate] => Wed, 10 Mar 2010 00:12:04 GMT
                                [category] => UK Politics
                            )
    
                        [49] => SimpleXMLElement Object
                            (
                                [title] => UK trade gap unexpectedly widens
                                [description] => The UK goods trade deficit with the rest of the world widens in January, causing the pound to dip below $1.50. 
                                [link] => http://news.bbc.co.uk/go/rss/-/2/hi/business/8557200.stm
                                [guid] => http://news.bbc.co.uk/2/hi/business/8557200.stm
                                [pubDate] => Tue, 09 Mar 2010 20:38:26 GMT
                                [category] => Business
                            )
    
                    )
    
            )
    
    )

    This magic here, it is done by a single function simplexml_load_file() which is supported from php 5. This function converts the well-formed XML document in the given file to an SimpleXMLElement object.


    This xml data in SimpleXMLElement Object, you can access these data as shown in the third line of the code,  $SimpleXML_object->channel->title  will give you the title of the feed. You have to use the schema of the xml to get the correct data.

    Now, I want to convert this SimpleXMLElement Object into an associative array. Personally, I like to deal with associative arrays rather than this Object to avoid type casting and other things.

    Usually, I use the following function to do this, I like this one because, it doesn't add the attributes and I don't need that. But if you need those attributes, you may check for some other functions here. Probably, I picked the following function from this page too.

    function convertXmlObjToArr( $obj, &$arr )
    
        {
            $children = $obj->children();
            $executed = false;
            foreach ($children as $elementName => $node)
            {
                if( @array_key_exists( $elementName , $arr ) )
                {
                    if(@array_key_exists( 0 ,$arr[$elementName] ) )
                    {
                        $i = count($arr[$elementName]);
                        convertXmlObjToArr ($node, $arr[$elementName][$i]);    
                    }
                    else
                    {
                        $tmp = $arr[$elementName];
                        $arr[$elementName] = array();
                        $arr[$elementName][0] = $tmp;
                        $i = count($arr[$elementName]);
                        convertXmlObjToArr($node, $arr[$elementName][$i]);
                    }
                }
                else
                {
                    $arr[$elementName] = array();
                    convertXmlObjToArr($node, $arr[$elementName]);   
                }
                $executed = true;
            }
            if(!$executed&&$children->getName()=="")
            {    
                $arr = (String)$obj;
            }
            return ;
        }

    Usage of this function : the full code will look like this


    $SimpleXML_object = simplexml_load_file("http://newsrss.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml");
    $XMLDataAraay = array();
    convertXmlObjToArr($SimpleXML_object, $XMLDataAraay);
    print_r($XMLDataAraay);



    And now, you will be able to access the title this way.   $XMLDataAraay['channel']['title'] 


    Here, the last step of converting the Object into an Array may seem unnecessary. You can avoid this and deal directly with the object too.



    And, Here is the new problem. If your site where you are going to use and extend this code to show data, this script will load the xml file every time from BBC's server. But every time, it is getting the same page unless this feed is updated in every minute.
    2 problems are there I can remember of it for now, 
    One, it is consuming your server's valuable bandwidth and another is the slower response of fetching the file first and then processing it.


    Solution  : solution of this kind of problem is Caching. You can save a cache of the xml file with a suitable timeout and a timeout mechanism.. This system will save a copy of the xml file and every-time you request this file, this mechanism will look for the Cached file if the timeout interval (say, 15 minute, this means, we are expecting that this new feed will not be updated in next 15 minutes or, we can allow this maximum delay of getting that new news) of that cache file hasn't reached. If the cache file has timed out, it will refresh the cache file with new one and deliver it.

    Sounds too much complex ? Actually, it is just a few lines of php code. I will discuss it some other day.  :)


    
    

    SQL string comparison and LIKE operator

    0 comments
    The original reference to this blog is from MySQL's online documentation. , msdn and another site.
    There are three string comparison functions as defined in MySQL's documentation.

    LIKE               Simple pattern matching
    NOT LIKE     Negation of simple pattern matching
    STRCMP()     Compare two strings



    The description of this operator in msdn seemed  more readable, that's why I am using that in my next paragraphs. For complete reference, try one of these links.

    LIKE
    Determines whether or not a given character string matches a specified pattern. A pattern can include regular characters and wildcard characters.
    Syntax
    match_expression [ NOT ] LIKE pattern [ ESCAPE escape_character ]
    Arguments
    match_expression
    Is any valid SQL Server expression of character string data type.
    pattern
    Is the pattern to search for in match_expression, and can include these valid SQL Server wildcard characters.


    Wildcard character Description Example
    % Any string of zero or more characters. WHERE title LIKE '%computer%' finds all book titles with the word 'computer' anywhere in the book title.
    _ (underscore) Any single character. WHERE au_fname LIKE '_ean' finds all four-letter first names that end with ean (Dean, Sean, and so on).
    [ ] Any single character within the specified range ([a-f]) or set ([abcdef]). WHERE au_lname LIKE '[C-P]arsen' finds author last names ending with arsen and beginning with any single character between C and P, for example Carsen, Larsen, Karsen, and so on.
    [^] Any single character not within the specified range ([^a-f]) or set ([^abcdef]). WHERE au_lname LIKE 'de[^l]%' all author last names beginning with de and where the following letter is not l.

    escape_character
    Is any valid SQL Server expression of any of the data types of the character string data type category. escape_character has no default and must consist of only one character.
    Result Types
    Boolean
    Result Value
    LIKE returns TRUE if the match_expression matches the specified pattern.


    That's enough of the theories, Now let's have a look at some examples.



    Example Queries using % wildcard

    The first example that we'll take a look at involves using % in the where clause of a select statement. We are going to try to find all of the suppliers whose name begins with 'Hew'.
    SELECT * FROM suppliers
        WHERE supplier_name like 'Hew%';

    You can also using the wildcard multiple times within the same string. For example,
    SELECT * FROM suppliers
        WHERE supplier_name like '%bob%';

    In this example, we are looking for all suppliers whose name contains the characters 'bob'.

    You could also use the LIKE condition to find suppliers whose name does not start with 'T'. For example,
    SELECT * FROM suppliers
        WHERE supplier_name not like 'T%';

    By placing the not keyword in front of the LIKE condition, you are able to retrieve all suppliers whose name does not start with 'T'.


    Example Queries using _ wildcard
    Next, let's explain how the _ wildcard works. Remember that the _ is looking for only one character.
    For example,
    SELECT * FROM suppliers
        WHERE supplier_name like 'Sm_th';

    This SQL statement would return all suppliers whose name is 5 characters long, where the first two characters is 'Sm' and the last two characters is 'th'. For example, it could return suppliers whose name is 'Smith', 'Smyth', 'Smath', 'Smeth', etc.

    Here is another example,
    SELECT * FROM suppliers
        WHERE account_number like '12317_';

    You might find that you are looking for an account number, but you only have 5 of the 6 digits. The example above, would retrieve potentially 10 records back (where the missing value could equal anything from 0 to 9). For example, it could return suppliers whose account numbers are:


    123170
    123171
    123172
    123173
    123174
    123175
    123176
    123177
    123178
    123179.


    Example Queries using Escape Characters
    Next, in Oracle, let's say you wanted to search for a % or a _ character in a LIKE condition. You can do this using an Escape character.
    Please note that you can define an escape character as a single character (length of 1) ONLY.
    For example,
    SELECT * FROM suppliers
        WHERE supplier_name LIKE '!%' escape '!';

    This SQL statement identifies the ! character as an escape character. This statement will return all suppliers whose name is %.

    Here is another more complicated example:
    SELECT * FROM suppliers
        WHERE supplier_name LIKE 'H%!%' escape '!';

    This example returns all suppliers whose name starts with H and ends in %. For example, it would return a value such as 'Hello%'.

    You can also use the Escape character with the _ character. For example,
    SELECT * FROM suppliers
        WHERE supplier_name LIKE 'H%!_' escape '!';

    This example returns all suppliers whose name starts with H and ends in _. For example, it would return a value such as 'Hello_'.

    Redirecting cron output to a file and stop mailing

    0 comments
    Usually, there is an option of setting the cronjob with an email address. This email address is used to send the output of the cron script so that the developer or the system admin can check the output or use it for some other purpose.

    Now, this became very annoying for me when I tried to setup a Cronjob with an interval of every minute (run in every minute), and it was sending an email to my mailbox and after a few hours my mailbox became filled up with these emails.
    I want to read at these outputs later when I feel to check the output but don't want to mess with my mailbox.
    So, what is the solution ?

    Solution 1  : Very lame and quite obvious, use a separate address for your Cronjobs. Now a days, we can get a mailbox with almost unlimited disk access for free.

    Solution 2 : This is the blog all about. Save the output of that script in a file.



    Suppose, the command (here, i am talking about the comment that we usually enter while using Cpanel, please note that the command for setting up the Cronjob using SSH or Telnet is different than this) which was sending the outputs to that email was

    php your_cron_script.php

    Now, what we need is, use it any of the following


    Use this to send the output to a file named   "cron_script.log"
    php your_cron_script.php >cron_script.log

    The above command will create a file named "cron_script.log" if one doesn't exist or overwrite if a file with that name exists in the directory. Use the following if you want to append the output rather than overwriting the existing file.
    php your_cron_script.php >>cron_script.log

    These above two deals with the scripts output. But notice that it is not the error log. These commands will put the output and error information into separate files.

    If you prefer to capture both output and error outputs (if any) to the same file, you can use the ?& operator to combine the two outputs. So, the command will look like,
    php your_cron_script.php >>cron_script.log 2>&1

    What are these 2 and 1 in 2>&1 ? These are called the file descriptors. 1 is the standard output file descriptor and 2 is the file descriptor for standard error.

    So, finally, we could save and append the output of  the script to a specific file in an specific folder named "cronputputs" and save (append) thestandard errors of that script to another file in an specific folder named "cronerrors"  using the following command.
    php your_cron_script.php >>/cronputputs/cron_script.log 2>>/cronerrors/cron_script_error.log



    The above commands and more advanced options about these can be found in any book about "Shell Programming". I used Beginning Linux Programming 3rd Edition published by Wrox.(chapter 2 : Shell Programming).

    php cronjob command and GET parameters

    0 comments
    If any reader here doesn't know what is a cronjob, for them in short, Cronjob is the scheduled job for a server to execute some command periodically in the server.
    In unix server with cpanel access, usually you will find that in Advanced tad in an icon named "CronJobs"
    You can execute any command and also call a php script too.


    Now, to the topic what happened with me.

    Problem : 

    While trying to make a script for cronjob, I coded the script to accept parameters in GET method.
    The cron's command was like,


    php /home/hosting_account/public_html/mywebsite.com/cronscript.php?sindex=VideoGames&bnode=345135031&PSPConsole=1&session_call_max=5
     
     
     
    

    This command resulted into a message like,. "input file not found" .....

    If this command looks unfamiliar, it is just a simple command to execute a php file from Command Line. Notice the space after php command. php is the executable's name for php and after a space next one is the php file's server absolute address.

    Now, why this thing happened ? It was very foolish of me that I couldn't think about some basic ideas.



    GET and POST methods are two different methods that are solely for HTTP call to a script. But here, we are writing a command that will be run exactly as we could run it from my pc's "cmd" command line.

    That's why when we are writing a command, it doesn't recognize the GET parameter part of it.

    So what should we do ?

    Solution :  

    The correct command will be like,

    php /home/hosting_account/public_html/mywebsite.com/cronscript.php sindex=VideoGames bnode=345135031 PSPConsole=1 session_call_max=5   
    

    Explanation : This means, every parameter should be separated by a space  and when passed to the script, it is passed through the associative array $_REQUEST  rather than in $_GET

    So, in the script, where we would write $_GET['sindex'], we will have to write $_REQUEST['sindex']

    New unnecessary problem : Now, what came to my mind that, I want this same script to be called by a cronjob and also from my browser's URL, how I we do that ?

    Just add this code below at the beginning of the cron script.

                // If the $_REQUEST array has your parameters, 
                // then assign this to $_GET
                if(isset($_REQUEST['sindex']) AND $_REQUEST['bnode']){
                    $_GET = $_REQUEST ;
                }
    
    
    


    But, this solution's code needs to be changed to check the correct parameter. But, more generic solution will be,

                if(isset($_REQUEST[$_SERVER['argv']['0']])){
                    $_GET = $_REQUEST ;
                }
    
    

    I has been a long discussion for this simple thing, So, I am not going to explain this last code  ;)

    how to calculate script's execution time in php

    0 comments
    Many times, we need to check what was the execution time for a script we have written in PHP. This was necessary for me when I was requesting some data using an API and the execution time of each api call needs to be traced.
    We need to take care of the execution time (minimize it) to ensure that the script is not Timed Out in the server.
    Doing this is quite simple.
    Just a php function microtime( ) is enough to do this.


    Here is the example : 


    $start_time = microtime(true);
    // Do some work or your whole script
    $end_time = microtime(true);
    $execution_time = $end_time - $start_time ;

    Now, the function's details


    microtime
    (PHP 4, PHP 5)

    microtime — Return current Unix timestamp with microseconds

    Description
    mixed microtime ([ bool $get_as_float ] )

     microtime() returns the current Unix timestamp with microseconds. This function is only available on operating systems that support the gettimeofday() system call.

    Parameters


    get_as_float
    When called without the optional argument, this function returns the string "msec sec" where sec is the current time measured in the number of seconds since the Unix Epoch (0:00:00 January 1, 1970 GMT), and msec is the microseconds part. Both portions of the string are returned in units of seconds.

    If the optional get_as_float is set to TRUE then a float (in seconds) is returned.
    And now, here is an Object Oriented approach of doing this
    example :

        $timer = new Timer();     $timer->start();
        //   Add the codes whose time you need to count
        $timer->stop();     echo "elapsed time ".$timer->getTime() ;  // Use method    getTime()   to get the elapsed time in seconds     $timer>printTime();                      // Use printTime()   to  echo the elapsed time 
                                                 //like, Elapsed Time : 0.0893499851227 seconds
       
    //And, here is the class :
    class Timer {     var $clock = 0;     var $elapsed = 0 ;     function start () {         $this->clock = microtime(true);         $this->elapsed = 0 ;         return true ;     }     function stop () {         if($this->clock == 0) return false ;                $this->elapsed = microtime(true) - $this->clock ;         return true ;     }         function getTime(){         return $this->elapsed ;     }         function printTime(){         if($this->elapsed != 0){             $txt = "Elapsed Time : " . $this->elapsed . "  seconds";         }else if($this->clock == 0){             $txt = "Counter Not started yet";         }else{             $txt = "Counter started but Not stopped";         }         echo " ".$txt." ";         return $txt ;     } }

    Javascript page Redirection

    0 comments
    Maybe, you have seen while browsing internet this thing. When a form has been submitted successfully, in the webpage, there is some text like saying,
    Update Successful, You will be redirected after 5 seconds. If you are not redirect automatically, please click in this link to redirect.
    or, 
    This site has been moved to a new domain, You will be redirected after 5 seconds.
    The later message is shown when the website has been moved to a new domain name and you need to redirect the user, 
    Now, how it's done
    Here is the sample script to do this




    <html>
    <head>
    <script type="text/javascript">
    <!--
    function delayer(){
    window.location = "../javascriptredirect.php"
    }
    //-->
    </script>
    </head>
    <body onLoad="setTimeout('delayer()', 5000)">

    <h2>Prepare to be redirected!</h2>
    <p>This page is a time delay redirect, please update your bookmarks to our new
    location!</p>

    </body>
    </html>




    Now, the explanation of this code fragment 
    When the page is loaded, the onLoad event executes this script 
    
    setTimeout('redirect()', 5000)
    It sets a timeout after 5000 miliseconds / 5 seconds and after 5 seconds, the function redirect is called. In the function redirect, this window's location property is changed and this window is redirected to http://new-domain.com
    The setTimeout() Method
    Syntax var t=setTimeout("javascript statement",milliseconds);
    The setTimeout() method returns a value - In the statement above, the value is stored in a variable called t. If you want to cancel this setTimeout(), you can refer to it using the variable name. The first parameter of setTimeout() is a string that contains a JavaScript statement. This statement could be a statement like "alert('5 seconds!')" or a call to a function, like "redirect()". The second parameter indicates how many milliseconds from now you want to execute the first parameter. Note: There are 1000 milliseconds in one second.
    Window Object  The window object represents an open window in a browser.
    window.location  : Returns the Location object for the window