Archive for 'September, 2008'

Show random image(s) from a directory

Posted on September 21, 2008, Filled under PHP,  Bookmark it

Hi,

If you need to show images randomly from a directory then this script can help you.

index.php

Let’s configure some variables: $extensions, $images_folder_path & $url_to_images_folder.

<?php
// extensions to be checked

$extensions = array('jpg','jpeg','gif','png','bmp');

// images folder

$images_folder_path = '/home/site/public_html/images/';

// url to folder

$url_to_folder = 'http://www.yoursitename.com/images/';

Read more from this entry…

Hi,

Here’s a script which randomly selects a value from an array:

<?php
function selectRandomValueFromArray($array, $range = '')
{
srand((float) microtime() * 10000000);

if(empty($range))
{
$last_key = count($array) - 1;
$range = array(0, $last_key);
}

$rand_array = rand($range[0], $range[1]);

$result = $array[$rand_array];

return $result;
}

$array = array('apple', 'pine', 'pear', 'strawberry', 'cherries', 'mango', 'grape', 'watermelon', 'papaya');

/*
1st argument: array
2nd argument (optional): the array range (2 values) of the keys
*/

echo selectRandomValueFromArray($array); // select random value from all

echo "<br />";

echo selectRandomValueFromArray($array, array(0, 3)); // select random value from  the first 4 values of the array

echo "<br />";

echo selectRandomValueFromArray($array, array(2, 5)); // select random value between the 3rd and the 6th value of the array
?>

NOTE The array starts from 0. If you need, for instance, to set a range from the 2nd to the 5th value of the array then you should use the following argument: array(1, 4).

The advantage of this function, compared to array_rand() is that it can randomly select values from a specified range.

Good luck!

Did you ever want to hide the path where your downloadable files are located? If so, then you came in the right place. In this tutorial we will learn how to create download links which do not disclose the actual location of the files.

We will begin creating the configuration file. First, you will decide whether the file downloaded will have the original or the alias name.

config.php

<?php
/*
Show alias name or the original name to the user?
TRUE = original name, FALSE = alias
*/

define("SHOW_ORIGINAL_FILE_NAME", true);

Then, we will build the array that will contain the downloadable files. The key of the array is used as an ID to download the real file.

/*
This is the list with the files.
'alias' => 'realfile'

Example: www.yoursite.com/download.php?file=images_archive

Make sure that each key is unique
*/
$aliases = array('images_archive'  => 'images_archiver_08-19-2008.zip',
                              'document'  => 'document.txt',
		                     'logo'   => 'bit-logo.jpg');

Read more from this entry…

PHP: Creating a simple web data (spider) extractor

Posted on September 14, 2008, Filled under PHP,  Bookmark it

In this tutorial we will learn how to create a simple web spider that will extract specific information from a web page. Our script will have 2 files: index.php & functions.php. In our sample, the extractor will check how many pages from a site are indexed by Google.

First, we will create the library file which will have 2 functions: one to fetch the content from our pages and the other one to extract content between two strings (delimiters).

functions.php

<?php
function LoadCURLPage($url, $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4)
 Gecko/20030624 Netscape/7.1 (ax)",
$cookie = '', $referer = '', $post_fields = '', $return_transfer = 1,
$follow_location = 1, $ssl = '', $curlopt_header = 0)
{
$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, $url);

if($ssl)
{
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
}

curl_setopt ($ch, CURLOPT_HEADER, $curlopt_header);

if($agent)
{
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
}

if($post_fields)
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
}

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

if($referer)
{
curl_setopt($ch, CURLOPT_REFERER, $referer);
}

if($cookie)
{
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
}

$result = curl_exec ($ch);

curl_close ($ch);

return $result;
}

function extract_unit($string, $start, $end)
{
$pos = stripos($string, $start);

$str = substr($string, $pos);

$str_two = substr($str, strlen($start));

$second_pos = stripos($str_two, $end);

$str_three = substr($str_two, 0, $second_pos);

$unit = trim($str_three); // remove whitespaces

return $unit;
}
?>

Let’s continue creating the index.php file. We will start by including the functions file & setting up some configuration variables:

index.php

<?php
error_reporting (E_ALL ^ E_NOTICE);

include 'functions.php';

// Site to check
$site = 'www.microsoft.com';

// Connect to this url using CURL
$url = 'http://www.google.com/search?hl=en&q=site%3A'.$site.'&btnG=Search';

Let’s use cURL to connect to the $url.

$data = LoadCURLPage($url);

Now $data contains the html output for $url;

We will use extract_unit() to get the information between 2 strings. In our case the total indexed pages for $site is between ‘<em></b> of about <b></em>‘ and ‘<em></b></em>‘.

Results <b>1</b> – <b>10</b> of about <b>678,000</b> from <b>www.microsoft.com</b>. (<b>0.04</b> seconds)
// Extract information between STRING 1 & STRING 2

$string_one = '</b> of about <b>';
$string_two = '</b>';

$info = extract_unit($data, $string_one, $string_two);

Output our result:

echo 'Google has indexed '.$info.' pages for '.$site.'.';
?>

The script will output something like this:

Google has indexed 678,000 pages for www.microsoft.com.

A tableless 3 (fluid) columns layout

Posted on September 13, 2008, Filled under HTML & CSS,  Bookmark it

This is a simple tableless layout that uses DIVs having 3 (fluid) columns, a header & a footer.

layout-style.css – CSS File

BODY
{
margin: 0px;
padding: 0px;
background-color: white;
}

#main_wrapper
{
width: 900px;
height: auto;
}

#top
{
height: 80px;
background-color: #FFFFDF;
}

#left
{
float: left;
width: 200px;
background-color: #BED3C4;
}

#content
{
float: left;
width: 500px;
background-color: #E0F0FB;
}

#right
{
float: left;
width: 200px;
background-color: #f2faf2;
}

#bottom
{
width: 900px;
height: auto;
background-color: #F9E8F8;
}

.clear
{
clear: both;
}

layout.html – The actual page

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE>3 Columns Layout</TITLE>
  <META name="Author" Content="Bit Repository">

  <META name="Keywords" Content="columns, css, layout">
  <META name="Description" Content="A 3 columns layout">

<LINK REL="stylesheet" HREF="layout-style.css" TYPE="text/css">

</HEAD>

 <BODY>

<center>

<div id="main_wrapper">
<div id="top">Welcome to my WebSite<br /><br />my header text here my header text here my header text here my header text here.</div>

<div style="width: 900px;">

<div id="left">my text here my text here my text here my text here my text
here my text here my text here my text here my text here. </div>

<div align="center" id="content">my text here my text here my text here my text here my text here
my text here my text here my text here my text here my text here my text here my text
here my text here my text here my text here</div>

<div id="right">my text here my text here my text here my text here my text here
my text here my text here my text here my text here my text here my text here my text
here my text here my text here my text here</div>

</div>

<div class="clear"></div>

<div id="bottom">Copyright &copy; MySite.com<br />My footer text here My footer text here
My footer text here My footer text here My footer text here My footer text here
My footer text here My footer text here My footer text here </div>

</div>

</center>

 </BODY>
</HTML>

Our sample output will look like this:

Equivalent of PHP’s in_array() function

Posted on September 13, 2008, Filled under JavaScript,  Bookmark it

Hello coders,

This is a JavaScript function that works like in_array() in PHP. Below is the function with a usage example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE>Equivalent of PHP in_array() | JavaScript Library</TITLE>
  <META NAME="Author" CONTENT="Bit Repository">

  <META NAME="Keywords" CONTENT="in_array, php, javascript">
  <META NAME="Description" CONTENT="Equivalent of PHP in_array() | JavaScript Library">

<SCRIPT LANGUAGE="JavaScript">

function in_array(string, array)
{
   for (i = 0; i < array.length; i++)
   {
      if(array[i] == string)
      {
         return true;
      }
   }
return false;
}

var extensions = new Array("jpg","jpeg","gif","png","bmp");

/*
// Alternative way of creating the array

var extensions = new Array();

extensions[1] = "jpg";
extensions[0] = "jpeg";
extensions[2] = "gif";
extensions[3] = "png";
extensions[4] = "bmp";
*/

var str_to_check = "bmp";

if(in_array(str_to_check, extensions))
{
  alert(str_to_check +" is in our array.");
}
else
{
  alert(str_to_check +" is not in our array.");
}

</SCRIPT>

</HEAD>
<BODY>
</BODY>
</HTML>

If you have any comments or suggestions regarding this script please post them.

Advanced IP Ban PHP Script

Posted on September 12, 2008, Filled under PHP,  Bookmark it

Hi,

This script is useful if you want to restrict the access to some people on your site, based on their IPs. There are 2 lists which can be used: one which has a list of the IPs you want to ban and one which you can use to ban a range of IPs. The script checks both lists and if the Visitor’s IP is in the list then the script will show a message to the user and exit.

<?php
/* List with IPs */
$ban_ip_list = array('68.180.206.184', '64.233.167.99', '207.46.232.182');

/* List with IP ranges. Use the '*' as the range selector */
$ban_ip_range = array('69.*.83.197');

/* Visitor's IP Address */
$user_ip = $_SERVER['REMOTE_ADDR'];

/* Message to output if the IP is in the ban list */
$msg = 'You do not have permission to access this page.';

/* Message to output if the IP is in the ban list */

    if(in_array($user_ip, $ban_ip_list))
	{
	  exit($msg);
	}

/* Check if the Visitor's IP is in our range's list */

if(!empty($ban_ip_range))
{
foreach($ban_ip_range as $range)
{
	$range = str_replace('*','(.*)', $range);

    if(preg_match('/'.$range.'/', $user_ip))
	{
	  exit($msg);
	}
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE>Welcome to My site</TITLE>
 </HEAD>
 <BODY>

  Here is my site content.
 </BODY>
</HTML>

PHP: How to calculate the size of a file

Posted on September 11, 2008, Filled under PHP,  Bookmark it

This function calculates the size of a file in bytes, kilobytes, megabytes and gigabytes.

<?php
function get_file_size($filename, $size_in = 'MB')
{
$size_in_bytes = filesize($filename);

// Precision: decimals at the end for each type of size

if($size_in == 'B')
{
$size = $size_in_bytes;
$precision = 0;
}
elseif($size_in == 'KB')
{
$size = (($size_in_bytes / 1024));
$precision = 2;
}
elseif($size_in == 'MB')
{
$size = (($size_in_bytes / 1024) / 1024);
$precision = 2;
}
elseif($size_in == 'GB')
{
$size = (($size_in_bytes / 1024) / 1024) / 1024;
$precision = 2;
}

$size = round($size, $precision);

	return $size.' '.$size_in;
}

$file = '/path/to/public_html/folder/my_file.zip';

# 1st argument: path to file
# 2st argument: calculate in: B (Bytes), KB (Kilobytes), MB (Megabytes), GB (Gigabytes)

echo get_file_size($file, 'MB');
?>

A tableless 2 (fluid) columns layout

Posted on September 10, 2008, Filled under HTML & CSS,  Bookmark it

This is a simple tableless layout that uses DIVs having 2 (fluid) columns, a header & a footer.

layout-style.css – CSS File

body
{
margin: 0px;
padding: 0px;
background-color: white;
}

#main_wrapper
{
width: 900px;
height: auto;
}

#top
{
height: 80px;
background-color: #FFFFDF;
margin-bottom: 10px;
}

#left
{
float: left;
width: 200px;
background-color: #BED3C4;
}

#content
{
float: right;
width: 690px;
background-color: #E0F0FB;
}

#bottom
{
margin-top: 10px;
width: 900px;
height: 30px;
background-color: #F9E8F8;
}

.clear
{
clear: both;
}

layout.html – The actual page

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE>2 Columns Layout</TITLE>
  <META name="Author" Content="Bit Repository">

  <META name="Keywords" Content="columns, css, layout">
  <META name="Description" Content="A 2 columns layout">

<LINK REL="stylesheet" HREF="layout-style.css" TYPE="text/css">

</HEAD>

 <BODY>

<center>

<div id="main_wrapper">
<div id="top">Welcome to my WebSite<br /><br />my header text here my header text here
my header text here my header text here.</div>

<div id="content_wrapper">

<div id="left">my text here my text here my text here my text here my text
here my text here my text here my text here my text here. </div>

<div id="content">my text here my text here my text here my text here my text here
my text here my text here my text here my text here my text here my text here my text
here my text here my text here my text here</div>

</div>

<div class="clear"></div>

<div id="bottom">Copyright &copy; MySite.com<br />My footer text here My footer text here
My footer text here My footer text here My footer text here My footer text here
My footer text here My footer text here My footer text here </div>

</div>

</center>

 </BODY>
</HTML>

Our sample output will look like this: