How to keep single session per user in Laravel

Standard

By now, most of you know about the PHP framework Laravel. It’s beautiful and those who haven’t tried this yet, please feel free to try it. I’m sure that you will love it. Coming to the requirement, our project required single session per user, meaning a user’s login process would clear all other sessions (or last session to be precise) from other devices or browsers for the same user. After a few study, here’s what I did to resolve it.

Added the following method in LoginController.php

/**
 * Swap a user session with a current one
 * 
 * @param \App\User $user
 * @return boolean
 */
protected function swapUserSession($user)
    {
        if (!($user instanceof \App\User)) {
            return false;
        }

        $new_session_id = Session::getId(); //get new session_id after user sign in
        $last_session = Session::getHandler()->read($user->last_session_id); // retrive last session

        if ($last_session) {
            Session::getHandler()->destroy($user->last_session_id);
        }

        $user->last_session_id = $new_session_id;
        $user->save();

        return true;
    }

Added a column ‘last_session_id’ in users table and in user model, added ‘last_session_id’ in the ‘fillable’ array.

That’s all. If you know any better method to handle this, please feel free to leave a comment.

Thanks for reading this post.

Advertisements

How to solve PHP warning: mkdir(): No such file or directory

Standard

I was on a Linux server and working on a Laravel project. All were working fine. Suddenly, we found that all mail related functionalities were crashing. From the log, we saw the SwiftMailer was trying to create cache in default /tmp folder and crashes with a warning saying:

warning: mkdir(): No such file or directory

I tried to run a simple PHP script that would create a folder or file under /tmp folder. It also produced the same warning.

PHP Code:

<?php
mkdir('/tmp/test');

To solve the issue, I’ve changed the TMPDIR environment variable in the boot() method of app/Providers/AppServiveProvider.php.

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        /**
         * Somehow PHP is not able to write in default /tmp directory and SwiftMailer was failing.
         * To overcome this situation, we set the TMPDIR environment variable to a new value.
         */
        if (class_exists('Swift_Preferences')) {
            \Swift_Preferences::getInstance()->setTempDir(storage_path().'/tmp');
        } else {
            \Log::warning('Class Swift_Preferences does not exists');
        }
    }

Please make sure that the new “tmp” folder location is writable by the web server.

This is the quickest solution that we had. For any other PHP frameworks, you can set a new TMPDIR location to solve this issue. If anyone knows the exact issue that causes the warning, please feel free to leave a comment.

Thank you for reading the post.

OpenCart has error in core mysqli client library

Standard

Today, while studying the architecture and code of OpenCart v1.5.5.1, I just found an issue with it and wanted to bring in everyone’s notice.

Open the file system/database/mysqli.php and look for the method “query”. It has an unmatched if…else statement. This also proves that OpenCart is not using “mysqli” library. They are still using “mysql” library for database communication.

Continue reading

How to bypass Page Cache created by W3 Total Cache

Standard

In our recent WordPress plugin development, a bridge between Invision Power Board (IPB) forum software and WordPress, we needed to handle the user login through IPB and get the logged in user details in the WordPress. Post getting the IPB user session in the WordPress, our plugin opened up the comment section in the WordPress that would allow the comments to be posted in IPB thread. This is a very basic overview of the plugin and it can be seen working LIVE at Red94.net. The plugin can do a lot more. If you have any questions about the plugin, you can contact us through our official website, AxeFinch.

Continue reading

How to delete error_log files from the server periodically

Standard

At times, I get inquiries for deleting error_log files from the server as they are consuming space. I strongly recommend not to delete the files. Those files contains critical information those cannot be seen through a normal debugging process. Trust me, error_log files are life saver. But, still I would like to publish a small PHP script here in order to delete the error_log files from the server. Continue reading

WordPress plugin “sharebar-reloaded” pre-release note

Standard

I’m excited to let you know that I’ve completed working on WordPress plugin “sharebar reloaded” that is the descendent of it’s popular “sharebar” plugin. While working on my client’s site Red94.Net, I encountered a weird issue with the “sharebar” plugin when it stopped working with my “tabbed widgets reloaded” plugin installed in the site. So, I took a look of the code and found some interesting places where I could improve the code. Continue reading

WordPress plugin “tabbed-widgets-reloaded”

Standard

I’m pleased to introduce the popular “tabbed widgets” with support of WordPress 3.3.1 and few new features. It’s available at http://wordpress.org/extend/plugins/tabbed-widgets-reloaded/

Various users have reported that the “tabbed-widget” plugin breaks on WordPress 3.3.1 and added a work around in some way to make it work. I’ve changed the plugin code in order to make it work for WordPress 3.3.1 and also added a functionality to add custom CSS so the core CSS does not require a update. Continue reading