Author Archives: Online Web Solution

PHP GET VS POST

Category : PHP

In PHP, one can specify two different submission methods for a form. The method is specified inside a FORM element, using the METHOD attribute. The difference between METHOD=”GET” (the default) and METHOD=”POST” is primarily defined in terms of form data encoding. According to the technical HTML specifications, GET means that form data is to be encoded (by a browser) into a URL while POST means that the form data is to appear within the message body of the HTTP request.

 

Get Post
History: Parameters remain in browser history because they are part of the URL Parameters are not saved in browser history.
Bookmarked: Can be bookmarked. Can not be bookmarked.
BACK button/re-submit behavior: GET requests are re-executed but may not be re-submitted to the server if the HTML is stored in the browser cache. The browser usually alerts the user that data will need to be re-submitted.
Encoding type (enctype attribute): application/x-www-form-urlencoded multipart/form-data or application/x-www-form-urlencoded Use multipart encoding for binary data.
Parameters: can send but the parameter data is limited to what we can stuff into the request line (URL). Safest to use less than 2K of parameters, some servers handle up to 64K Can send parameters, including uploading files, to the server.
Hacked: Easier to hack for script kiddies More difficult to hack
Restrictions on form data type: Yes, only ASCII characters allowed. No restrictions. Binary data is also allowed.
Security: GET is less secure compared to POST because data sent is part of the URL. So it’s saved in browser history and server logs in plaintext. POST is a little safer than GET because the parameters are not stored in browser history or in web server logs.
Restrictions on form data length: Yes, since form data is in the URL and URL length is restricted. A safe URL length limit is often 2048 characters but varies by browser and web server. No restrictions
Usability: GET method should not be used when sending passwords or other sensitive information. POST method used when sending passwords or other sensitive information.
Visibility: GET method is visible to everyone (it will be displayed in the browsers address bar) and has limits on the amount of information to send. POST method variables are not displayed in the URL.
Cached: Can be cached Not Cached
Large variable values: 7607 characters maximum size. 8 Mb max size for the POST method.

PHP Errors

Category : PHP

There are 13 types of errors in PHP, We have listed all below –

  • E_ERROR: A fatal error that causes script termination.
  • E_WARNING: Run-time warning that does not cause script termination.
  • E_PARSE: Compile time parse error.
  • E_NOTICE: Run time notice caused due to error in code.
  • E_CORE_ERROR: Fatal errors that occur during PHP initial startup.
    (installation)
  • E_CORE_WARNING: Warnings that occur during PHP initial startup.
  • E_COMPILE_ERROR: Fatal compile-time errors indication problem with script.
  • E_USER_ERROR: User-generated error message.
  • E_USER_WARNING: User-generated warning message.
  • E_USER_NOTICE: User-generated notice message.
  • E_STRICT: Run-time notices.
  • E_RECOVERABLE_ERROR: Catchable fatal error indicating a dangerous error
  • E_ALL: Catches all errors and warnings.

Install Apache2 PHP Mysql Ubuntu

Category : PHP

Install Apache2

sudo apt-get update
sudo apt-get install apache2

Install PHP 7

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get purge php5-fpm
sudo apt-get install php7.0-cli php7.0-common libapache2-mod-php7.0 php7.0 php7.0-mysql php7.0-fpm php7.0-mysql
sudo apt-get install php7.0-cgi php7.0-dbg php7.0-dev php7.0-curl php7.0-gd
sudo apt-get install php7.0-mcrypt php7.0-xsl php7.0-intl
sudo service apache2 restart

verify: php -v

Install Mysql 5.7

wget https://dev.mysql.com/get/mysql-apt-config_0.6.0-1_all.deb
sudo dpkg -i mysql-apt-config_0.6.0-1_all.deb

On config screen: Mysql server -> mysql 5.7- -> apply

 sudo apt-get update
 sudo apt-get install mysql-server-5.7 

verify: mysql –version

sudo service apache2 restart

Aditional Tools

Git

sudo apt-get install git

Composer

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Zip / Unzip

sudo apt-get install zip unzip

PhpMyAdmin

cd /usr/share
sudo su
wget https://files.phpmyadmin.net/phpMyAdmin/4.5.4.1/phpMyAdmin-4.5.4.1-all-languages.zip
unzip phpMyAdmin-4.5.4.1-all-languages.zip
mv phpMyAdmin-4.5.4.1-all-languages phpmyadmin
chmod -R 0755 phpmyadmin

vi /etc/apache2/sites-available/000-default.conf

After DocumentRoot /var/www/html

Alias /phpmyadmin "/usr/share/phpmyadmin/"
<Directory "/usr/share/phpmyadmin/">
     Order allow,deny
     Allow from all
     Require all granted
</Directory>
service apache2 restart

Configuring

enable mod_rewrite

sudo a2enmod rewrite
sudo service apache2 restart

rewrite for .htaccess

sudo vi /etc/apache2/apache2.conf 

AllowOverride None to AllowOverride All

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

sudo service apache2 restart

Enable PHP mail() function on Ubuntu

Category : MYSQL , PHP

Introduction

This is a short guide covers how to enable the mail() function in PHP on Ubuntu.

Here are the steps I took to get sendmail working on my Ubuntu server.

Step 1: Install sendmail

The first thing you need to do is install the sendmail package. Do this by running the following command in your terminal:

$ sudo apt-get install sendmail

Sendmail might already be installed, but just in case, enter above into your terminal:

Step 2: Configure sendmail

After the previous command finishes installing the sendmail package, you need to configure it. You can do that by running the following command, which will configure the mail server:

$ sudo sendmailconfig

You need to choose Yes to every question or otherwise configure it as you see fit.

Step 3 (Optional): Edit hosts file

To edit your server’s hosts file, access it by entering the following command into the terminal:

$ sudo vim /etc/hosts
for example 0.0.0.0  abcd.com

Step 4: Restart Web Server

At this point emails sent via PHP’s mail() function should be sending successfully without much delay. It is not necessary to restart server, but let’s restart server just for good measure.

For Apache Server, you can enter the following command to restart the server.

$ sudo service apache2 restart

For Nginx Server, you can enter the following command to restart the server:

$ sudo service nginx restart

That’s it! You can now use the mail() function in your PHP app.. I hope this solved the problem for you.


PHP oops concepts

Category : PHP , Uncategorized

Class vs Object

Class is like your house blueprint. Before your house is constructed, there is a house blueprint. It is not an actual house, but a plan how this house will look like, how many rooms it will have and so on. Then the house will be constructed by following the blueprint exactly. In this analogy, the house blueprint is a class and your actual house is an object. We can have unlimited objects of a class, just like we can build unlimited exact houses by following the same house blueprint.

A few key points to keep in mind:

  • Class is generic, whereas Object is specific
  • Class defines properties/functions of an Object
  • Object is an instance of a Class
  • You can instantiate an object, but not a Class

 

PHP Class

Class is consist of properties and methods. Below is a PHP class. In this simple class. $postCode is a property and ringBell() is a method. They are all prefixed with a visibility keyword (public).

Class House {

	public $postCode = “560121”;

	public function ringBell() {
		echo “Ding Dang Dong”;
	}
}

To instantiate an object of a class, use the keyword new as below:

$house = new House();

Visibility

Each method and property has its visibility. There are three types of visibility in PHP. They are declared by keywords publicprotected and private. Each one of them controls how a method/property can be accessed by outsiders.

Public: It allows anyone from outside access its method/property. This is the default visibility in PHP class when no keywords are prefixed to a method/property.

Protected: It only allows itself or children classes to access its method/property.

Private: It does not allow anyone except itself to access its method/property.

Inheritance

It lets subclass inherits characteristics of the parent class. Parent class decides what and how its properties/methods to be inherited by declared visibility.

class Shape {
	public function name() {
		echo "I am a shape";
	}
}

class Circle extends Shape {

}

$circle = new Circle();
$circle->name(); // I am a shape

The key word here is extends. When Circle class extends from Shapeclass, it inherits all of the public and protected methods as well as properties from Shape class.

Polymorphism

The provision of a single interface to entities of different types. Basically it means PHP is able to process objects differently depending on their data type or class. This powerful feature allows you to write interchangeable objects that sharing the same interface.

interface Shape {
	public function name();
}

class Circle implements Shape {
	public function name() {
		echo "I am a circle";
	}
}

class Triangle implements Shape {
	public function name() {
		echo "I am a triangle";
	}
}

function test(Shape $shape) {
	$shape->name();
}

test(new Circle()); // I am a circle
test(new Triangle()); // I am a triangle

Above example, test(Shape $shape) function declares(type hints) its only parameter to be Shape type. This function is not aware of Circle and Triangle classes. When either class is passed to this function as a parameter, it processes respectively.

Encapsulation

Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties’ direct access to them. It is a concept that motivates us to think through a method/class responsibility and hide its internal implementation/details accordingly. This will make it easy to modify the internal code in a long run without affecting other part of the system. Visibility is the mechanism for encapsulation.

class Person {
	private $name;

	public function setName($name) {
		$this->name = $name;
	}

	public function getName($name) {
		return $this->name;
	}
}

$robin = new Person();
$robin->setName('Robin');
$robin->getName();

In this simple class above. Field $name is encapsulated (private). Users of the class is not aware how $name is stored in Person class. Right now the $name is stored in memory. We can modify internal code of Person class to store it to a flat file or event a database. Users of the class will not need to change any code, in fact they do not even know how $name is stored, because that is encapsulated and hided from them.

Abstraction

Abstraction is the concept of moving the focus from the details and concrete implementation of things, to the types of things (i.e. classes), the operations available (i.e. methods), etc, thus making the programming simpler, more general, and more abstract. It is like a generalization instead of a specification.

class TV {
	private $isOn = false;

	public function turnOn() {
		$this->isOn = true;
	}

	public function turnOff() {
		$this->isOn = false;
	}
}

$tv = new TV();
$tv->turnOn();
$tv->turnOff();

Code above defined an TV class. We can not do much with it except turning it on and off. The class TV is an abstraction of a real TV in a very simple use case.

Interface vs Abstract class

Interface

Interface declares what methods a class must have without having to implement them. Any class that implements the interface will have to implement details of those declared methods. Interface is not a class, so you can not instantiate an interface. It is useful when you need to enforce some classes to do something.

interface Vehicle {
	public function startEngine();
}

class Car implements Vehicle {
	public function startEngine() {
		echo "Engine Started";
	}
}

Vehicle is an interface with a declared method startEngine()Carimplements Vechicle, so it has to implement what startEngine() method does.

Abstract class

Abstract class is able to enforce subclasses to implement methods similar to interface. When a method is declared as abstract in an abstract class, its derived classes must implement that method.

However it is very different from interface. You can have normal properties and methods as a normal class, because it is in fact a class, so it can be instantiated as a normal class.

abstract class Vehicle {
	abstract public function startEngine();

	public function stopEngine() {
		echo "Engine stoped";
	}
}

class Car extends Vehicle {
	public function startEngine() {
		echo "Engine Started";
	}
}

Vehicle is an abstract class. Car extends Vechicle, so it has to implement what startEngine() method does, because this method is declared as abstract. However Car does not have to anything with method stopEngine(), it is inherited as a normal class does.