23Oct/23

Debug PHP API function using output to a file

Add ob_start(); function from where you want to put output to a file

Use following function to get output of code execution :

$contents = ob_get_contents();

Put the content to a file:

file_put_contents(<FILE PATH WITH NAME>,$contents);

Clean the output with using ob_end_clean();

Example:

…..

ob_start();

print_r($data);

$contents = ob_get_contents();

file_put_contents(‘var/www/html/dumprequest.txt’,$contents);

ob_end_clean();

05Sep/23

Install Apache2, PHP, MySql ubuntu 20.04

Step 1 – Installing Apache

Update the package list for upgrades and new packages:

sudo apt-get update

Now Apache can be installed.

sudo apt-get install apache2

Step 2 – Installing MySql

Install MySql Server

sudo apt-get install mysql-server

After MySql has been installed, you will need to set the root password of the database and secure it using the following command :

sudo mysql_secure_installation

You will be presented a screen where MySQL asks whether you would like to activate the VALIDATE PASSWORD PLUGIN. For now, keeping things simple, type no.

In the next type the root password of your choice. Confirm it again.

In the next screen MySql will ask whether to remove anonymous users. Type yes Disallow root login remotely?

Type No Remove test database and access to it?

Type Yes Reload privilege tables now?

Type Yes

After the password has been set you can check the whether MySQL is working correctly by logging into the database with the command :

sudo mysql -u root -p

Password is the same that was set in the previous step.

Step 3 – Installing PHP with Common Extensions

Now install PHP and commonly used PHP extensions by using the following command :

sudo apt-get install php libapache2-mod-php php-mysql php-curl php-gd php-json php-zip php-mbstring

After PHP has been installed, restart Apache.

sudo service apache2 restart

04Aug/23

Install PHP 8.2 on Ubuntu 22.04

Update System

sudo apt update && apt upgrade -y

Install PHP 8.2

sudo apt install php8.2 -y

If system doesn’t found this package, then add Ondrej repository :

sudo add-apt-repository ppa:ondrej/php

Update system:

sudo apt update

Install main extensions of PHP 8.2

sudo apt-get install -y php8.2-cli php8.2-common php8.2-fpm php8.2-mysql php8.2-zip php8.2-gd php8.2-mbstring php8.2-curl php8.2-xml php8.2-bcmath

Check PHP installed version:

php --version

Check installed extensions:

php -m

or

php8.2 -m (If you are using multiple versions of PHP)
04Aug/23

How to start/stop/restart/reload php-fpm on Ubuntu/Debian Linux

For PHP 7.x

sudo service php7.x-fpm start
sudo service php7.x-fpm stop
sudo service php7.x-fpm restart <- restart it
sudo service php7.x-fpm reload <-
Reload configuration without start

For PHP 8.x

sudo service php8.x-fpm start
sudo service php8.x-fpm stop
sudo service php8.x-fpm restart <- restart it
sudo service php8.x-fpm reload <-
Reload configuration without start

08Jun/23

Accept only numbers with limit in UITextField

Add UITextField in your controller or view

class UITextViewController: UIViewController{
@IBOutlet weak var mtnGSMtxt: UITextField!
override func viewDidLoad() {
mtnGSMtxt.delegate = self
super.viewDidLoad()
}
}

Now extend defined class with UITextFieldDelegate:

extension UITextViewController : UITextFieldDelegate{
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//For GSM numer validation
let maxLength = 12    
if textField == mtnGSMtxt {
        let gsmLength = Int(mtnGSMtxt.text?.count ?? 0)
        if gsmLength >= 0 && gsmLength < maxLength{
            let allowedCharacters = CharacterSet(charactersIn:"0123456789 ")//Here change this characters based on your requirement
            let characterSet = CharacterSet(charactersIn: string)
            return allowedCharacters.isSuperset(of: characterSet)
        }else{
            return false
        }

    }
    return true
}
}

05Jun/23

Importing and Exporting MySQL database Command Line

Exporting from MySQL :

mysqldump -u mysql_username -p database_name > output_file.sql

You can put absolute or relative path of output_file.sql

Enter the database password.

Eg. : mysqldump -u root -p blueheri > blueheri.sql

Importing to MySQL :

mysql -u username -p database_name < input_file.sql

You can put absolute or relative path of input_file.sql

Enter the database password.

Eg. mysql -u root -p blueheri < blueheri.sql