Categories
PHP

PHP Foreach Loop Benchmark – Optimize your code & Keep the server happy!

Keep the internet as energy efficient as possible and the easiest ways to reduce your carbon footprint is to optimize your code. The future of humanity and the planet is in your hands! The execution speed is very clear from the image below, each variant is sorted according to the speed of execution, the first […]

Categories
PHP Useful

Get HTTP Status Code – PHP Functions

Below are some useful solutions (php functions) for HTTP Status Codes returned in the required page header by servers when loading a web page. global $http_status_code; $http_status_code = [ // ~~~ officially known https codes 100 => ‘Continue’, 101 => ‘Switching Protocols’, 102 => ‘Processing’, // WebDAV; RFC 2518 103 => ‘Early Hints’, // RFC 8297 […]

Categories
PHP Useful

Convert String Array Associative to Array Type – Eval and array index as variable

Recently I encountered a small problem with a library downloaded from github or sourceforge (I don’t remember exactly), but quite old, with many functions used on the old style, many of them already deprecated. Another problem encountered was the evaluation of a string and its transformation into an array, the internal representation being of a […]

Categories
PHP Useful

Gets the current working directory in PHP

using getcwd() – gets the current working directory – php function echo getcwd(); using dirname() php function echo dirname(__FILE__); echo dirname($_SERVER[PHP_SELF]); using basename() php function  – (PHP5+) echo basename(__DIR__) so if you want to use a file with require_once, include or require, the usage syntax can be the following: require_once dirname(__FILE__) . ‘/folder/script.php’;

Categories
PHP Useful

Get HTTP Status Code from Header – PHP

1. First of all we have to: deactivate the SSL verification because there are cases in which the site is redirected from a domain where the SSL has expired! limit the number of redirects, so as not to enter an endless loop initialize User-Agent with a real value , otherwise there is the possibility that […]

Categories
PHP Useful

Get last URL after following HTTP redirections in PHP – get_headers

1st. step: Disable https/ssl verify & set some headers … $context = stream_context_create( [ ‘ssl’ => [ ‘verify_peer’ => false, ‘verify_peer_name’ => false, ], ‘http’=>array( ‘max_redirects’=>10, ‘ignore_errors’=>0, ‘method’=>”GET”, ‘header’=>”Accept-language: en\r\n” . “Cookie: \r\n” . “User-Agent: Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19\r\n” ) ]); default method is GET, […]

Categories
Linux PHP Servers Useful

Get PHP MAJOR,MINOR and RELEASE Version Number – Linux Server

To find out the current version of php active on the server, there are basically two simple solutions and both use the php command with additional parameters. 1. php + grep + cut @ php -v | grep ^PHP | cut -d’ ‘ -f2 7.4.13 2. php +  internal constant from php itself @ php […]

Categories
Linux PHP Servers Useful

PHP-FPM Service – How to start,stop,reload – Ubuntu Linux Severs

Used with many web servers like Apache, Nginx, and other, PHP-FPM is a FastCGI process manager for PHP. Sometimes we need to restart the server for various reasons: changing settings for example, and depending on what linux we have on the server there are some variations in the commands used. Ubuntu/Debian Linux (Ubuntu Linux 16.04+; 18.04; […]

Categories
Linux PHP Servers

Where is the php.ini configuration and enabled PHP modules? – Ubuntu Server

PHP configuration folders is usually in /etc/php/ and the installed version, in my case is in /etc/php/7.4/ The active version can be found with the following command in Ubuntu (ubuntu 20.04 for my case): @ php -version PHP 7.4.13 (cli) (built: Dec 10 2020 08:08:23) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) […]

Categories
PHP Useful

Add and Remove Query String Variables in PHP – The Easy Way

PHP offers two functions that are very useful for us in generating or modifying a link: parse_str – automatically converts all query parameters into an array http_build_query – generates a URL-encoded query string from the associative (or indexed) array Below is a function that can help you add, delete or modify a parameter in a […]