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.

What is Static variable in PHP ?

Normally when a function is terminates then all the variable inside the function loose their value. But if we want to hold those value for the feature works, then we need do use static variable. Generally those variables which holds the values are called static variables inside a function. To doing this you must write static keyword just before variable. let’s see two example one is normal variable and another is static.

Variable without static

<?php
    function test_variable()
    {
        $x = 1;
        echo $x;
        $x++;
    }
    test_variable();
    echo "<br>";
    test_variable();
    echo "<br>";
    test_variable();
?>

Output

1
1
1

 

Variable with static

<?php
    function test_count()
    {
        static $x=1;
        echo $x;
        $x++;
    }
    test_count();
    echo "<br>";
    test_count();
    echo "<br>";
    test_count();
?>

Output

1
2
3

SO, Hope you guys enjoy it…..

Easy way to start java script

Start java script code just need to follow some simple step. Java Script is basically scripting language. Which is called a client-side scripting language. Because of the all operation of java script occurred in users computer. Today millions of website used java script to make their website more attractive and more professional. It’s help to make very attractive look of your web page.
You can start Java Script with follow some simple step.

Step 1: Create a html file in your editor/IDE (You can write code in html file in body section)

Step 2: Then you need to initialized java script where you need to write code. To do this write/copy this code

<html>
    <head>
        <title>My First Java Script Code</title>
    </head>
    <body>
        <script type="text/javascript">
            // You need to write java script code here
        </script>
    </body>
</html>

 

Step 3:  After the initialization you have to write your code inside the ”<script type=”text/javascript”> Write code here </script>”. I am going to show an simple example .

 

<html>
    <head>
        <title>My First Java Script Code</title>
    </head>
    <body>
        <script type="text/javascript">
           document.write('This is my First Java Script Code');  // This is my first Java Script Code
        </script>
    </body>
</html>

 

 

Those are very basic step to start java script. I am trying to write some post about java script in feature. Hope you guys are enjoy it.

What is PHP Ternary Operator

PHP Ternary Operator is the Shorthand of if/else statement. It’s is important to know “What is it, How to use it ” for php programmer.

It’s simply said that the shorthand of if/else statement. It’s rally easy to use and reduce you code.

Ternary Operator using “(condition) ? (return true value) : (return false value)” for execute the statement.It’s called Ternary Operator because it’s use three operands, one condition, one result for true, one result for false. Hear is an example.

<?php
    $age=10;
    $Status= ($age<18)? “Child”:”Adult”;
    echo $Status;
?>

Output

Child

NB:  The most important thing of using it reduce your code ,save the executing time.Makes maintaining code quicker, easier.

 

Image Uploader Script in PHP

Recently I am developing my component library to make my library rich. Because it’s reduce my production time. For this reason I have developed the image uploading class. Almost every application need image uploading part. So that I have decided to make a class to for image uploading. Which will is very easy to use and no need to extra settings. Just copy the script and use it. I have made it open source so anyone can use it.

<?php
/*
* Image Upload Class
*/
class ImageUpload
{
    /**
     * @param null $Path         Custom path where uploaded image will be stored
     * @param null $MaximumSize  Custom Maximum size in (Bytes) will be passed.
     * @param array $AllowedType Custom extension type (JPG|GIF|PNG)
     * @param null $Option       Extra options as array where we can define 'Image File Name', blah blah blah
     *
     * @return string Here return/output will be a string (Message)
     */
    public function ImgUpload($Path = null, $MaximumSize = null, array $AllowedType = null, $Option = null)
    {
        if (isset($_POST['submit'])) {
            if (!empty($_FILES['ImgName']['name'])) {
                $ImageFile = $_FILES['ImgName'];
                if ($this->CheckSizeLimit($ImageFile, $MaximumSize)) {
                    if ($this->CheckImageType($ImageFile, $AllowedType)) {
                        if ($this->CheckPath($Path)) {
                            if ($this->CheckPermission($Path)) {
                                if (move_uploaded_file($ImageFile['tmp_name'], $Path . $ImageFile['name'])) {
                                    $result = 'Image has been Uploaded';
                                    return $result;
                                } else {
                                    return false;
                                }
                            } else {
                                return "Directory do not have write permission,";
                            }
                        } else {
                            return "Directory could not found,";
                        }
                    } else {
                        if (isset($AllowedType)) {
                            return "Please select " . implode('"/"', $AllowedType) . " Image";
                        } else {
                            return "Please select jpg/png/gif image";
                        }
                    }
                } else {
                    $result = "Image must me less then" . $MaximumSize . " byte";
                    return $result;
                }
            } else {
                $result = 'You do not select any file';
                return $result;
            }
        }
    }

    /**
     * @param $ImageFile        Image File which is image is selected for upload
     * @param null $MaximumSize Custom max image size passed
     *
     * @return bool Return true or false
     */
    function CheckSizeLimit($ImageFile, $MaximumSize = null)
    {
        if (isset($MaximumSize)) {
            if ($ImageFile['size'] < $MaximumSize) {
                return $ImageFile['size'];
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

    /**
     * @param $ImageFile         Image File which is image is selected for upload
     * @param array $AllowedType Custom extension type (JPG|GIF|PNG)
     *
     * @return bool Return true or false
     */
    function CheckImageType($ImageFile, array $AllowedType = null)
    {
        if (isset($AllowedType)) {
            $i = 0;
            while ($i < sizeof($AllowedType)) {
                if ($ImageFile['type'] == "image/" . $AllowedType[$i]) {
                    return true;
                    break;
                }
                $i++;
            }
            return true;
        } else {
            if ($ImageFile['type'] == "image/jpg" || $ImageFile['type'] = "image/png" || $ImageFile['type'] == "image/gif") {
                return true;
            } else {
                return false;
            }
        }
    }

    /**
     * @param null Custom path where uploaded image will be stored
     *
     * @return bool Return true or false
     */
    function CheckPath($Path = null)
    {
        if (isset($Path)) {
            if (is_dir($Path)) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

    /**
     * @param null Custom path where uploaded image will be stored
     *
     * @return bool Return true or false
     */
    function CheckPermission($Path = null)
    {
        if (isset($Path)) {
            if (is_writable($Path)) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

    protected function ImgSize($ImgFile = null)
    {
        return "OK";
    }
}

This is the class structure. you can get this code from github. There I have written more details about it. I hope this will be helpful for you development.