Wednesday, March 28, 2012

Encode PHP files, How to encode php?


PHP Code is our asset of development and concepts. We need to protect / secure our PHP Source code on distribution server. In order to protect PHP source code we need to  encode php files before going to distribute. To encode PHP files to protect PHP source. Here is my solution to protect our PHP Source code with PHP encryption and get Encrypted PHP Source to deploy. In this task we have to use htmlspecialcharsgzdeflatebase64_encodehtml_entity_decodeeval functions



How to encode PHP files and PHP Encoder Online


SourceCode.php   
Put your PHP Source you need to Encode/Protect also include domain based verification code inside this.

<?php
// SAMPLE SOURCE CODE allowed to run only on w3lessons.com
// Start
// PHP Tag in begining and Ending is Must to process encoding...
if(!strstr($_SERVER['HTTP_HOST'],"w3lessons.com"))
die("Unlicenced Domain");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>How to Encode php files</title>
</head>

<body>
<?php
echo "<h1>Welcome</h1>";
?>
</body>
</html>
<?php
//End
?>

Encoder.php
Run Encoder.php (Output will create a New File EncodeOutput.php

<?php ob_start();

// Put your Source in this file SourceCode.php
$EncodeFileName = "SourceCode.php";
$EncodeCount = intval(rand(5,72)); // Adjust to improve your code security.

// It will cleated automatically after encoding....
copy($EncodeFileName,"EncodeOutput.php");

for($i=0;$i<=$EncodeCount;$i++)
{
 $phpcode = file_get_contents("EncodeOutput.php");
 //remove space
 $phpcode=trim($phpcode);
 //count string
 $count=strlen($phpcode);
 $last=$count-4;
 $rest = substr($phpcode, 2, $last);
 $display= substr($rest, 4);
 $string= htmlspecialchars($display,ENT_NOQUOTES,'UTF-8');
 $phpcode=stripslashes($phpcode);
 $compressed = gzdeflate($string, 5);
 $encode = base64_encode($compressed);

 $phpcode = "&lt;"."&#63;"."php \n";
 $phpcode .= 'eval(html_entity_decode(gzinflate(base64_decode("'.$encode.'")),ENT_NOQUOTES));';
 $phpcode .= "\n".'?'.'>';

 file_put_contents("EncodeOutput.php",html_entity_decode($phpcode,ENT_NOQUOTES));
 usleep(1000);
}

echo "Encoding Completed ! <br /><br />View <a href=\"EncodeOutput.php\">Encoded Output</a>";

?>



EncodeOutput.php
Backup your Original file and replace the encodeoutput.php content instead of Actual PHP Code



<?php 
eval(html_entity_decode(gzinflate(base64_decode("NVa3ruWGEuv9Ga52IQPKCYaLo5xzbhbKOesoff27zWunGJIAOZy/yjMdfjXHOPwpp6M9nj9Fmc9F+at+26ka0qP8laV7SWD/n/9tJC7Kbc4s9N5bxo+CN/mGOaftTN0Z+fpzbbsFoR1BZKXnpcA3ECIXqsbN7KzLI78WZtArsp00kJV7u407OI4AGrhcpoLAk5lH2UjgCdLH4oxANSqBOMvnwL3p7gerUjkv/l0gF28xEiensPA3wLUnqT+c4Svojbx3E8efTEI2JkJfLBdPNSxHn6fsQPGUipQAPCjOaPWsYq1lAYLp/GLW90X+gEuho+lIIRtikPqlpJ7LuvgOa7MbGKHro9mSi3C4OR4xOjX7fDCrvJ7dXcnrDUc5vC3p6j4HPH1YRTAe56IKX00Nj6Aui0f8qFdEO0T5U6gWDDo8+ciDyzYDZeXg0/Iqf4zeg9tJK0VH+t5vJUSCOGf6OvNkHhCgKa4GmbRqZw8WiYc2NGpm+PNgejggMeeX6sussGa7EyeE3PyN584xf1CT+KLwZkBzRvqK6rhF7EQQVL7TRB2xoOfy6/qjIIZUgcJADG77b+E/7+2DUNMFjQTMF8HxNTp4U4nTNiFfBPwlUB66yEAcQ1Ptpvmy52EL0JU5aHcgW8eIAihGVFPMhIpFAaSaIDXtCUV+Yv0bgvqiOZJhGq9pxI98J5IP29CeS0vsK9ulNQeteDUmqf43PgqARr+McyB8G+smUfvjdoa1ytB1LI0hTFJSDNbTZLyyMk4ZYW9kFcK1HowSLH8vHV61hJZAvyGbZPScaxcuQZFj7oluyZazZYcyyYxjg2VL3OASOUHhTezw++s6Ka0neSO1iJocc41fu7bJJVa7xBo3aXBdxyr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FeIr/6MtUmVRzrqTPMTp8+UiJ+UKXILLouz91mr8MH8ix5ku5R0Gfg0S4mgxFvk9hZ1WG6kjZlYrtTOTb0wlhcs4duFe7IulSZB2d73uFOtD95Iq54wkMqZFzmo6ELK6dBiWzQ2L/nfM22mx9EYOMMKs8ZcTqtCAxGT3QYACytBc3VM+awnR1w3Trx7W9ipbgQhyPmypnXV+9n2FaB6+seLxaMW86sOFiQBTLAgOhmhoNB9ViTR+2IXGP4qUiXlEOip2upunq3xlmtOLuue6B4E08luwB+82FiHgl7CZ+IDgiqMobc6yJcoODZ1wVfAZc9pZcTGhbmsxro2ztHk6OfMHwoSpPuVymSoHFAiedI9L5ozgWsY+jj++L9WJRjsuT1TRA/ebyDep4tQwyKW/acsrh0SdFDfrYH60oPL/jbsbhWkPxrZT965FMDcNyEhLV1wi1AjAS7hQ+g8JXKAzBQOjHClMPDg6jlMPdhIWTwNyroff2JbiCSPn/qZ5sfMFkbb3Qdr1riTDhDuUfW+lEDVxKxxNi0e2CSxHtfTL0NJnBNS8weCFJYVmeYLvhNAXOBrK5rpg+hainRoikGnH2fOakve/f4yH3/81ogNJBiSb76UG/W0yEELR0+8NNPSXVrH4DjTp7VzCefb3hvaagTEWKanqKkAXUcVXOxrMhDLHIAA40T2ECKyTFRg/naz+KI4Cpwo0gPATe3lnDF9sjSw7ijmJn+NBHN9pRUe3i5HirtgJnOcEIRWNDnkKfmZFt9LCyR2xl8mi6pL/LBYyPrTVTd/GlQ0sHfktCbGdqSPg1msVuH6ZJdUeLWsyGQa7q2wjc9c7qONOtC9h5iph1FsxVXSm16IMKeqvykVcdjjD42GgiC1milnwgMtzhDZmkGwelFSRBgrv/++/v37394w/tjmPbPUt79/fvfv/4H")),ENT_NOQUOTES));
?>


Tuesday, March 6, 2012

How to SEO Website, Series.


Search Engine Optimization SEO, is an key work for any website to promote your business or brand through web among Search Engines. Here is the Major points as a answer for your question, We will saw one by one in this "How to SEO Website" Series.

How to SEO your Website



12. Optimize Title
11. Local Listing
10. Sitemap Submission
09. Blogger Linking
08. Sidelink Showing
07. Google Trends
06. Social Singles
05. Followed Links
04. 404 Fixes
03. 301 Fixes
02. Indented Listings
01. You will get first Place on Google



Monday, March 5, 2012

Page Loading Time PHP


Execution of server side scripting need to optimize in order to get the Optimized page loading time. In this case most of the Server Side Response typically not more than 2 to 30 seconds. In this case we need to find the Exact page loading time in PHP using the following Script.


Find Page Loading Time PHP
<?php
function page_loading_time()
{
    list ($msec, $sec) = explode(' ', microtime());
    $microtime = (float)$msec + (float)$sec;
    return $microtime;
}

$start=page_loading_time();

usleep(100000);  // Delaying page output 0.1 second for testing purpose.

echo "<br />";
$end = page_loading_time();
// Print results.
echo 'Page Loading Time: <b>' . round($end - $start, 2) . '</b> seconds';   
?>

Login with Facebook


Today I am going to teach you, How to Integrate Login with Facebook Button to our website without an registration form on your website using Visitors Facebook Account.

In Order to Create a "Login with Facebook" for your website you need a Facebook account to generate APP_ID and APP_SECRET, Create a Facebook user account and Navigate to the App Developer Page
http://www.facebook.com/developers/

In Top right  press "Create New App" Button you will navigate to the following screen.


In this App Creation Popup, tell your app Display Name on your Facebook Login Popup. and App Namespace is Unique Identifier on Facebook (all lowercase) Check I Agree and Continue. in next screen expand Website Rown from "Select how your app Integrates with facebook" Section.


put your website URL which actually place the "Login with Facebook" Button. eg: http://www.w3lessons.com/ or In case of Local testing you can put http://localhost/facebooklogin/
 press "Save Changes" on Bottom of that screen register your App on Facbook.

Now Your App is Ready and you can create "Login with Facbook" Button for your website.

1. PHP functions for Server Side handling to get Facebook data and store them to our database.
2. Create Login Button Using Facbook JavaScript SDK
3. Create Logout Button Using Facebook SDK

facebook-login.php  (full Source)
<?php
session_start();
define('YOUR_APP_ID', 'YOUR_APP_KEY_HERE');
define('YOUR_APP_SECRET', 'YOUR_SECRET_KEY_HERE');

function get_facebook_cookie($app_id, $app_secret) { 
    $signed_request = parse_signed_request(@$_COOKIE['fbsr_' . $app_id], $app_secret);
    // $signed_request should now have most of the old elements
    $signed_request['uid'] = $signed_request['user_id']; // for compatibility 
    if (!is_null($signed_request)) {
        // the cookie is valid/signed correctly
        // lets change "code" into an "access_token"
  // openssl must enable on your server inorder to access HTTPS
        $access_token_response = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=$app_id&redirect_uri=&client_secret=$app_secret&code={$signed_request['code']}");
        parse_str($access_token_response);
        $signed_request['access_token'] = $access_token;
        $signed_request['expires'] = time() + $expires;
    }
    return $signed_request;
}

function parse_signed_request($signed_request, $secret) {
  list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

  // decode the data
  $sig = base64_url_decode($encoded_sig);
  $data = json_decode(base64_url_decode($payload), true);

  if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
    error_log('Unknown algorithm. Expected HMAC-SHA256');
    return null;
  }

  // check sig
  $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
  if ($sig !== $expected_sig) {
    error_log('Bad Signed JSON signature!');
    return null;
  }

  return $data;
}

function base64_url_decode($input) {
  return base64_decode(strtr($input, '-_', '+/'));
}

if (isset($_COOKIE['fbsr_' . YOUR_APP_ID]))
{ 
$cookie = get_facebook_cookie(YOUR_APP_ID, YOUR_APP_SECRET);

$user = json_decode(@file_get_contents(
    'https://graph.facebook.com/me?access_token=' .
    $cookie['access_token']));
 
/*
HERE YOU CAN CREATE YOUR OWN SESSIONS TO MANAGE USER LOGIN LOGOUT
Uncomment this to show all available variables
echo "<pre>";
 - print_r function expose all the values available to get from facebook login connect.
print_r($user);
 1. Save nessary values from $user Object to your Database
 2. Register a Sesion Variable based on your user account code
 3. Redirect to Account Dashboard
echo "</pre>";
*/
 
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Facebook Login Connect for Website Demo</title>
<style type="text/css">
body,td,th {
 font-family: Verdana, Geneva, sans-serif;
 font-size: 14px;
 color: #333;
}
body {
 margin-left: 50px;
 margin-top: 50px;
}
</style>
</head>
<body>
<?php if (@$cookie) { ?>
<h2>Welcome <?= $user->name ?> </h2> <br />
E-mail ID: <?= $user->email ?>
<br />
<a href="javascript://" onclick="FB.logout(function() { window.location='facebook-login.php' }); return false;" >Logout</a>
<?php } else { ?>
<h2>Welcome Guest! </h2>    
<div id="fb-root"></div>
<fb:login-button perms="email" width="width_value" show_faces="true" autologoutlink="true" size="large">Login with Facebook</fb:login-button>
<?php } ?>
<script src="http://connect.facebook.net/en_US/all.js"></script>   
<script>
 // Initiate FB Object
 FB.init({
   appId: '<?= YOUR_APP_ID ?>', 
   status: true,
   cookie: true, 
   xfbml: true
   });
 // Reloading after successfull login
 FB.Event.subscribe('auth.login', function(response) { 
 window.location.reload(); 
 });
</script>
</body>
</html>



Download This Script     Facebook Connect Live Demo

Sunday, March 4, 2012

Remove HTML comments PHP


Cleaning or Removing HTML comments is one of the main part of Optimizing web pages. Here is the solution to Remove HTML Comments with PHP.

To Remove HTML Comments in PHP, We need to use the following Technique.

Remove HTML Comments with PHP
<?php
// Start OutputBuffer
ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Remove HTML Comments</title>
</head>
<body>

<!-- // HTML Comments for code identification -->

<!-- Mutiple Closing Tags-->
<h1>Remove HTML Comments Example</h1>
<!-- Unclose End Tag-->
<p>This is an example to remove HTML Comments <p>

</body>
</html>
<?php
// Store HTML Output Buffer as variable with ob_get_clean();
$html = ob_get_clean();

// Specify configuration
$config = array(
           'indent'         => false,
     'hide-comments' => true,
           'output-xhtml'   => true,
           'wrap'           => false
     );

// Tidy
$tidy = new tidy;
$tidy->parseString($html, $config, 'utf8');
$tidy->cleanRepair();

// Output
echo $tidy;
?>

Friday, March 2, 2012

PHP Tidy - Clean HTML Tags, HTML code, HTML Source, Comments


Website Webpage Optimization part, HTML pages contain many unnecessary development usage section such as Comments, Indents, Empty Lines, Accidental Enclosed tags without End Tag, Unknown Closing Tag, These are very sensitive for Browser Compatibly Issues, Page Loading Time and Web Page Optimization



We can able to keep our Web pages keep away from Unnecessary things such Comments, Un closed Tags, Un closed End Tags using HTML Tidy Object from PHP.

Here is the Example Code for PHP tidy Object. with Examples.
With this Example We have to use tidy Object (Tidy Object need to Enabled in PHP Configuration).
Check with phpinfo() under Tidy. ob_get_clean() , parse HTML String with parseString() function from Tidy object, and Clean our HTML with cleanRepair()