Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Wednesday, January 31, 2018

PHP generates JWT

This script generates basic JWT token

<?php
$decode = file_get_contents('php://input');
$arr = json_decode($decode, true);
if ($arr['email'] == 'me@example.com' && $arr['password'] == '123') {
    $key = 'very-secret-value-only-on-server';
    // header
    $h = ["alg" => "HS256", "typ" => "JWT"];
    $h = base64_encode(json_encode($h));
    
    //payload
    $p = ["username" => "username", "role" => "admin"];
    $p = base64_encode(json_encode($p));
    
    // encryption and signing
    $signature = hash_hmac('sha256', "$h.$p", $key, true);
    $signature = base64_encode($signature);

    $token = "$h.$p.$signature";
    echo $token;
}

You should add iat and exp to payload. Debugger for JWT.

Thursday, October 22, 2015

Webgrind profiling in WAMP

You can get list of all PHP functions and their arguments that were executed with Webgrind extension. In URL of page put XDEBUGPROFILE=true as shown on video.



You need to setup your php.ini like this
; XDEBUG Extension

zend_extension = "c:/wamp/bin/php/
php5.5.12/zend_ext/
php_xdebug-2.2.5-5.5-vc11-x86_64.dll"
;
[xdebug]
xdebug.remote_enable = 1
xdebug.profiler_enable = 1
xdebug.profiler_enable_trigger = 1
xdebug.profiler_output_name = cachegrind.out.%t.%p
xdebug.profiler_output_dir = "c:/wamp/tmp"
xdebug.show_local_vars=0
Keep in mind that WAMP has two php.ini files. So you need co change the right one.

Friday, October 16, 2015

Sunday, July 5, 2015

PHP Dependancy Injection

This two scripts show Motor class as dependency that is injected in Car class.

<?php
class Motor {
    public function startEngine(){
        echo 'engine has started';
    }
}

class Car {
    public function __construct(Motor $m){
        $m -> startEngine();
    }
}

$a = new Car(new Motor());
?>

and second example

<?php
class Motor {
    public function __construct(){
        echo 'engine has started';
    }
}

class Car {
    public function __construct(){
        $m = new Motor();
    }
}

$a = new Car();
?>

The point is that instancing of dependent class is not done in client but in consumer class. 

Thursday, March 27, 2014

Consuming SOAP services

Full web service description is at http://www.webservicex.net/ws/WSDetails.aspx?CATID=12&WSID=64
Yellow highlighted portion is SOAP Web Service URI
Green Highlighted portion is the method name.

Wednesday, March 19, 2014

Wednesday, March 12, 2014

Most Useful PHP Snippets

Dumping arrays or objects with HTML markup text as member. This prevents creating iFrame and similar tags in your debug output.

Monday, March 3, 2014

OOP Basics

/**
 * Instances CANNOT use private and protected members
 * Children CAN inherit only public and protected members
 */
class MyClass
{
    public $name = 'John';
    protected $username = 'JohnT23';
    private $password = 'secret';

    function printAll()
    {
        echo "From class MyClass:";
        echo $this->name." ";
        echo $this->username." ";
        echo $this->password." ";
    }
}

$obj = new MyClass();
echo "From Instance \$obj:".$obj->name."\n"; //Works
//echo $obj->username; // Fatal Error
//echo $obj->password; // Fatal Error
$obj->printAll();

echo"\n<br>";

class MyClass2 extends MyClass
{
    // We can re-declare/access the public and protected method
    // but not private ones
    function printAll()
    {
        echo "From class MyClass2:";
        echo $this->name." ";
        echo $this->username." ";
        // Children cannot access private member of parent
        // echo $this->password; // Error
    }
}

$obj2 = new MyClass2();
echo "<br>
 From Instance \$obj2:".$obj2->name; // Works
//echo $obj->username; // Fatal Error
//echo $obj->password; // Fatal Error
$obj2->printAll();

Sunday, February 23, 2014

Testing File Permission for file upload and database connectivity

On a freshly installed Linux server you need to test file permission and database connectivity.
Here are some scripts that can help you with that:

Saturday, January 18, 2014

Two Way Encryption

Two way encryption on PHP works with mcrypt extension enabled.
Here's a simple snippet that uses password and generates different encryption string every time you reload.
<?php
$string = "This is a string to be encrypted";
$key = "This is a key";
// Encryption Algorithm
$alg = MCRYPT_TWO_FISH;
// Create the initialization vector for added security
$iv = mcrypt_create_iv(mcrypt_get_iv_size($alg, MCRYPT_MODE_ECB), MCRYPT_RAND);
// Output original string
print "Original string: $string <p>";
// Encrypt $string
$encrypted_string = mcrypt_encrypt($alg, $key, $string, MCRYPT_MODE_CBC, $iv);
// Convert to hexadecimal and output to browser
print "Encrypted string: ".bin2hex($encrypted_string)."<p>";
$decrypted_string = mcrypt_decrypt($alg, $key, $encrypted_string, MCRYPT_MODE_CBC, $iv);

print "Decrypted string: $decrypted_string";
?>
A short video how code works

Monday, November 11, 2013

Nesting with columns named id and parent_id

Create table mytable :

CREATE TABLE IF NOT EXISTS `mytable` (
  `id` int(11) NOT NULL,
  `parent_id` int(11) NOT NULL,
  `name` varchar(256) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `mytable`
--

INSERT INTO `mytable` (`id`, `parent_id`, `name`) VALUES
(1, 0, 'item 1'),
(2, 1, 'item 2'),
(3, 2, 'item 3');

And then run following script

Thursday, July 18, 2013

Execute Linux Commands from Browser

If you need to execute a shell command using browser here's how to do it. To understand this tutorial you need to have advanced level of understanding of Linux and PHP.

Wednesday, July 10, 2013

PHP debugging with JavaScript Console, Error and Access Log

You can use JavaScript console to output global and other variables from your PHP file. That way you can use JS console to track what is is going on in PHP backed files that are called by $.ajax. Also you should be familiar with PHP's functions utf8_encode and utf8_decode

Raw outuput of JSON string and Base64 encoded JSON string

Sunday, July 7, 2013

JSONP Tutorial

This is an advanced tutorial with working example you must know jQuery and JSONP foundations. JSONP bypass browser's "same origin" policy.

Tuesday, July 2, 2013

Parse JSON generated by PHP

This tutorial shows how to parse a JSON string generated by PHP. Place job.php and index.html in same folder.

Thursday, June 6, 2013

Basic APC Tutorial

APC is very powerful caching system for PHP. Here are some basics. First run test_apc.php and if you in 5 seconds click on delete link the key "str" will be deleted from cache. If you click on delete link after 5 second the key "str" will expire from APC caching system.

Friday, May 31, 2013

Tuesday, April 9, 2013

Delete row with jQuery and PHP

Do you want to delete a row in HTML and MySQL table with fancy fade out effect? This is the right tutorial for you assuming that you are familiar with basics of jQuery and PHP.
You make an asynchronous call to PHP script called delete.php so end user doesn't leave page when a row is deleted, instead he gets an alert box that confirms that row is deleted and the row gradually disappears.