Category: "PHP"

Find the PHP session files that are older than 24 minutes

sudo find /var/lib/php/session -mmin +24 -type f -exec ls -l {} \;

Yes, that's the whole post

Apache 2.4 virtual host specific PHP-FPM error logs

If you are using PHP-FPM with Apache and you would like to separate the error logging by user, directory or virtual host, you can use the ProxyFCGISetEnvIf directive

In a server level Apache .conf file


<Directory /home/user/public_html>
ProxyFCGISetEnvIf "true" PHP_ADMIN_VALUE "error_log=/var/log/php-fpm/user/error.log"
</Directory>

In this case, the error log for user would be

/var/log/php-fpm/user/error.log

Set up the ACL (AMI 2 Linux)

setfacl -m u:user:x /var/log/php-fpm
setfacl -m u:user:rx /var/log/php-fpm/user
setfacl -d -m u:user:r /var/log/php-fpm/user

Test it with

sudo su user
more /var/log/php-fpm/user/error.log

Credit to:

Apache 2.4 virtual host specific PHP-FPM error logs
Comment from PHP.net (documentation)

https://www.php.net/manual/en/install.fpm.configuration.php#123335

AMI - upgrade PHP from 7.1 to 7.3

AMI - upgrade PHP from 7.1 to 7.3
PHP logo

Don't do this on a production system

I ran this on an Amazon Linux AMI - it's probably fine on CentOS, etc.

Get all the PHP 7.1 packages and make a file called php. You might have to change the .x86_64 to .i386/.i686

sudo yum list installed php71* | grep php | cut -f1 -d' ' | tr -d '.x86_64' | tr "\n" ' ' | sed "s/71/73/g" > php

Remove PHP 7.1 (remember I said not to do this on a production machine)

sudo yum remove php71*

Now edit your php file and add

sudo yum install at the beginning of the list of packages

It should look something like this

sudo yum install php73 php73-cli php73-common php73-gd php73-imap php73-intl php73-json php73-mbstring php73-mysqlnd php73-opcache php73-pdo php73-pecl-apcu php73-pecl-igbinary php73-pecl-memcached php73-pgsql php73-process php73-soap php73-ml

Run the php file with

source php

And, if you are using memcached, run this too

sudo yum install php7-pear php73-devel
sudo pecl7 install memcached
sudo pecl7 update-channels

Add this into php.ini somewhere ...

extension=memcached.so

Restart Apache

sudo apachectl restart

Bask in the glory

Upgrade PHP 5.5 to PHP 7.1 on CentOS 6.9 - Recklessly

I wasn't planning to upgrade PHP today, but in order to use Symfony 4.0, I had to.

First, I wanted to get all the PHP RPMs

sudo yum list installed php55u* | grep php | cut -f1 -d' ' | tr -d '.i686' | tr "\n" ' ' | sed "s/55/71/g" > php

Next, I removed all the PHP 5.5 RPMs:

sudo yum remove php55*

Then I edited the output file (php) and added a sudo yum install at the beginning of the file

So I could use

source php

There were a few more RPMs I needed, after roaming about the web for a bit, these were the commands that I ran

sudo yum install pear1u
sudo yum install php71u-json
sudo yum install libmemcached-devel
sudo pecl install memcached

The recklessly part of this is that I confess I did not check ... anything. My existing Symfony 3.3.10 application comes up and lets me log in. I haven't checked more than that.

Good luck!

Web application session timeout code

Session timeout warning for a web application with a variety of page layouts and frequent use of multiple tabs.

Nutshell explanation - ping the server, if you get a 403, show a huge red bar across the top of whatever page pinged.

Result:

  • It is immediately apparent the tab has timed out
  • If one tab times out, it does not disrupt the others
  • There is a link to help the user log in again

function sessionPing() {
    var pinger;
    function doPing() {
        var pReq = new XMLHttpRequest();
        pReq.addEventListener("load", function () {
            var sessionExpired, sessionExpiredMessage;
            var reloadLink;
            if (this.status === 403) {
                clearInterval(pinger);
                sessionExpired = document.createElement("div");
                // Sometimes an inline style is really the best solution
                sessionExpired.setAttribute("style","display:block; width: 100%; line-height: 2.5em; position:absolute; top:0; z-index:10000; text-align: center; background-color: #f00; color: #fff; font-family: 'Trebuchet MS',sans; font-size: 1.5em; font-style: italic");
                sessionExpiredMessage = document.createTextNode("Session expired ");
                sessionExpired.appendChild(sessionExpiredMessage);
                reloadLink = document.createElement("a");
                reloadLink.href = location.href;
                reloadLink.textContent = "Click to Continue";
                reloadLink.setAttribute("style","font-size:0.7em;color:#ddd");
                sessionExpired.appendChild(reloadLink);
                document.body.insertBefore(sessionExpired, document.body.firstChild);
                document.title = "Session expired";
            }
        });
        pReq.open("GET", "/ping.php");
        pReq.send();
    }
    pinger = setInterval(doPing, 30000);
}
sessionPing();


<?php
session_start();
if (empty($_SESSION['user_id'])) {
    header('HTTP/1.1 403 Forbidden');
    exit;
}
header('HTTP/1.1 201 No content');
exit;

One may argue that the page should be cleared, in this case, I chose to leave it up so people can copy the content off.

Confession: I didn't test this code, it is an extract.

This post courtesy of Game Creek Video