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

Find IP Address in PHP


Find IP Address in PHP

Here now learn the script of to find ip address in php of visitor which visit on website.

To store Visitor ip addres in PHP has syntax :-

1
$_SERVER[‘REMOTE_ADDR’]     // return visitor ip address
When a ip address has been received then now need text file to enter in it by using the
 syntax fwrite() function

$myipaddress = $_SERVER['REMOTE_ADDR']."\r\n";
In above codes first declare a variable which can pick IP address of  Visitor. One thing remember if you are testing or working on localhost server then naturally it return Local IP 127.0.0.1
First, we will use a variable to store the ip address. If you are working on a localhost environment, it will return 127.0.0.1
1
$myipaddress = $_SERVER['REMOTE_ADDR']."\r\n";
Now time to store visitor IP address in file using the PHP functions fopen() and fwrite(). 
Use below codes for that :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// store visitor ip address
$myipaddress = $_SERVER['REMOTE_ADDR']."\r\n";

// Log file name which store all ip address
$ipfile = 'logdata.txt';

// store visitor data by opening log file
$ipfp = fopen($ipfile, 'a');

fwrite($ipfp, $myipaddress);

fclose($ipfp);
These codes produce the output like as below in our log file:

127.0.0.1
127.0.0.1
127.0.0.1
127.0.0.1
We can also approve more this script with help of to enter other PHP funcations like as browser information and visitor’s visited page.
For that we need miner changes in our pre-written PHP script codes.
Use below codes for that :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// store visitor ip address
$myipaddress = $_SERVER['REMOTE_ADDR']."\r\n";

// visited page name
$page = $_SERVER['SCRIPT_NAME'];

// browser detail
$brow = $_SERVER['HTTP_USER_AGENT'];

// Log file name which store all ip address
$ipfile = 'logdata.txt';

// store visitor data by opening log file
$ipfp = fopen($ipfile, 'a');

fwrite($ipfp, $myipaddress);

fclose($ipfp);
These codes produce the output like as below in our log file:

127.0.0.1 - /product.html Mozilla 19.0 (Windows NT)
127.0.0.1 - /product.html Mozilla 19.0 (Windows NT)
127.0.0.1 - /product.html Mozilla 19.0 (Windows NT)
127.0.0.1 - /product.html Mozilla 19.0 (Windows NT)
Enjoy :) you have done!
Share:

PHP Pagination


Pagination of pages in PHP is a topic famous topic. So here I not explain any new php codes for Php Pagination. I will explain how you can use pagination for data held within an array.
Normally you did only need to paginate your data that if you’ve got quite a lot of it in.With database systems of course pagination (Php Pagination) can be achieved relatively easily by specifying the offset parameters in an SQL query. But what if your data didn’t come from a database table and instead came from a flat file. So look at the following code which expalin exactly how it’s done Php Pagination.
// Data, normally from a flat file or some other source
$data = "Item 1|Item 2|Item 3|Item 4|Item 5|Item 6|Item 7|Item 8|Item 9|Item 10";
// Put our data into an array
$dataArray = explode('|', $data);
// Get the current page
$currentPage = trim($_REQUEST[page]);
// Pagination settings
$perPage = 3;
$numPages = ceil(count($dataArray) / $perPage);
if(!$currentPage || $currentPage > $numPages)
$currentPage = 0;
$start = $currentPage * $perPage;
$end = ($currentPage * $perPage) + $perPage;
// Extract ones we need
foreach($dataArray AS $key => $val)
{
if($key >= $start && $key < $end)
$pagedData[] = $dataArray[$key];
}

Created the $data array which contains a long line of items distribute up by the pipe '|' character. Realistically this would be real data just for this tutorial I'll keep it simple. Then, using the explode() function I've cut up the $data variable into array using '|' as the delimeter.
The line 8 in Php Pagination simply gets the current page number one is provided.
Lines 7 to 13 Php Pagination do the math calculations which make this array pagination work. Now set how many items we'd like to display per page into the variable, $perPage. In the example above I've set this to 3.
On line 8 we're working out how many pages there are going to be. This can be done by dividing the total number of items (by using the count() function) by the items per page value. Notice on this line that I'm also using the ceil() function. This basically rounds the number up (e.g. 5.134 becomes 6).
We then have a simply if statement on lines 9 and 10 of Php Pagination. It's basically saying that if no page number has been provided or if the provided page number is more than the number of pages, set it to 0. This stops people from trying to access pages which have no items.
On lines 14 and 15 of Php Pagination we're setting the $start and $end variable which you might recognize if you've done pagination using SQL queries before. The $start variable holds the lowest ID of the item which can be displayed on this page. The $end variable is maximum cap which the items ID can be to be displayed (actually, it's one above, but this depends on how you do your if statement on line 17 of Php Pagination).
Great, we're nearly there. Now, on line 15 of Php Pagination we start a foreach statement which loops through each of our data items. Inside this loop is a simple if statement to see if the id of the current data item is above (or equal to) the $start value and below the $end value. If it is then we place a copy of it into our $pagedData array.
Once the foreach loop has finished the $pagedData array now contains all of the data items which we should be displaying on the current page. All we have to do now is to loop through and display them. This has been shown in the following code snippet.
foreach($pagedData AS $item)
            echo $item . "<br>";

As displaying the data goes that's it. Now need to display the pagination links to let you navigate your way through the pages. Here's the code for Php Pagination.

if($currentPage > 0 && $currentPage < $numPages)
            echo '<a href="?page=%27%20.%20%28$currentPage%20-%201%29%20.%20%27">« Previous page</a><br>';

if($numPages > $currentPage && ($currentPage + 1) < $numPages)
            echo '<a href="?page=%27%20.%20%28$currentPage%20+%201%29%20.%20%27" class="right">Next page »</a><br>';

Above codes of Php Pagination contain two quite simple if statements. First is displaying the "previous page" link and the another is for displaying the "next page" link.
If first statement checking to see if the current page is more than 0 and if the current page is less than the total number of pages.
The second if statement is checking to see if the total number of pages is more than the current page number and if there are any more pages after the current page.
Atleast you have done Php Pagination.
Share:

Send Mail using Php


Now Todays we will lean “send mail using php”. It simple method to send email using php codes. There are some field require to process this php script. These are like as To, Subject, Message, From. You can also extend all other option its depend on your requiremet. This guide to beginner for “send mail using php”.

Send Mail using Php Step 1:

Create File with name sendmail.php and paste above codes in it.

<?php
$to = "toaddress@mailservername.com";
$subject = "Just a test mail";
$message = "Just training on php";
$from = "fromaddress@mailservername.com";

Now we define the parameters to,suject,message and header containing from and reply address. In script header is the area where user set the from address, reply to address, cc address, etc.

/* To send HTML mail, you can set the Content-type header. */

$headers1 = "MIME-Version: 1.0\r\n";
$headers1 .= "Content-type: text/html; charset=iso-8859-1\r\n";

/* additional headers */
$headers1 .= "To: ".$to."\r\n";
$headers1 .= "From: ".$from."\r\n";
$headers1 .= "Reply-To: ".$from."\r\n";
?>

Send Mail using Php Step 2:
 
Now send the mail through Php Api

<?php
mail($to, $subject, $message, $headers);
?>
 You have done Send Mail using Php !
Share:

Generate Php Form for mailing

 In this tutorial explain about 'How make mail form in Php?'. First of all you need a form, as for example the one in the table:

Form.html 

<FORM ACTION="formtomail.php" METHOD=post>

<!-- Your fields here -->

<INPUT TYPE=submit value="Submit">
</FORM> 

The Form Action must be directed to the PHP script bellow. you need the php script (copy the information in the table to a text file and save the file as "formtomail.php" in your server).

Formtomail.php 
<?
if ($_POST){
      // posted information is added to variable message
      $message="";
      foreach ($_POST as $formfieldname => $formfieldvalue){
            $message.="$formfieldname \n $formfieldvalue\n\n";     //
      }
     // send email with information from the form to
      mail("webmaster@mysite.com","Form to Mail", $message,"From: <webmaster@mysite.com>\nContent-Type: text/plain");
      print "The information has been send to webmaster";
}else{
      print "No information has been posted";
}
 
?> 

You need to customize the script: substitute red text in the script by your email. This scripts assumes you have no restrictions to use mail commnad.
Share: