How Does PHP Session Works

Basically, the session is the system or way to store information for individual users. It’s storing individual users information against a unique session ID. So in this way, user’s data can be accessible across all pages of a website. PHP follow simple workflow for manage session, and that is when a session is started, then PHP either retrieve existing session or create a new session. If PHPSESSID is passed then PHP retrieved existing session otherwise creates a new session

By default, session creates a file in a temporary directory (which determined in php.ini file) on a server. All all the variables & value are stored there.

How Does PHP Session Works

How is Works:

  • Firstly PHP creates a unique identifier number (a random string of 32 hexadecimal number, e.g 3c7foj34c3jj973hjkop2fc937e3443) for an individual session.
  • PHPSESSID cookie passes that unique identification number to users’ browser to save that number.
  • A new file is created on the server with the same name of unique identification number with sess_ prefix (ie sess_3c7foj34c3jj973hjkop2fc937e3443.)
  • The browser sends that cookie to the server with each request.
  • If PHP gets that unique identification number from PHPSESSID cookie (on each request), then PHP searches in the temporary directory and compares that number to the file name. If both are the same, then it retrieves the existing session, otherwise it creates a new session for that user.

A session gets destroyed when the user closes the browser or leaves the site. The server also terminates the session after the predetermined period of session time expires. These are the simple mechanism steps that PHP is using to handle the session. I hope this article with help you to understand how PHP SESSION is working.

Understanding of PHP Traits

As we know PHP is a single inheritance language, so what does it mean? It means one class can only inherit from another one class. So it doesn’t allow to inheriting from multiple class. But sometimes it’s might be beneficial to inherit from multiple classes.

To take care of this problem PHP introduced Traits in their 5.4 release. With the using traits, the developer can avoid the problems of multiple inheritances.

What is Traits:

Traits are something like a group or bundle of methods which you want to use within other classes. PHP introduced traits to reduce code duplication and overcome the limitations of single inheritance.

Using PHP Traits

From the above code snippet, you can see we have traits called Common and two different classes called Article and Comments.
Both Article and Comments use Common traits. So when a class uses any traits then all of the methods will available within that class. So the socialSharing() method of Common traits is now available within of these two classes.

In this way, you can reduce your code and avoid multiple inheritances.

Return Image as response by PHP

Return Image as response By PHP

To return an image as a request response by PHP you fpassthru function of PHP. I have included a sample code snippet, which will help you to understand how to do this.

From the above code snippet, you can see I have opened an image with binary mode by fopen($image, rb). And then send that with appropriate headers by header(“Content-Type: image/png”). After that dumping that image by using fpassthru, and finally stop the execution of PHP. It’s really easy to return image as response by PHP

How to Install xDebug in Ubuntu

xDebug is most famous and popular PHP extension which provides the debugging of your code very smartly. It is most important to a programmer to debugging their code/data, xDebug gives us more flexibility to debug our code.  Here I am gonna show you how to install  xDebug in your ubuntu machine.

Run this code through your terminal.
[PHP]~$ sudo apt-get install php5-xdebug[/PHP]

Then open your php.ini file and add these code in your ini file.

[PHP]
# Added for xdebug
zend_extension=”/usr/lib/php5/20100525/xdebug.so”
xdebug.remote_enable=1
xdebug.remote_handler=dbgp xdebug.remote_mode=req
xdebug.remote_host=127.0.0.1 xdebug.remote_port=9000
[/PHP]

Finally restart the server.
[PHP]~$ sudo service SERVER_NAME restart[/PHP]

All done! now you can enjoy your debugging.

Singleton Design Pattern

In software engineering, a singleton the design pattern which allowed only object of a class. That means singleton pattern restricts to create multi instance of a class. This is useful, when we need to create only one instance or object of a class. To do this we must need to ensure a mechanism where we can access that class without creating object for them.

This is very basic concept of  singleton design pattern.

The way of specified a String in PHP

A string literal can be specified in four different ways.

  • single quoted
  • double quoted
  • heredoc syntax
  • nowdoc syntax

Single Quoted

single quoted is the simplest way to specified a string. It can  print everything inside the quoted.

$var = 'I Love to play cricket';
echo $var

Double Quoted:

If the string is enclosed in double-quotes (“), PHP will interpret more escape sequences for special characters:

$var = "I Love to play cricket";
echo $var;

Heredoc Quoted:

Heredoc is the third way to specify the string. To use heredoc we need to use (<<<) this syntaxt. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.

$var = <<<EOD
   This is  Heredoc string quoting example
EOD;
echo $var;
?>

 Nowdoc Quoted:

Nowdocs are to single-quoted strings what heredocs are to double-quoted strings.

$var = <<<'EOD'
    Example of  Nowdoc String.
EOD;
echo $var;
?>

This is the four way of string specification in PHP

What is stdClass Object in PHP

PHP stdClass Object means the “Standard Predefined Classes ”. Generally it is generic empty class. And it is very effective way to use generic data object in your code. And it is Predefined class so no need to initialization.

Using stdClass you can create an object and assign data into it without having to formally define a class. let see how

Creating an Object

<?php
    //Creating an object ($ClassName)
    $ClassName = new stdClass();

    //Assigning data into Object ($ClassName)
    $ClassName->Name = "Your Name";
    $ClassName->Address = "Your Address";
    $ClassName->Cell = "Your Cell No";
?>

Print The Object

<?php
    print_r($ClassName);
?>

Output

stdClass

SO you guys see how to assign data into stdClass and out or that data. It is possible to convert this object into array. You can do that by using type casting.

 

 

How to install WordPress

WordPress is the most attractive and very much user friendly content management system. WordPress is pretty easy to use . It has some attractive feature. People of none IT can easily install it and manage their website. In this tutorial I am gonna show you “How to install WordPress”

Step 1 : Download the latest version of Wordpree from wordpress.org and extract the file.

Step 2 : Copy the file into your htdocs directory. Rename your extracted file into your chosen name (e.g wordpress).

Step 3 :  Type localhost/wordpress in your browser and you will see this image. Then click the “Create a Configuration File

wordpress_ins_1

 

 

Step 4 : The you will see another page and click “Lets Go” then you will see this page.

 

wordpress_ins_2

Now you have create a database and replace database name ,username,password with your database name .username,password. then submit it and you will see another page. Just press “Run the install“.

Step 5 :  Now you have to set your “Site Title, Username,Email,Password and press “install wordpress

Step 6 : Finally you have to type “localhost/wordpress” in your browser to visit your website. and “http://localhost/wordpres/wp-admin” for your admin panel. Login into your admin panel with you give Username and Password which is set in step 5

This is the simple step for install the wordpress. How you guys are enjoy it.

 

How to Install CakePHP

CakePHP is the most simpler, faster, powerful open source framework. it is written by PHP (Object Oriented)” . It’s follow the MVC (Model View Controller) pattern. Which is most interactive part of it. You can easily build your application require less coding. Let’s I am going to introduce how to Install CakePHP with some step.

Step 1 : Download latest version of CakePHP from cakephp.org and extract the downloaded file.

Step 2 : If you downloaded 2.2.3 then you will see cakephp-2.2.3 >> cakephp-2.2.3 this folder structure. You need to rename the second cakephp-2.2.3 with your desire name like cake.

Step 3 : Copy the folder and paste into your htdocs folder (your local server). and type in your browser “http://localhost/cake” (If you rename with cake name, otherwise write http://localhost/your_desire_name). Then you will see something like this image.

cake_install

Step 4 : After that you need to change the “Security Salt” and ”Security ChiperSeed”. To that go to app/config and open the core.php and you will see this code.

* A random string used in security hashing methods.
*/
    Configure::write('Security.salt', 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi');

/**
* A random numeric string (digits only) used to encrypt/decrypt strings.
*/
    Configure::write('Security.cipherSeed', '76859309657453542496749683645');

You need to change those security.salt and security.CiperSeed. Like this

* A random string used in security hashing methods.
*/
    Configure::write('Security.salt', 'fesfsdfsdfsesfdlkfmma;lfm,as,mfcesR2G0FgaC9mi');

/**
* A random numeric string (digits only) used to encrypt/decrypt strings.
*/
    Configure::write('Security.cipherSeed', '7685645641654165416546549309657453542496749683645');

Step 5:  After that rename the file dsatbase.php.default to database.php . For doing that you have to go app/config and you will see the database.php.default

Step 6 : Then create a database and then open database.php file and you will see this code.

public $default = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'user',
        'password' => 'password',
        'database' => 'database_name',
        'prefix' => '',
        //'encoding' => 'utf8',
    );

change the host,login,password,database name with your host,user, password,database name.

Step 7: That’s fine, you have done all things. Now refresh you browser and see you browser.