Create custom error pages through .htaccess file

This is always a best practices to create your own error page rather showing the host default page. You can use your own custom error pages for any know error like 404 – page not found, 500 – Internal Server Error etc.
It can be simply done by adding the following code to your .htaccess file.

ErrorDocument errornumber /file.html

1. 404 – page not found

RewriteEngine On
ErrorDocument 404 /404.html

2. 500 – Internal Server Error

RewriteEngine On
ErrorDocument 500 /500.html

3. 403 – Forbidden

RewriteEngine On
ErrorDocument 403 /403.html

4. 400 – Bad request

RewriteEngine On
ErrorDocument 400 /400.html

5. 401 – Authorization Required

RewriteEngine On
ErrorDocument 401 /401.html

You can also redirect all error to single page. like

RewriteEngine On
ErrorDocument 404 /404.html
ErrorDocument 500 /404.html
ErrorDocument 403 /404.html
ErrorDocument 400 /404.html
ErrorDocument 401 /401.html

Password protect a PDF document in PHP

The following program will create password protected PDF documents in the fly using PHP. You need to download the required library files from here. Also you could get more information from here.

PHP Code

<?php
function pdfEncrypt ($origFile, $password, $destFile){
     require_once('fpdi/FPDI_Protection.php');
     $pdf =& new FPDI_Protection();
// set the format of the destinaton file
     $pdf->FPDF("P", "in", array('8.27','11.69'));
//calculate the number of pages from the original document
     $pagecount = $pdf->setSourceFile($origFile);

// copy all pages from the old unprotected pdf in the new one
for ($loop = 1; $loop <= $pagecount; $loop++) {
     $tplidx = $pdf->importPage($loop);
     $pdf->addPage();
     $pdf->useTemplate($tplidx);
}

$pdf->SetProtection(array(),$password);
$pdf->Output($destFile,'F');
return $destFile;
}

//password for the pdf file
$password = "wearecoders";

//name of the original file
$origFile = "AuditReport.pdf";

//name of the destination file 
$destFile ="AuditReport_p.pdf";

//encrypt the book and create the protected file
pdfEncrypt($origFile, $password, $destFile );

?>

jQuery Ajax File Upload Without Page Refresh in PHP

This is a very simple script used to upload single image file without page refresh using jQuery, Ajax and PHP. Here the user have option to set upload file format and set maximum file size. You need two jQuery files jquery.js and jquery.form.js to run this script.

Include .js  Files

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>

Create the HTML Form


<form class="uploadform" method="post" enctype="multipart/form-data" action='upload.php'>
Upload your image <input type="file" name="imagefile" />
<input type="submit" value="Submit" name="submitbtn" id="submitbtn">
</form>

<!-- The uploaded image will display here -->

<div id='viewimage'></div>

Create the HTML Form

<script type="text/javascript" >
$(document).ready(function() {
$('#submitbtn').click(function() {
$("#viewimage").html('');
$("#viewimage").html('<img src="img/loading.gif" />');
$(".uploadform").ajaxForm({
target: '#viewimage'
}).submit();
});
});
</script>

PHP File upload script

<?php
$file_formats = array("jpg", "png", "gif", "bmp"); // Set File format
$filepath = "upload_images/";

if ($_POST['submitbtn']=="Submit") {
  $name = $_FILES['imagefile']['name'];
  $size = $_FILES['imagefile']['size'];

   if (strlen($name)) {
      $extension = substr($name, strrpos($name, '.')+1);
      if (in_array($extension, $file_formats)) { 
          if ($size < (2048 * 1024)) {
             $imagename = md5(uniqid().time()).".".$extension;
             $tmp = $_FILES['imagefile']['tmp_name'];
             if (move_uploaded_file($tmp, $filepath . $imagename)) {
		 echo '<img class="preview" alt="" src="'.$filepath.'/'. $imagename .'" />';
	     } else {
		 echo "Could not move the file.";
	     }
	  } else {
		echo "Your image size is bigger than 2MB.";
	  }
       } else {
	       echo "Invalid file format.";
       }
  } else {
       echo "Please select image..!";
  }
exit();
}

?>

Process Credit Cards with PayPal

Now a days PayPal is weirdly used and popular payment processing platform to process credit cards. It is very easy to integrate PayPal payment pro API to your php shopping cart. It requires API Username, API password and API signature which you can get from PayPal site.

Files Included in this project.

constants.php
You need to change the file with your API username, password and signature which you get from PayPal. You can also set URL for sandbox mode and regular payment. Please change the following.

define('API_USERNAME', 'YOUR USER NAME HERE');
define('API_PASSWORD', 'YOUR PASSWORD HERE');
define('API_SIGNATURE', 'YOUR API SIGNATURE HERE');

Please change the following for sandbox mode and regular mode.

Regular payment:
define('API_ENDPOINT', 'https://api-3t.paypal.com/nvp');
define('PAYPAL_URL', 'https://www.paypal.com/webscr&amp;amp;amp;cmd=_express-checkout&amp;amp;amp;token=');

Sandbox mode:
define('API_ENDPOINT', 'https://api-3t.sandbox.paypal.com/nvp');
define('PAYPAL_URL', 'https://www.sandbox.paypal.com/webscr
&amp;amp;amp;cmd=_express-checkout&amp;amp;amp;token=');

CallerService.php

No need to change anything in this file.

confirmation.php

This is the sample file that contains a form with minimum fields required to process a payment. On payment success or failure PayPal will return success/ failure message to this page.

paypal_entry.php

This page process user data to PayPal.

How to Integrate Paypal Payment System in PHP & MySQL

Website Payments Standard consists of HTML FORM on your website that direct your customer’s web browser to the PayPal website in order to pay and after successful payment return the customer to your website.
Lets create a basic HTML form on submit it will redirect to PayPal site and after successful payment return back to your thank you page and on payment declined return back to payment declined page.

HTML Form


<form action="https://www.paypal.com/cgi-bin/webscr" method="post">

<!-- Identify your business so that you can collect the payments. -->
<input type="hidden" name="business" value="PayPal Email Id">

<!-- Specify a PayPal Shopping Cart Add to Cart button. -->
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="add" value="1">

<!-- Specify details about the item that buyers will purchase. -->
<input type="hidden" name="item_name" value="Your Product Name">
<input type="hidden" name="amount" value="3.95">
<input type="hidden" name="quantity" value="1">
<input type="hidden" name="currency_code" value="USD">

<!-- Display the payment button. -->
<input type="image" name="submit" border="0" src="https://www.paypalobjects.com/en_US/i/btn/btn_cart_LG.gif" alt="PayPal - The safer, easier way to pay online">
<img alt="" border="0" width="1" height="1" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" >
</form>

The above from will send single item to PayPal page. If you want to send multiple items then you need to change Amount field name to amount_x Quantity field name to quantity_x etc. ‘x’ represents the number. Also don’t forget to include the upload variable.

<input type="hidden" name="upload" value="1"> 

HTML Form to send Multiple Items.


<form action="https://www.paypal.com/cgi-bin/webscr" method="post">

<!-- Identify your business so that you can collect the payments. -->
<input type="hidden" name="business" value="PayPal Email Id">

<!-- Specify a PayPal Shopping Cart Add to Cart button. -->
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">

<!-- Specify details about the item that buyers will purchase. -->

<input type="hidden" name="item_name_1" value="Your Product Name-1">
<input type="hidden" name="amount_1" value="3.95">
<input type="hidden" name="quantity_1" value="1">

<input type="hidden" name="item_name_2" value="Your Product Name-2">
<input type="hidden" name="amount_2" value="4.95">
<input type="hidden" name="quantity_2" value="1">

<input type="hidden" name="item_name_3" value="Your Product Name-3">
<input type="hidden" name="amount_3" value="5.95">
<input type="hidden" name="quantity_3" value="1">

<input type="hidden" name="shopping_url" value="http://www.a2zwebhelp.com">
<input type="hidden" name="currency_code" value="USD">

<!-- Display the payment button. -->
<input type="image" name="submit" border="0" src="https://www.paypalobjects.com/en_US/i/btn/btn_cart_LG.gif" alt="PayPal - The safer, easier way to pay online">
<img alt="" border="0" width="1" height="1" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" >
</form>

You can also visit PayPal for more information. Click here to know more information about form fields and values.

Click here to know  how to Process Credit Cards with PayPal.

Get visitor’s location details from IP address in php

If you want to track your visitor’s location details like city, state, country etc then you can use the following script. In PHP we can easily get the visitors/ user IP address by using $_SERVER[‘REMOTE_ADDR’] option. But you can’t get other information like country, city, state etc directly in PHP. Codehelper.io provides the solution for this.

How to get user IP Address in PHP

 $ip = $_SERVER['REMOTE_ADDR']; 
 echo $ip;

How to get user/ visitor Location in PHP

require_once("userip/ip.codehelper.io.php");
require_once("userip/php_fast_cache.php");

$_ip = new ip_codehelper();

$real_client_ip_address = $_ip->getRealIP();
$visitor_location       = $_ip->getLocation($real_client_ip_address);

$guest_ip   = $visitor_location['IP'];
$guest_country = $visitor_location['CountryName'];
$guest_city  = $visitor_location['CityName'];
$guest_state = $visitor_location['RegionName'];

echo "IP Address: ". $guest_ip. "";
echo "Country: ". $guest_country. "";
echo "State: ". $guest_state. "";
echo "City: ". $guest_city. "";

The above code provides the information like ip, Continent Code, Continent Name, Country Code in 2 digit, Country Code in 3 digit, Country short code, Country Name, Region / State Name, City Name, City Latitude, City Longitude, Country Latitude, Country Longitude, Local Time Zone, Calling Code, Population, Area in Sq Km, Capital, Electrical, Languages, Currency and Flag image.

$ip 			= $visitor_location['IP'];
$Continent_Code 	= $$$visitor_location['ContinentCode'];
$Continent_Name 	= $visitor_location['ContinentName'];
$Country_Code2 		= $visitor_location['CountryCode2'];
$Country_Code3 		= $visitor_location['CountryCode3'];
$Country 		= $visitor_location['Country'];
$Country_Name 		= $visitor_location['CountryName'];
$State_Name 		= $visitor_location['RegionName'];
$City_Name 		= $visitor_location['CityName'];
$City_Latitude 		= $visitor_location['CityLatitude'];
$City_Longitude 	= $visitor_location['CityLongitude'];
$Country_Latitude 	= $visitor_location['CountryLatitude'];
$Country_Longitude 	= $visitor_location['CountryLongitude'];
$Country_Longitude 	= $visitor_location['CountryLongitude'];
$LocalTimeZone 		= $visitor_location['LocalTimeZone'];
$Calling_Code 		= $visitor_location['CallingCode'];
$Population 		= $visitor_location['Population'];
$Area_SqKm 		= $visitor_location['AreaSqKm'];
$Capital 		= $visitor_location['Capital'];
$Electrical 		= $visitor_location['Electrical'];
$Languages 		= $visitor_location['Languages'];
$Currency 		= $visitor_location['Currency'];
$Flag 			= $visitor_location['Currency'];