Php and Python - Quick Tips

Posted on 03-04-2019 by Nadir Latif

Following are some quick tips related to Php:

Fixing timeout problems with Php scripts on Plesk

In order to allow long running Php scripts to run on a Plesk server, we need to increase the max_execution time value in the Php configuration settings. If the Plesk is running Php-FPM or PHP Fast CGI, then the following directives should be added to the file /etc/httpd/conf.d/fcgid.conf:

FcgidIOTimeout 9000
FcgidBusyTimeout 3600

The above options can also be added to the Apache settings for the domain

Php server APIs

Php may be run as an Apache module. This configuration is known as mod_php. It may also be run as a FPM server or a Fast CGI server.

In the Fast CGI configuration, the Php binary is accessed by the web server using the Fast CGI protocol.

In the Php FPM configuration, the Php is managed by a Fast CGI server called Php FPM. The webserver communicates with this Php FPM server over a TCP socket.

Fetching Gzip content with Php Curl

To fetch urls such as RSS Feeds that have been compressed with gzip encoding the following curl option can be used:

curl_setopt($ch,CURLOPT_ENCODING , "gzip");
Using Usort function for sorting Arrays

The Php usort function allows sorting an array of values using a custom callback function. The usort function takes a comparator callback function as an argument. This function takes two arguments which are the two values to compare. The strcmp function can be used inside the comparator function for comparing string values.

Php isset function

The Php function isset returns false for null values. It should not be used to check if array key exists, since some array keys may have empty value. Its better to use array_key_exists function instead

Generating random strings with array_rand and shuffle functions

The Php functions array_rand and shuffle functions can be used to generate random strings

What is the difference between a PHP interpreter and a PHP handler?

A Php Handler is an Apache module that is used by Apache to communicate with the Php Interpreter. It is basically used by Apache to handle requests for Php files. There are different types of Php Handlers. For example mod_php is an Apache module for Php. mod_fcgid allows communication with the php_fpm interpreter over a TCP or Unix socket.

The Php Interpreter is an application that executes Php code one line at a time. The output of the Php Interpreter is sent to the Php Handler. This output may be processed by other Apache modules such as mod_deflate or mod_security. The final output is sent out of the TCP socket that Apache listens on.

SuPHP is an Apache module that allows executing Php scripts with the permissions of the script owner. Normally Php scripts are executed with the permissions of the Php interpreter. SuPhp allows the Php files to have custom owner and permissions. The Php file is then executed by the same owner that owns the Php file. Its useful in shared hosting environments where Php files belonging to different users need to be executed by the same Php interpreter.

Passing command line arguments to Python script

Command line arguments can be passed to a Python script. For example the parameters passed to python script_name argument1 argument2 argument3 can be read with the sys module. The sys module provides the command line arguments. To use the sys module, we have to use the command: import sys. The command line arguments can be used as: sys.argv[0], sys.argv[1], sys.argv[2]. The number of command line arguments can be read with: len(sys.argv).

Php 7 error handling

Php 7 uses a different approach for handling errors. It uses a different Error hierarchy then Php 5. The base error class in Php 7 is Throwable. So if we want to catch all exceptions that can be raised by a given code, then we should enclose the code in try/catch block. The catch block should use the Throwable class. For example:

try {
// some php code
catch (Throwable $e) {

Executing Php code using eval

The Php eval function allows executing arbitrary Php code. It takes as argument a string containing Php code it returns null or the value returned by the Php code

Generate Url query strings using http_build_query

The http_build_query function allows generating valid url query strings from simple or complex arrays including nested arrays

Leave a Comment: