PHP – Stop Script Execution After Specific Time Limit

Do you have a long-running PHP script or just some PHP script you want to ensure doesn’t exceed a certain time limit, while exiting the script safely? Simply use the following function in your script. This is an example of an entire working script. Ordinarily, this script would run for 30 seconds, but thanks to this function, it will exit after 3 seconds.

<?php

$starttime = time();
$runtime = "3"; // How long script should run (in seconds)

for($x=1; $x <= 30; $x++)
    {
    sleep(1);
    echo "Second $x of script has passed!<br>";
    $breaktimer = breaktimer($starttime, $runtime);
    if($breaktimer == "1") { echo "The script has been running for $runtime seconds. Time to stop the script!"; break; }
    }

function breaktimer($starttime, $runtime)
    {
    $breaktimer = "0";
    $now = time()-$starttime; // Calculate how many seconds script has been running for until now
    if ($now >= $runtime) // Return 1 if script has run for allotted run time
        {
        $breaktimer = "1";
        }
    return $breaktimer;
    }

?>

Enjoy!

Fix phpList Error: IMAP is not included in your PHP Installation

It is hard to find a newsletter and email marketing software as nice as phpList. If you have installed this wonderful software on your own VPS/Dedicated server, and are getting the phpList error, Error: IMAP is not included in your PHP Installation, continue reading to learn how to easily fix this error.

phpList Process Bounces Error IMAP PHP not Installed

This error means you do not have the IMAP PHP extension installed. To install it, simply SSH into your server and type in the following:

apt-get install php5-imap

When asked if you want to continue, type Y and hit Enter.

Then, type in the following:

php5enmod imap

Now, restart your PHP processor by typing in the following:

service php5-fpm restart

Finally, restart your Nginx web server by typing in the following:

service nginx restart

If you are using an Apache web server, restart it by typing this instead:

service apache2 restart

Import MySQL Database File via Command Line Ubuntu Linux

Importing a MySQL database from the terminal in Linux is very easy. All it takes is one simple line:

mysql -u username -p database_name < myDatabase.sql

Make sure you enter in your MySQL username in place of username. In place of database_name, enter in the name of the database you are importing in to. After you enter this line of code via SSH, you will be prompted for the password of the MySQL user. Go ahead and enter in the password, and your database should get imported.

Execute PHP Code & Scripts / Files in Bash / SSH & Cron

Execute PHP Code in Bash/SSH and Cron

To execute PHP code via shell, type the following code after you SSH into your server:

php -r 'echo "Hello World";'

The above code simply outputs Hello World, but feel free to use your own PHP code between the apostrophes in the above example.

Execute PHP Script/File in Bash/SSH and Cron

To execute a PHP script/file via shell, you must first run the following command to get the PHP directory:

which php

The output will be what you want. It should be something like this:

/usr/local/bin/php

You would then run a PHP file by typing in the following:

/usr/local/bin/php /home/username/public_html/filetoexecute.php

If the output of the which php command did not give you the correct directory, then you need to know what version of PHP your web server is using. To find out, type the following code after you SSH into your server:

php -v

The above code will tell you what version of PHP you are using on your web server. If you are using PHP 5.4, you would execute a PHP file by typing in the following via SSH:

/opt/php54/bin/php /home/username/public_html/filetoexecute.php

If you have a different version of PHP than 5.4, you will use a slightly different command. Simply substitute the 4 with the relevant number.

The following are locations for different PHP versions:
PHP 5.2: /opt/php52/bin/php
PHP 5.3: /opt/php53/bin/php
PHP 5.4: /opt/php54/bin/php
PHP 5.5: /opt/php55/bin/php
PHP 5.6: /opt/php56/bin/php

If that command is not working for you, try looking for the correct location using the following command via SSH:

whereis php