Get execution time of the script PHP

Today I’m going to give you a very useful snippet that you can use in your further projects to see the execution time of the script.The principle is pretty simple.You will need to define a variable at the start microtime() and one at the end.After that you will need to subtract the end microtime variable from the start microtime variable and thats it.Also you can set the output value to only show the first 2 decimals.

 

How to easily read lines from a text file with PHP

Today I’m going to give you a very quick and simple php snippet or function which reads every line of a .txt file and returns them into an array.For example, you have this text file called countries.txt with the following content:

Romania
Russia
Germany
Bulgaria

You can read them very easily with PHP with an integrated function called file() that requires 1 parameter, the file location of the .txt.

this will output something like:

Array ( [0] => Romania [1] => Russia [2] => Germany [3] => Bulgaria )

Thanks for reading this quick and simple tutorial.

Block multiple IP addresses with PHP

Today I’m going to give you a quick and very useful snipped on how to block multiple IP addresses with PHP.

First off you will have to implement this custom function in your code, preffered in the file that initializes the script.

After that, you need to define an array which contains the ip’s you want to block and then call out the function with the array parameter.

add this code to your header so that every page the specific user access, he will be blocked and will get this message:

Your IP(127.0.0.1) has been blocked !

Thanks for reading my mini tutorial, enjoy.

Check server status with PHP

Hi, today I’m going to give you a little snippet on testing if a server with a specific port is online or not.First of all this is going to be a function with 2 parameters, server ip and port.We are going to test if the server is online or not with the fsockopen() function that will require you to have PHP Sockets activated on your host.

to  access the function you simple replace the $ip with the domain or the ip and the $port with the port you want to check.

and thats it, thanks for following me.

How to disable errors for FSOCKOPEN

Hi, today I want to share with you a little tip on the FSOCKOPEN function.You know that when fsockopen function fails on php it always gives you a warning like this:

Warning: fsockopen(): unable to connect to google.com:8440 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ) in E:\xampp\htdocs\test.php on line 2

You can simply block only the fsockopen function warnings/ notices or another errors from that line of the code by simply adding “@” symbol in the front of the variable or function, like this:

@$fp = fsockopen(“google.com”,”8440″,$errno,$errstr,5);

or like this:

$fp = @fsockopen(“google.com”,”8440″,$errno,$errstr,5);

Hope this helps you, thanks for reading my tutorial.