How to avoid showing more than 3 google adsense content units on a page

Google adsense is being used by publishers to show advertisements on their webpages, websites and web properties. Due to dynamic nature of various web pages some times it happens that more than maximum number of ads are shown than allowed. Like in case of Google Adsense it is 3 and it may happen that you have put the adsense code 4 times on a page like 1 in header section, other in content section, another in sidebar and 4th one in footer. And this may happen when you have common header, sidebar and footer i.e. 3 ads are running on every page and you have inserted one more ad in content, making the total count 4.

So, to avoid such kind of situation use simple PHP code to control it. In your main settings file create a variable say,

$ad_max_allowed=3;

and initialise ad showing counter to 0

$ad_shown_count=0;

Now, where you show an ad, increment this counter and check if that count has exceeded more than the allowed number of ads.

if($ad_shown_count<$ad_max_allowed)
{
$ad_shown_count++;
//display ad here
}

With this implementation, only maximum number of ads will be shown.

Now, tweaking this code further for a dynamic website. Lets say on some pages we want to display ads in header, content and sidebar and on other page we want to show ads on header, sidebar and footer only.
To achieve this, define slots in database and in your pages. First create a page table where page information is stored. In this table create a column say, ad_slots and write a string like 1110 which means slot 1, slot 2 and slot 3 are activated for this particular page and for the second page it could be 1011 which means slot 1, slot3 and slot 4 are activated. Here,

  • Slot 1 – Header
  • Slot 2 – Content
  • Slot 3 – Sidebar
  • Slot 4 – Footer

This could be changed and made more advanced as per the requirement.

Now, when a page is being rendered just check before the adsense code if this ad slot has to be shown on this page or not.

$ad_slot_number=2; //Slot number defined for this slot.
if($ad_slot_array[$ad_slot_number]=='1')
{
if($ad_shown_count<$ad_max_allowed)
{
$ad_shown_count++;
display ad here
}
}

Here, $ad_slot_number is the ad slot number for the page. And $ad_slot_array is the array formed from the ad_slot column from page table where ad_slot string is converted to array.

Now, it is checked if ad_slot_number in $ad_slot_array value is 1. If it is 1 then show the ad.

While implementing this technique make sure that spacing, segments, DIV and TABLE are being displayed correctly.

Category: PHP  Tags: , ,  Leave a Comment

How to implement Dynamic CSS Sprite Painting effect using CSS and PHP

It is very easy to give a painting effect to images on your website. Just use simple CSS Sprite logic only or use PHP (or other scripting language like ASP etc.) with CSS Sprite to make it easy and more dynamic. Read more about this effect at Dynamic CSS Sprite implementation gives a Painting effect

Include following in your CSS Style Sheet

<style type="text/css">
.sprite-img-color { background-image : url('images/logo/kunal-bansal.jpg'); background-color : transparent; background-repeat : no-repeat;
cursor:url(images/img/paint-brush-drip.gif),auto;}
.sprite-img-bw { background-image : url('images/logo/kunal-bansal-bw.jpg'); background-color : transparent; background-repeat : no-repeat;
cursor:url(paint-brush.gif),auto;
}
</style>

To change the cursor to a paint bursh, following code has been added to the style above

cursor:url(paint-brush.gif),auto;

Change Painting Brush image according to your requirement.

Now, define each individual division of two images using CSS sprite (non-dynamic), something like:

//Code for Row 1 of image sprite
<div style='clear:left;'>
<div id='img_div_id_00' class='sprite-img-bw' style='width:20px; height:20px; background-position: -0px -0px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_00").setAttribute("class", "sprite-img-color");'>
</div>
<div id='img_div_id_01' class='sprite-img-bw' style='width:20px; height:20px; background-position: -20px -0px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_01").setAttribute("class", "sprite-img-color");'>
</div>
<div id='img_div_id_02' class='sprite-img-bw' style='width:20px; height:20px; background-position: -40px -0px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_02").setAttribute("class", "sprite-img-color");'>
</div>
<div id='img_div_id_03' class='sprite-img-bw' style='width:20px; height:20px; background-position: -60px -0px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_03").setAttribute("class", "sprite-img-color");'>
</div>
<div id='img_div_id_04' class='sprite-img-bw' style='width:20px; height:20px; background-position: -80px -0px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_04").setAttribute("class", "sprite-img-color");'>
</div>
<div id='img_div_id_05' class='sprite-img-bw' style='width:20px; height:20px; background-position: -100px -0px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_05").setAttribute("class", "sprite-img-color");'>
</div>
<div id='img_div_id_06' class='sprite-img-bw' style='width:20px; height:20px; background-position: -120px -0px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_06").setAttribute("class", "sprite-img-color");'>
</div>
<div id='img_div_id_07' class='sprite-img-bw' style='width:20px; height:20px; background-position: -140px -0px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_07").setAttribute("class", "sprite-img-color");'>
</div>
<div id='img_div_id_08' class='sprite-img-bw' style='width:20px; height:20px; background-position: -160px -0px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_08").setAttribute("class", "sprite-img-color");'>
</div>
<div id='img_div_id_09' class='sprite-img-bw' style='width:20px; height:20px; background-position: -180px -0px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_09").setAttribute("class", "sprite-img-color");'>
</div>
</div>

//Code for Row 2 of image sprite
<div style='clear:left;'>
<div id='img_div_id_10' class='sprite-img-bw' style='width:20px; height:20px; background-position: -0px -20px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_10").setAttribute("class", "sprite-img-color");'>
</div>
<div id='img_div_id_11' class='sprite-img-bw' style='width:20px; height:20px; background-position: -20px -20px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_11").setAttribute("class", "sprite-img-color");'>
</div>
<div id='img_div_id_12' class='sprite-img-bw' style='width:20px; height:20px; background-position: -40px -20px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_12").setAttribute("class", "sprite-img-color");'>
</div>
<div id='img_div_id_13' class='sprite-img-bw' style='width:20px; height:20px; background-position: -60px -20px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_13").setAttribute("class", "sprite-img-color");'>
</div>
<div id='img_div_id_14' class='sprite-img-bw' style='width:20px; height:20px; background-position: -80px -20px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_14").setAttribute("class", "sprite-img-color");'>
</div>
<div id='img_div_id_15' class='sprite-img-bw' style='width:20px; height:20px; background-position: -100px -20px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_15").setAttribute("class", "sprite-img-color");'>
</div>
<div id='img_div_id_16' class='sprite-img-bw' style='width:20px; height:20px; background-position: -120px -20px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_16").setAttribute("class", "sprite-img-color");'>
</div>
<div id='img_div_id_17' class='sprite-img-bw' style='width:20px; height:20px; background-position: -140px -20px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_17").setAttribute("class", "sprite-img-color");'>
</div>
<div id='img_div_id_18' class='sprite-img-bw' style='width:20px; height:20px; background-position: -160px -20px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_18").setAttribute("class", "sprite-img-color");'>
</div>
<div id='img_div_id_19' class='sprite-img-bw' style='width:20px; height:20px; background-position: -180px -20px; display:block; float:left;'
onmouseover='document.getElementById("img_div_id_19").setAttribute("class", "sprite-img-color");'>
</div>
</div>

Similarly, write code for other rows of image sprite

Following code is being used so that when a mouse is taken over a Sprite Block, colored block comes up.

onmouseover='document.getElementById("img_div_id_01").setAttribute("class", "sprite-img-color");'

To do it dynamically using PHP implement the following code, otherwise as mentioned in non-dynamic you have to define so many DIV tags. But with PHP it can be done easily.

<?php
$img_width=200; //Define Image Width
$img_height=260; //Define Image Height

 //If you don't know it or want to implement dynamic images which have different dimensions and images changes, then use the PHP function to calculate width and height. Just specify the image path and use following function

//$img_path="directory path to// image";
//list($img_width, $img_height, $type, $attr) = getimagesize($img_path);

$img_cols=10; //number of columns in which image will be divided. i.e. number of blocks to form
$img_rows=13; //number of rows in which image will be divided. i.e. number of blocks to form
//More the number of cols and rows better paiting effect.

$img_blocks=$img_cols*$img_rows; //Total Number of blocks
$img_block_width=ceil($img_width/$img_cols,0); //Try to provide img_cols in such a way so that img_blocks comes to whole number
$img_block_height=ceil($img_height/$img_rows,0);

$img_block_width_px=$img_block_width."px";
$img_block_height_px=$img_block_height."px";

$img_div_id_arr=array();
for($i_col=0; $i_col{
	echo "</pre>
<div style="clear: left;">";
 for($i_row=0; $i_row {
 $img_block_start_position=$i_row*$img_block_width;
 $img_block_end_position=$i_col*$img_block_height;

 $img_block_start_position_px=$img_block_start_position."px";
 $img_block_end_position_px=$img_block_end_position."px";
 $img_div_id="img_div_id_".$i_col.$i_row;
 $img_div_id_arr[]=$img_div_id;
echo "

";

 }
 echo "</div>
<pre>
";

}
?>

Trim a string without cutting words in PHP

Often we require to trim/cut a string to desired length. Using a function is suggested as writing code again and would be tedious task.

Below is the code of function by which we can cut a string to a desired length without cutting a word (optional) and including a desired symbol at the end of trimmed string.

function limitlength($oldstring, $newlength, $symbol, $method, $mode)
{
	if($symbol=='0')
	{
		$symbol="";
	}
	elseif($symbol=='1')
	{
		$symbol="..";
	}

	$oldstring_len=strlen($oldstring);
	if($oldstring_len>$newlength)
	{

		if($method=='1')
{
preg_match('/(.{' . $newlength . '}.*?)\b/', $oldstring, $matches);
		$newstring=rtrim($matches[1]);
}
else
{
$newstring=substr($oldstring,0,$newlength);
}
		$newstring.=$symbol;
	}
	else
	{
		$newstring=$oldstring;
	}

	if($mode=='0')
	{
	return($newstring);
	}
	elseif($mode=='1')
	{
	echo "$newstring";
	}
}
$newstring=limitlength("I am writing here some long string. But this string is not very long.", 25, "..", 0,0);
echo $newstring;

In the code above, there is a function limitlength to which we pass various settings like string to be trimmed, maximum length, if any symbol is to be used at the end of new string, trim string to desired length or trim string to without cutting of any word and mode to return or print result.

How to read an RSS Feed using PHP

There are many ways 2 read XML Feed using PHP.

Some basic ways with which this can be done are given below along with code:

Basic Way 1

<?php
function getFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);

echo "<ul>";

foreach($x->channel->item as $entry) {
echo "<li><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></li>";
}
echo "</ul>";
}
getFeed('http://somefeedurl/')
?>

In the above method, create a function and call that function.

basic Way 2

  <?php
  include('rssclass.php');
  $feedlist = new rss('http://feeds2.feedburner.com/9lesson');
  <a href="http://www.php.net/echo">echo</a> $feedlist->display(9,"9lessons");

  $feedlist = new rss('http://feeds.feedburner.com/nettuts');
  <a href="http://www.php.net/echo">echo</a> $feedlist->display(9,"Nettuts");

  $feedlist = new rss('http://feeds.labnol.org/labnol');
  <a href="http://www.php.net/echo">echo</a> $feedlist->display(9,"Labnol");
  ?>
<?php
 class rss {
     var $feed;

  function rss($feed)
    {   $this->feed = $feed;  }

  function parse()
    {
    $rss = simplexml_load_file($this->feed);

    $rss_split = <a href="http://www.php.net/array">array</a>();
    foreach ($rss->channel->item as $item) {
$title = (string) $item->title; // Title
    $link   = (string) $item->link; // Url Link
    $description = (string) $item->description; //Description
    $rss_split[] = '<div>
        <a href="'.$link.'" target="_blank" title="" >
            '.$title.'
        </a>
   <hr>
          </div>
';
    }
    return $rss_split;
  }
  function display($numrows,$head)
  {
    $rss_split = $this->parse();

    $i = 0;
    $rss_data = '<div>
           <div>
         '.$head.'
           </div>
         <div>';
    while ( $i < $numrows )
   {
      $rss_data .= $rss_split[$i];
      $i++;
    }
    $trim = <a href="http://www.php.net/str_replace">str_replace</a>('', '',$this->feed);
    $user = <a href="http://www.php.net/str_replace">str_replace</a>('&lang=en-us&format=rss_200','',$trim);
    $rss_data.='</div></div>';
    return $rss_data;
  }
}
?>
.vas{
    float:left;
    width:270px;
    padding:10px;
}
.title-head {
    font-size:18px;
    font-weight:bold;
    text-align:left;
    background-color:#006699;
    color:#FFFFFF;
    padding:5px;}
.feeds-links {
    text-align:left;
    padding:5px;
    border:1px solid #dedede;
 }

Basic Way 3

using SimpleXML

$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
print_r($doc);
if(isset($doc->channel))
{
    parseRSS($doc);
}
if(isset($doc->entry))
{
    parseAtom($doc);
}

function parseRSS($xml)
{
    echo "<strong>".$xml->channel->title."</strong>";
    $cnt = count($xml->channel->item);
    for($i=0; $i<$cnt; $i++)
    {
	$url 	= $xml->channel->item[$i]->link;
	$title 	= $xml->channel->item[$i]->title;
	$desc = $xml->channel->item[$i]->description;

	echo '<a href="'.$url.'">'.$title.'</a>'.$desc.'';
    }
}
function parseAtom($xml)
{
    echo "<strong>".$xml->author->name."</strong>";
    $cnt = count($xml->entry);
    for($i=0; $i<$cnt; $i++)
    {
	$urlAtt = $xml->entry->link[$i]->attributes();
	$url	= $urlAtt['href'];
	$title 	= $xml->entry->title;
	$desc	= strip_tags($xml->entry->content);

	echo '<a href="'.$url.'">'.$title.'</a>'.$desc.'';
    }
}

Basic Way 4

using Curl

$ch = curl_init("http://localhost/curl/rss.xml")
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
Category: PHP, Web  Tags: , , ,  Leave a Comment

Captcha PHP Script

About (in brief)
It does not require images but works on text. Now you can protect your pages with the use of Captcha, you can now have some additional security/authetication/nospam to your web pages.

Description
An easy to implement Captcha Script which does not require images but works on text. Now you can protect your pages with the use of Captcha, you can now have some additional security/authentication/nospam to your web pages. This script is based on the Question and Answer pattern. An easy to implement Captcha Script which does not require images but works on text. Now you can protect your pages with the use of Captcha, you can now have some additional security/authentication/nospam to your web pages. This script is based on the Question and Answer pattern.

Requirements

PHP & MySQL

Features

  • Provides enhanced security to your pages and prevents SPAM
  • Can be easily modified
  • Easily add or remove Questions and Answers from database table
  • Frame your own Questions and Answers
  • The answers can be in words or numerics (You can modify the settings)

Extraas

This script can be use in Login, Feedback, Comments/Review, Message Sending, Search, Forgot Password/Username Forms and many others.

Script Home | Demo | Download

Category: MySQL, PHP  Tags: , , ,  Leave a Comment

Breadcrumb PHP Script

About (in brief)
A nice script for MySQL users to show Breadcrumbs (i.e. to display current page location of a user) e.g. Home > Page 1 > Page 2 with links.

Description
A nice script for MySQL users to show Breadcrumbs (i.e. to display current page location of a user) e.g. Home > Page 1 > Page 2 with links on Home, Page 1 or link on only Home and not on Page 1 (because Page 1 doesn’t point to anywhere) or No links at all. A nice script for MySQL users to show Breadcrumbs (i.e. to display current page location of a user) e.g. Home > Page 1 > Page 2 with links on Home, Page 1 or link on only Home and not on Page 1 (because Page 1 doesn’t point to anywhere) or No links at all. A nice script for MySQL users to show Breadcrumbs (i.e. to display current page location of a user) e.g. Home > Page 1 > Page 2 with links on Home, Page 1 or link on only Home and not on Page 1 (because Page 1 doesn’t point to anywhere) or No links at all.

 

Requirements

PHP & MySQL

Features

  • Easy to implement script
  • Can be easily modified
  • Shows the breadcrumb on top

Script Home | Demo | Download

Block Words PHP Script

About (in brief)
Restrict certain words from being used at any place while registration, form filling and many other areas. It can be used at the places where you want to restrict the use of certain words like that of organisation related, admin related etc.

Description
Stop Vulgarity or Block Words is a PHP script which helps you to restrict certain words from being used at any place while registration, form filling and many other areas. It can be used at the places where you want to restrict the use of certain words like that of organisation related, admin related etc. This script uses the MySQL where the words are stored which are restricted to use. Also the words which contains these restricted words are blocked too and the homophones of the words are also blocked. Stop Vulgarity or Block Words is a PHP script which helps you to restrict certain words from being used at any place while registration, form filling and many other areas.
It can be used at the places where you want to restrict the use of certain words like that of organisation related, admin related etc.
This script uses the MySQL where the words are stored which are restricted to use. Also the words which contains these restricted words are blocked too and the homophones of the words are also blocked.

 

Requirements

PHP & MySQL

Features

  • Easily add and remove keywords
  • Block words in between words
  • Block homophone words too

Script Home | Demo | Download

Affiliate Referral PHP Script

About (in brief)
The user after login can send invitations his/her friends, only a limited number of invitations can be send daily by the user depending on the maximum number of invitations set by you (default is 10).

Description
User after login can send invitations to his/her friends, only a limited number of invitations can be send daily by the user depending on maximum number of invitations set by you (default is 10), you can change the default number and also it can vary for individual user. The user account has a Referral URL in his account which can be used for referring users. It is a 3 level referral system and user can view Referral Tree having users who comes in these 3 levels. Also user can view profile of these referrals. Only those profiles can be viewed which comes under 3 levels of user. Data for Profile Visits, Referral URL clicks and Invitation send are displayed in Total, Today and Last (date/time). User after login can send invitations to his/her friends, only a limited number of invitations can be send daily by the user depending on maximum number of invitations set by you (default is 10), you can change the default number and also it can vary for individual user. The user account has a Referral URL in his account which can be used for referring users. It is a 3 level referral system and user can view Referral Tree having users who comes in these 3 levels. Also user can view profile of these referrals. Only those profiles can be viewed which comes under 3 levels of user. Data for Profile Visits, Referral URL clicks and Invitation send are displayed in Total, Today and Last (date/time).

Requirements

PHP & MySQL

Features

  • 3 level Referral System
  • Points calculated level wise
  • Displays Profiles (only upto 3 levels)
  • Displays data related to Profile Visits, Referral URL clicks and Invitations

Extraas

This script can be used in referral tracking and affiliates programs.

 

Script Home | Demo | Download

KB Free PHP Scripts

KB PHP Scripts offers you easy to use and implement Free PHP Scripts for download!

Here, you can download various easy to use scripts. Also, you can rate and review these free scripts here. Though these scripts are Free but I may provide support for them, if I get some free time. Please feel free to write and suggest the features or any new script(s) you need. I may be able to design the script and put it here for Free Download.
For cool PHP and MySQL scripts visit: http://scripts.kunals.com
In this series find information about Free PHP MySQL scripts at scripts.kunals.com here.
Category: MySQL, PHP, Web  Tags: , ,  Leave a Comment
Get Adobe Flash player