Archive for the ‘PHP’ Category

Bad image cache in Internet Explorer

Wednesday, September 5th, 2007

Sometimes we need to upload/replace an image with the new one, using the same filename. Oftenly, our browser will display the same image as before we uploaded new image because they’re using the same name and browser just display image from cache. This could be nightmare!

I’ve tried some ways to prevent this bad caching done by the browser. Some only works for some browser and other don’t, mostly in Internet Explorer. Yeah, this browser has bad reputation with caching and I need a way to handle this problem.

Then I came into a conclusion that browser using filename to decide if it need to re-download the file or not. So I need to make a unique filename to call the same image, and it can be done using query string like filename.jpg?foo=bar or just filename.jpg?foo

To make the query string changed all the time, I use time() function in PHP. So to call an image, I will write:
User's avatar
and will changed time to time.

Amazingly this little trick solve all my caching problem!

Extract link from specific page/URL

Monday, August 27th, 2007

This is a simple function to extract link from a specific page/URL : function extract_url($main_url){

$cek_url = parse_url($main_url);
$prefix_url = $cek_url['scheme'].'://'.$cek_url['host'];

$f = fopen($main_url,"r");
$inputStream = fread($f,65535);
fclose($f);
if (preg_match_all("/(.*?)<\/a>/i”,$inputStream,$matches)) {
foreach($matches[1] as $link){
if(!eregi(’mailto:|javascript:|ymsgr:’,$link)){
if(eregi(”http://”,$link)){
$url = $link;
}
else{
$url = $prefix_url.$link;
}
if(eregi(’PHPSESSID’,$url)){
$url = explode(”PHPSESSID”,$url);
$url = substr($url[0],0,-1);
}
$output[] = $url;
}
}
}
return array_unique($output);
}
?>

And here is sample how to use the function: $start = time();
print_r(extract_url("http://dev.sandalian.com/"));
$end = time();

echo 'done in '.($end-$start).' second';
?>

This script only working when remote_url are allowed. Otherwise you will need to use CURL.

Increasing File Upload Limit Using .htaccess

Tuesday, February 6th, 2007

As PHP limits maximal file upload into 2 (two) megabytes, sometimes we need to increase the value to upload bigger files.

Editing php.ini is the easiest way, but it is impossible when our scripts are in a shared hosting. The only few choice is by using .htaccess file. It will rewrite php.ini’s configuration.

I’ve tried this method and worked as I expected: Here’s the .htaccess content:

RewriteEngine On
php_value upload_max_filesize 30M

Last words, there’s manything we can do using .htaccess to overide server’s default configuration ^_*

&nbsp;

Sample of search engine for multiple tables

Wednesday, January 24th, 2007

Searching a string within a table is quite easy, but searching within multiple tables in a database need a little trick. I know it’s not the good one, perhaps everybody who read this can share yours too ;)

Example: We have 3 tables with different content and structures for 3 different pages (News, Articles, Products)


$string = $_POST['what'];

# 1. Search for News and store it into array
$search_news = mysql_query("SELECT * FROM table_news WHERE news_title LIKE '%$string%' OR news_content LIKE '%$string%' ");
while($result=mysql_fetch_array($search_news)){
$array_results[] = array($result['news_title'], "news.php?id=".$result['news_id']);
}

# 2. Search for Articles and store it into array
$search_articles = mysql_query("SELECT * FROM table_articles WHERE articles_title LIKE '%$string%' OR articles_content LIKE '%$string%' ");
while($result=mysql_fetch_array($search_articles)){
$array_results[] = array($result['articles_title'], "articles.php?id=".$result['articles_id']);
}

# 3. Search for products and store it into array
$search_products = mysql_query("SELECT * FROM table_products WHERE products_name LIKE '%$string%' OR products_content LIKE '%$string%' ");
while($result=mysql_fetch_array($search_products)){
$array_results[] = array($result['products_name'], "products.php?id=".$result['products_id']);
}

// all result now is in the array $array_results. index [0] is for Title, and index [1] is for the URL
echo "

    “;
    foreach($array_results as $search_result){
    echo “
  1. $search_result[0]
  2. “;
    }
    echo “

“;
?>

Shorten Code for Inserting Random Invisible Character

Wednesday, December 13th, 2006

This post is related correction to my previous one, inserting invisible random character inbetween words to avoid copy-paste doer.

I wrote a function to insert invisible random character inbetween words, and it used looped str_replace() to seek for the whitespaces. And I realize it’s not good for a very long texts, because the process will slowed down. So here is a new function with call back method, hopefully it will be faster and cooler ^_*

// main function, will call the replacews() function
function ghostText($data){
return preg_replace_callback('{((\s)+?)}i', "replacews", $data);
}
function replacews($xxx){
$c = chr(mt_rand(62,155));
return "$c“;
}
?>

And the usage is still quite simple:
$data = "Quick brown fox jumps over the lazy dog!";
echo ghostText($data);
?>

Many thanks to Aryo for simplifying the function ^_*

&nbsp;

Invisible text using PHP

Thursday, December 7th, 2006

This time, let’s try to insert single character with same color as the background inbetween words. This method will avoid visitor to copy paste text from your homepage because they will get a buch of un-understood words.

This is cool (in my own opinion), I insert/replace any white space with single character, and set the color property of ghost class into the same color as the background.

So, visitor will not see the invisible text (yeah, it’s invisible). But when they try to block the text and do a copy-paste operation, all text will be all within.

First, set ghost class color in your css file or simply put inside STYLE tag as follow:

And here’s the function: function ghostText($txt){
// set the ASCII characters from 65-122
$ascii_min = 65;
$ascii_max = 122;

// let's do the job!
$txts = explode(" ",$txt);
foreach($txts as $teks){
$hasil .= $teks."“.chr(mt_rand($ascii_min,$ascii_max)).”“;
}
return ($hasil);
}
?>

You can see the sample here: Sample invisible text using PHP.

It only avoid copy paste. By grabbing the HTML and using strip_tags() function, your text will be easily stolen.

 

Update #1

I got some complains saying that this function is out of web standard and not good for people with disability and use screen reader. And I said yes, this function is not suit on web standard. This only for normal users and to avoid your text being copy-paste’ed by lamers.

So if you’re concern on web standard, please do not use this script/function.

Update #2

I’ve changed the code so it will be faster than this one. Please read faster inserting random invisible character inbetween words. ^_*

 

Create Thumbnail on The Fly Using PHP and GD

Saturday, December 2nd, 2006

Here’s a cool script I use to resize picture on the fly. I don’t wanna loose any space in my server, so instead of creating a real thumbnail file, I generate a thumbnail on the fly.

First, specify the HTTP header.
header("Content-type: image/jpeg");

Then, this is the formula to resize the image with aspect ratio. We will only need to specify new thumbnail’s width. And its height will be automatically set.
// get image size
$file = $_GET[image];
if($size = GetImageSize($file)){
$w = $size[0];
$h = $size[1];

//set new size
$nw = $_GET['width'];
$nh = ($nw*$h)/$w;
}
else{
//set new size
$nw = "0";
$nh = "0";
}
?>

Now let’s draw the image:
//draw image
$src_img = imagecreatefromjpeg($file);
$dst_img = imagecreatetruecolor($nw,$nh);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h);
imagejpeg($dst_img,"",100);
imagedestroy($src_img);
imagedestroy($dst_img);
?>

Save above pieces code into a file, name it thumbnail.php, for example. Then, call it with common &lt;IMG SRC=”thumbnail.php?image=img/picture.jpg&width=150″ /&gt;

Your image should be succesfully displayed. If you have any question, please don’t hesitate to contact me yeni.setiawan AT yahoo DOT com ^_*

&nbsp;

Kilabret - The Reverse Text generator

Tuesday, November 21st, 2006

Started from strange topic in diskusiweb.com, I write down this tiny script to reverse and un-reverse the text written there.

If you type kamen rider, it will return nemak redir. Sound cool right?


// save it as reverse.php
foreach($argv as $arg){
if($arg == $argv[0]) continue;
echo strrev($arg)." ";
}
?>

Now let’s give it a try:

C:\php4>php reverse.php reverse this words with thy power!!!!
Content-type: text/html
X-Powered-By: PHP/4.3.11

esrever siht sdrow htiw yht !!!!rewop

Hi..hi.. ridiculous?

PHP-CLI Introduction-Part-One

Tuesday, November 14th, 2006

PHP CLI? What kinda food is that? Don’t get mad with any “PHP variation” because PHP-CLI is a very simple thing.

CLI stands from Command Line Interface. That means PHP that run from command line. You can mention MS DOS in Windows or console/shell in Linux platform. No need web server anymore because we aren’t going to make a web. We will use it to create desktop applications.

First step, identify your PHP installation. Where did you install PHP on your machine? Linux user just need to write:

$ whereis php

and system will tell you where is PHP living in your machine.

Second step, let’s create the world’s famous programmer’s phrase:


// name it test.php
echo "Hello World!";
?>


and save it at the same directory as your PHP installation. It’s not a must, just to ease our practise.

Then, the moment we are waiting for. Executing the script!

C:\php4>php.exe test.php [enter]
Hello World!

of course we do the same in linux:

$ php test.php [enter]
Hello World!

Our third step is reading user’s input. We won’t create a selfish application, right? There will be two variables passed by the command line: $argv and $argc.

$argv is an array containing all parameters passed on the command line with space as the delimiters. While $argc is the number of total $argv array.


// name it test2.php
print_r($argv);
?>

Let’s see what will appear in the next post ^_*

Silly Me: MD5 Feature on PHPMyAdmin

Wednesday, November 8th, 2006

Today, I lost forget a password that I stored in my local database (MySQL). The password was hashed using MD5 so I can’t read it using PHPMyAdmin. So, I decided to create new hash using MD5 too. I use this script to do same thing since few months ago:

// usage: php.exe md5.php password
echo md5($argv[1]);
?>

c:\php4>php.exe md5 p455w0rd
Content-type: text/html

a4fd8e6fa9fbf9a6f2c99e7b70aa9ef2

My friend saw me doing this and laughing for a while. Then he said that PHPMyAdmin has a tool to auto generate md5 password:
MD5 hash using PHPMyAdmin

What a DH&AYD&*A ^o^

&nbsp;