Skip to main content
Home
Blog

Main navigation

  • Home
User account menu
  • Log in

Breadcrumb

  1. Home

Drupal 7 Block, very flexible!

By ongetc , 12 February, 2017

Before Drupal 7, if you want to create a block for your Drupal site you either have to create a module or a variable to show your block and it is not fun!

But in Drupal 7, Block is part of the core, just to go ?q=admin/structure/block and create a block.

In the block body, you can enter full HTML or PHP code and this is where the flexibility come in.

With full HTML code you can enter any ads including Javascript into your block and assign it anywhere on your site and it shows.

With PHP code, you can write a small snippet of PHP code and run on your site like for example this code below will show your visitor IP.


< ?php
if (user_access('access content')) {
$spiders=array('google', 'fatlens', 'yahoo', 'altavista', 'yandex', 'baidu', 'bing', 'thefind', 'bot', 'spider', 'crawl');
$interval = REQUEST_TIME - variable_get('user_block_seconds_online', 900);
$authenticated_count = db_query("SELECT COUNT(DISTINCT s.uid) FROM {sessions} s WHERE s.timestamp >= :timestamp AND s.uid > 0", array(':timestamp' => $interval))->fetchField();
$output="";
$max_users = variable_get('user_block_max_list_count', 10);
if ($authenticated_count && $max_users) {
$output.="Registered Users";
$items = db_query_range('SELECT u.uid, u.name, MAX(s.timestamp) AS max_timestamp FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.timestamp >= :interval AND s.uid > 0 GROUP BY u.uid, u.name ORDER BY max_timestamp DESC', 0, $max_users, array(':interval' => $interval))->fetchAll();
$output .= theme('user_list', array('users' => $items));
}
$guest_count = db_query("SELECT COUNT(DISTINCT s.hostname) FROM {sessions} s WHERE s.timestamp >= :timestamp", array(':timestamp' => $interval))->fetchField();
$sData=array(); $gData=array();
if ($guest_count && $max_users) {
$items = db_query_range('SELECT DISTINCT s.hostname, MAX(s.timestamp) AS max_timestamp FROM {sessions} s WHERE s.timestamp >= :interval ORDER BY max_timestamp DESC', 0, 10000000, array(':interval' => $interval))->fetchAll();
$i=0;
for ($i=0;$ihostname;
}
$ips=array_unique($ips);
foreach ($ips as $ip){
$host=preg_replace('/[^a-zA-Z\.]/', '', gethostbyaddr($ip));
if ($host=="...") $host="Unknown Hostname";
$spider=0;
foreach ($spiders as $spider){
if (strpos($host, $spider)!==false){
$spider=1;
break;
}
}
if ($spider==1){
$sData[]=array('ip'=>$ip, 'host'=>$host);
}else{
$gData[]=array('ip'=>$ip, 'host'=>$host);
}
}
}
if (isset($gData) && count($gData)>0){
$output.="Guests";
$i=0;
foreach ($gData as $d){
$output.="".$d['host']."";
$output.="

";
$output.="
".$d['ip']."
";
$loc=unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$d['ip']));
if ($loc['geoplugin_city'].$loc['geoplugin_region']!=''){
$output.="

".$loc['geoplugin_city'].", ".$loc['geoplugin_region']."
";
}
if ($loc['geoplugin_countryName']!=''){
$output.="

".$loc['geoplugin_countryName']."
";
}
$output.="

";
if ($i>$max_users){break;}
$i++;
}
}
if (isset($sData) && count($sData)>0){
$output.="Spiders";
$i=0;
foreach ($sData as $d){
$output.="".$d['host']."";
$output.="

";
$output.="
".$d['ip']."
";
$output.="

";
if ($i>$max_users){break;}
$i++;
}
}
return $output;
}
? >

It sure save me lot of time in compare to when I was using Drupal 6

Reseller hosting (free)

Hosting24 (w/ ssh)

A2 Hosting

Monthly archive

  • September 2009 (14)
  • October 2009 (11)
  • January 2010 (2)
  • June 2010 (3)
  • October 2011 (1)
  • March 2012 (1)
  • July 2012 (1)
  • February 2017 (564)
  • March 2017 (5)
  • April 2017 (2)

Pagination

  • 1
  • Next page

Popular content

Last viewed:

  • MosModule: Can MosModule be used in custom HTML module?
  • Dotclear
  • CFWheels
  • MosModule
  • TheHostingTool
  • Wheatblog
  • CodeIgniter
  • Bitweaver
  • MosModule FAQ
  • Why I got "Your domain is not pointing to our name servers at the moment.."?

User login

  • Reset your password