Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Tuesday, March 24, 2009

Block all the vistors from one country

I am using PHP, so I just talk someting about Apache. Hope it is something you are searching for.

Due to some reasons, some webmasters may want to block the traffic from one country. It's very easy to do it with the .htaccess file of the Apache. The following is the basic grammar.
order allow,deny
allow from all

# specific IP address
deny from 123.123.123.123
# deny a range: 123.123.0.0 - 123.123.255.255
deny from 123.123.0.0/16
# the whole class: 123.0.0.0 - 123.255.255.255
deny from 123.0.0.0/8
# shorthand for previous
deny from 123

Like the above, just change the IP range to the speific range and add to your .htaccess file. Then you can block a country. The most complex thing is to get the up-to-date IP range of the specific country. I just got a result from Google that who provides the IP range of all countries for free. Please check the following link.

http://www.blockacountry.com/

So, now everything is solved.

If you want to know more about the .htaccess file. Please check .htaccess tips and tricks.

Sunday, August 12, 2007

How to paste good formatted php source code on blogger?

Several days ago, I just wanted to paste some php codes on this blog. But the codes I pasted was ugly. Then I decided to write something to format the looking. It is finished today.

The formatted codes:

<?php

//creates a unique id with the 'about' prefix
$a uniqid(about);
echo 
$a;
echo 
"<br>";

//creates a longer unique id with the 'about' prefix
$b uniqid (abouttrue);
echo 
$b;
echo 
"<br>";

//creates a unique ID with a random number as a prefix - more secure than a static prefix 
$c uniqid (rand(), true);
echo 
$c;
echo 
"<br>";

//this md5 encrypts the username from above, so its ready to be stored in your database
$md5c md5($c);
echo 
$md5c;
echo 
"<br>";

?>


Two steps to paste your nice looking php code on your blog.

1. Visit http://phpgoogle.awardspace.com.

2. Submit your code.

3. Copy and paste the HTML source code to your blog.

Yeah, it's very easy.

And the mechanism is very easy too. I just use PHP's Built-in Source Highlighter - highlight_string(). You can read more about it on clicking the link.

I make the online demo to make the thing easy and convenient. Hope it will be useful for you.

Saturday, August 04, 2007

Four ways to generate unique id by PHP

1. Using uniqid() function

<?php

//creates a unique id with the 'about' prefix
$a uniqid(about);
echo 
$a;
echo 
"<br>";

//creates a longer unique id with the 'about' prefix
$b uniqid (abouttrue);
echo 
$b;
echo 
"<br>";

//creates a unique ID with a random number as a prefix - more secure than a static prefix 
$c uniqid (rand(), true);
echo 
$c;
echo 
"<br>";

//this md5 encrypts the username from above, so its ready to be stored in your database
$md5c md5($c);
echo 
$md5c;
echo 
"<br>";

?>

2. Using current time + IP style

<?php

//You can also use $stamp = strtotime ("now"); But I think date("Ymdhis") is easier to understand.
$stamp date("Ymdhis");
$ip $_SERVER['REMOTE_ADDR'];
$orderid "$stamp-$ip";
$orderid str_replace(".""""$orderid");
echo(
$orderid);
echo 
"<br>";

?>

3. Generate custom length unique id

<?php

//set the random id length 
$random_id_length 10

//generate a random id encrypt it and store it in $rnd_id 
$rnd_id crypt(uniqid(rand(),1)); 

//to remove any slashes that might have come 
$rnd_id strip_tags(stripslashes($rnd_id)); 

//Removing any . or / and reversing the string 
$rnd_id str_replace(".","",$rnd_id); 
$rnd_id strrev(str_replace("/","",$rnd_id)); 

//finally I take the first 10 characters from the $rnd_id 
$rnd_id substr($rnd_id,0,$random_id_length); 

echo 
"Random Id: $rnd_id" ;
echo 
"<br>";

?>

4. Generate XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX style unique id, (8 letters)-(4 letters)-(4 letters)-(4 letters)-(12 letters)

<?php

// Generate Guid 
function NewGuid() { 
    
$s strtoupper(md5(uniqid(rand(),true))); 
    
$guidText 
        
substr($s,0,8) . '-' 
        
substr($s,8,4) . '-' 
        
substr($s,12,4). '-' 
        
substr($s,16,4). '-' 
        
substr($s,20); 
    return 
$guidText;
}
// End Generate Guid 

$Guid NewGuid();
echo 
$Guid;
echo 
"<br>";

?>