Developers Archive for the 'php programming' Category

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

Creating a Multi-File Upload Script in PHP

Creating a Multi-File Upload Script in PHP Wednesday, April 25th, 2007

From this article, we can learn how to upload more than one file at a time.

Step 1 : Basic form to get no.of Upload file required (uploadform1.php)<html>
<head>
<title>No.of Files to Upload</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
</head>

<html><head><title>No.of Files to Upload</title><meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″></head><body>
<form name=”form1″ method=”post” action=”uploadform2.php”>
<p>Enter the amount of boxes you will need below. Max = 9.</p>
<p>
<input name=”uploadNeed” type=”text” id=”uploadNeed” maxlength=”1″>
</p>
<p>
<input type=”submit” name=”Submit” value=”Submit”>
</p>
</form>
</body>
</html>

In above script, we can get maximum number of files need to be uploaded by user. We can set max upload boxes as 9. We can increase or decrease this to satisfy depending upon our project needs.

Step2 : Creating the Dynamic Form (uploadForm2.php)<html>
<head>
<title>Dynamic form</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
</head>

<html><head><title>Dynamic form</title><meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″></head><body>

<form name=”form1″ enctype=”multipart/form-data” method=”post” action=”processFiles.php”>
<p>
<?
// start of dynamic form
$uploadNeed = $_POST[’uploadNeed’];
for($x=0;$x<$uploadNeed;$x++){
?>
<input name=”uploadFile<? echo $x;?>” type=”file” id=”uploadFile<? echo $x;?>”>
</p>
<?
// end of for loop
}
?>
<p><input name=”uploadNeed” type=”hidden” value=”<? echo $uploadNeed;?>”>
<input type=”submit” name=”Submit” value=”Submit”>
</p>
</form>
</body>
</html>

In this page, We can create a simple HTML form with the value of the attribute “type” set to “file”. Within the form We cen put a block of code to start the for loop. We set $x to 0 and made it stop at the desired need by setting $x to be less than $uploadNeed – the value specified by the user. We also echo the $uploadNeed into a hidden input field to be carried over to the last page.

The key to making this all work however is the $x variable We are echoing right next to the uploadFile name. What this will do is append a number starting with 0 to the name. This in turn will make each upload field’s name unique.

Step3 : Creating a Multi-File Upload Script in PHP (processFiles.php)Here is the last page to complete our multiple upload tasks.

Here is the last page to complete our multiple upload tasks.<?
$uploadNeed = $_POST[’uploadNeed’];
// start for loop
for($x=0;$x<$uploadNeed;$x++){
$file_name = $_FILES[’uploadFile’. $x][’name’];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace(”‘”,”",$file_name);
$copy = copy($_FILES[’uploadFile’. $x][’tmp_name’],$file_name);
// check if successfully copied
if($copy){
echo “$file_name | uploaded sucessfully!<br>”;
}else{
echo “$file_name | could not be uploaded!<br>”;
}
} // end of loop
?>

The first thing we do in this page is grab the uploadNeed from uploadForm2.php. We setup our for loop in the same fashion as the last page. The difference here though is we get the $_FILES name within the for loop. We assign this to the local variable name $file_name.

Next, we do a little parsing by adding the stripslashes and str_replace functions. The reason we add the stripslashes is due to file that may have apostrophes in their name; otherwise this will generate a parse error and prevent that file from being uploaded.

Notice once again how I add the $x variable, which in turn is a number, to the name of the $_FILES. By doing this the script now knows which file it is uploading.

We will use the copy function now to actually begin the upload process. The last thing We added was a simple if statement to check that the copy was successful and We echo that message out to the screen.

Read your e-mail with PHP!

Read your e-mail with PHP! Monday, March 19th, 2007

To retrieve the e-mails in our inbox we will have to use the POP3 protocol, and the raw PHP socket functions, such as fsockopen. But why should we all write it from scratch, when there are already many great libraries available?

That’s why we’re going to use a POP3 class from PHPClasses.org, available at http://www.phpclasses.org/browse/package/1120.html. Unfortunately, you’ll need to login to download the package, but registration doesn’t take long.

Once you’ve downloaded the class, try the following example:
<?php
require (’pop3.class.inc’);

$pop3 = new POP3;

// Connect to mail server
$do = $pop3->connect (’YOUR-EMAIL-SERVER-HERE);
if ($do == false) {
die($pop3->error);
}

echo ‘Connected to mail server’;

$pop3->close();

?>

In the above example you will have to put in your own mail server. If you don’t know what your own mail server is, try something like mail.YOURDOMAIN.com or mail.YOURISP.com.

If everything goes okay, you will see ‘Connected to mail server’ when running the example. This means that the script was able to connect to your mail server, and the next step is to login to your e-mail inbox.

This can be done with the login() method, and you will have to pass your username (which is usually your e-mail address) and your password, like this:
// Login to your inbox
$do = $pop3->login (’email@address.goes.here.com, ‘password-here’);

if ($do == false) {
die($pop3->error);
}

echo ‘
Logged in’;

Now that we’ve logged into our inbox, we’re almost ready to start downloading new e-mail messages, but first we have to get the ‘office status’, which basically tells our script how many new e-mails there are.

The office status can be retrieved with the get_office_status() method; see the example below:
// Get office status
$status = $pop3->get_office_status();

if ($status == false) {
die($pop3->error);
}

$count = $status[’count_mails’];

echo ‘There are ‘ . $count . ‘ new e-mails waiting for you!’;

As you can see in the above example, the get_office_status() method returns an array, which includes the number of new e-mails, but also a few other details, such as the total size of the new e-mails. This will allow you to create a neat looking progress bar.

Now we can start retrieving the new e-mails with a for-loop and the get_mail() method, like this:
for ($i = 1; $i <= $count; $i++) {
$email = $pop3->get_mail($i);

if ($email == false) {
echo $pop3->error;
continue;
}

echo ‘<pre>’;
print_r ($email);
echo ‘</pre>’;
}

If you run the above example, it will print every new e-mail in your inbox, including the headers of each e-mail.


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.