Developers Archive for April, 2007

Cookies in PHP

Cookies in PHP Friday, April 27th, 2007

Cookies in PHP
—————-

The main use for cookies is to solve the problem of lack of status when browsing web pages.

With cookies, small portions of information are embedded in the browser, allowing the identification of cookies in several pages from the same site or

even during visits between several days.

Actually, cookies are nothing but text strings that are sent from the client server (web browser) and embedded in the server, and then the browser sends

these cookies to the server allowing the identification of the client in the server.

How to use Cookies
———————-

In PHP, Cookie is set by using the setcookie Statement,

Syntax :
——–

int setcookie (string Name [, string Value [, int Expire [, string Path [, string Domain [, int Secure]]]]])

Setcookie() defines a cookie that is sent along with the rest of the information from the header. Cookies shall be sent before any html tag;

therefore, we shall call one of these statements before any tag or . This is a restriction of cookies, not of PHP.
All messages, except name, are optional.

* Name. Name of the cookie. If we create a cookie only with its name, the cookie existing in the client under said name will be deleted. We

can also replace any argument with an empty string(”").

* Value. Value to be stored by the cookie in the client.

* Expire. The argument expire is an integer argument that indicates the time a cookie will be deleted in the time format returned by the UNIX statements time() and mktime(). Time() + N seconds of duration is generally used to specify the duration of the cookie.

* Path. Subdirectory where the cookie has a value.

* Domain. Domain where the cookie has a value. If we establish www.domain.com as domain, the cookie is not set for domain.com.

Meanwhile, if we establish domain.com as domain, the cookie is set as for domain.com as for www.domain.com.

* Secure. The message secure indicates that the cookie will only be set by a secure HTTPS connection.

Example:
———

setcookie(”username”,”abc”);

here, username is a name, the value is abc. Now, cookie is set.

It is retreiving by using $_COOKIE.

echo $_COOKIE[’username’];
It displays abc.

setcookie(”username”,”abc” time()+3600);

In this example, we set a user name cookie that has the value abc, But the value is only availabe for hour. that means the cookies is expired after 1 hour.

IP Retrival & hit counter with PHP/GD Library image editing

IP Retrival & hit counter with PHP/GD Library image editing Friday, April 27th, 2007

Synopsis:

This tutorial shows how to make a PHP image which generates IP Address of the PC viewing it and act as a HIT counter .

The Article:

This is a really simple free php hit counter script that uses the GD library to generate a picture that displays the users IP address and the amount of views the picture / page has had.This can be used as a hit counter for your siteor as a cool forum signature.Your host will need php and the GD library installed to use this Free Hit Counter.

1. First save a blank .txt file named counter.txt
2. then open notepad (or any PHP-editor u use) and paste this code :

&ltl?php

$TextFile = “counter.txt”;
$Count = trim(file_get_contents($TextFile));
$FP = fopen($TextFile, “r”);
$Count=fgets($FP, 4096);
fclose ($FP);
settype($Count, “integer”);
$Count++;
if ($FP = fopen ($TextFile, “w”)){
fwrite ($FP, $Count);
fclose ($FP);

}

$image = “counterpic.png”;
$im = imagecreatefrompng($image);
$red = ImageColorAllocate ($im, 255, 0, 0);
$blue = ImageColorAllocate ($im, 0, 0, 255);
$hit = “$Count”;
$ip = $_SERVER[”REMOTE_ADDR”];

ImageString($im, 2, 18, 1, “www.yoursitename.com”, $blue);
ImageString($im, 2, 1, 19, ” Your ip: $ip”, $red);
ImageString($im, 2, 1, 30, ” Page visited $hit times “, $red);
header(”Content-Type: image/png”);
Imagepng($im,'’,100);
ImageDestroy ($im);

?>

3. Save it as index.php or anything u like just with .php extention

4.Now save the 3 files index.php , counter.txt & a background image some thing like a blue rectangle in the name counterpic.png in a folder and upload it through FTP to any server supporting php

sample image counterpic.png

5. Grant counter.txt CMOD 777 ( right click in FTP > properties > all previlages)

6. Then call index.php in your browser . that`s it.

TIP:

1. You can link to the index.php with the normal image tags.

2. If you upload these files to a folder called pic.gif you will be able to use it on forums by using the url yoursite.com/pic.gif
and it will run the index.php and output the image, Useful if the site or forum don’t allow php images or html code.

3. You can enter any no. in counter.txt the image will show that much hits !!!! LOL

4. This is the final image

Template Design pattern

Template Design pattern Wednesday, April 25th, 2007

In this pattern basic algorithm is defined in abstract class. Then the subclass can override the functions to change the behaviors.

Example:
We take human as an example.Here Human is abstract class.
The human can classified as with the type of work that human doing.Example as Doctor, Engineer and etc.

abstract class Human {
public final function walk() {
echo “walking”;
}
public final function eat() {
echo “eating”;
}

abstract void work(){
}

}

Doctor class

class Doctor extends Human {
public void work(){
echo “some work1″;
}
}

class Doctor extends Engineer {
public void work(){
echo “some work1″;
}
}

So in the above example abstract class contains the basic functions human can do.Based on our classifications we can override the function which are defined in the abstract class.

Usage
1. Avoiding duplication of coding
2. can control the sub class


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.