Archive for the ‘php’ Category

Simple use of cookies with php

Thursday, July 26th, 2007

With php you can set a “cookie” in a users session, which stores a small amount of data on the users computer.

Cookies can be useful to check if the user recently logged in to your webpage,
recently visited your webpage, or such.

With the use of cookies are pretty simple…

Heres how you create the cookie:

  1. <php
  2. setcookie( "user", $_SERVER[‘REMOTE_ADDR’], time()+3600 );
  3. ?>

(more…)

How to create and store a thumbnail in php

Thursday, July 19th, 2007

Here I will show you guys how to create a thumbnail from a larger image file ( .jpg file)

You create a new .php file, and enter this function:

  1. <?php
  2.  
  3. function thumbnail($file, $size, $save_as) {
  4.  list($width, $height) = getimagesize($file) ;
  5.  $modwidth = $width * $size;
  6.  $modheight = $height * $size;
  7.  $tn = imagecreatetruecolor($modwidth, $modheight) ;
  8.  $image = imagecreatefromjpeg($file) ;
  9.  imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
  10.  imagejpeg($tn, $save_as, 100) ;
  11.  }
  12.  
  13. ?>

then you can call the function like this:

  1. <?php
  2.  
  3. thumbnail(‘original_image.jpg’, ‘0.60′, ‘new_image.jpg’);
  4.  
  5. ?>

The last part calls the function, and tells it to create a thumbnail from the “original_image.jpg”, with 0.6 the size of the original, and store the new image as “new_image.jpg” in the same folder as the script.

Some of the code in the function is explained in this post.

Simple :)

Simple php visitor counter

Wednesday, July 18th, 2007

This shows a simple way to log number of visitors to your site, there is no need of any database, it uses flat files (plain textfiles) only.

Create an empty file named stats.txt.

Create a php file, call it counter.php or something, and put it in the same directory as the stats.txt file.

Open it in notepad and enter this php code

  1. <?php
  2. $counter_file = ("stats.txt");
  3. $visits = file($counter_file);
  4. $visits[0]++;
  5. $fp = fopen($counter_file , "w");
  6. fputs($fp , "$visits[0]");
  7. fclose($fp);
  8. echo "Visitors: $visits[0]";
  9. ?>

What the code does:

  1. Define the textfile with stats
  2. Read the stats from the file
  3. increase the stats with 1
  4. open the stats file in writing mode
  5. write to the file
  6. close the file
  7. and finally, print out the content

Simple use of CAPTHCA on your website with php

Monday, July 16th, 2007

 First, what is CAPTCHA?

Captcha is a “Completely Automated Public Turing test to tell Computers and Humans Apart”.

In these days, Im sure you have already seen one or many versions of captcha on the internet. In forms for registering, uploading, searching or posting comments.

Captcha is used to prevent robots spamming your website with ads or such,
and to stop ”mass registering” on websites, by non-human users.

There are different methods to tell if the user is human or a bot. (more…)

Blacklist IPs from viewing your webpage

Sunday, July 15th, 2007

This is a simple small piece of code that allows you to ban visitors fro viewing your webpage, if you know their ip adresses…

This can be made much more advanced.

With the use of a database such as Mysq (or in some cases just a textfile),  you can create a management system for adding, editing and deleting blacklisted ips.

  1. <?PHP
  2. $ip = $_SERVER[‘REMOTE_ADDR’];
  3. $ban = array(
  4. "74.89.41.93",
  5. "123.40.1.63",
  6. );
  7.  
  8. if(in_array($ip, $ban)){
  9. die("Sorry, you are banned!");
  10. }
  11. ?>

(more…)