Category: Programming

  • NextCloud APCu not available for local cache

    NextCloud APCu not available for local cache

    When trying to upgrade nextcloud, I get the following error

    www-data@mail:/www/nextcloud$ php7.3 occ upgrade
    An unhandled exception has been thrown:
    OC\HintException: [0]: Memcache \OC\Memcache\APCu not available for local cache (Is the matching PHP module installed and enabled?)
    www-data@mail:/www/nextcloud$ 
    

    To fix this, you need to install APC cache.

    apt install php7.3-apcu
    

    For CLI, you need to edit PHP configuration file

    vi /etc/php/7.2/cli/php.ini
    

    at the end of the file, add

    apc.enable_cli = 1
    

    You can also enable apc for the current command with option

    php7.3 -d apc.enable_cli=1 occ upgrade
    
  • How to clear Laravel cache

    To clear the Laravel cache, run the command

    php artisan cache:clear

    You can also delete compiled files from

    rm -rf bootstrap/cache/*

    Back to Laravel

  • How to find java version

    To find the java version, run the command

    java --version
    

    Example

    boby@sok-01:~$ java --version
    openjdk 14.0.2 2020-07-14
    OpenJDK Runtime Environment (build 14.0.2+12-Ubuntu-120.04)
    OpenJDK 64-Bit Server VM (build 14.0.2+12-Ubuntu-120.04, mixed mode, sharing)
    boby@sok-01:~$ 
    
  • PHP Script to verify private key matches SSL certificate?

    OpenSSL command can be used to verify if an SSL certificate matches a private key file. You need to find the checksum for the SSL certificate and Private key, if both checksums are the same, then the key matches.

    To make this process easier, I created a PHP script to verify if the SSL certificate matches the private key provided.

    Create a file

    mkdir ~/bin/
    vi ~/bin/ssl-verify
    

    Add following content

    #!/usr/bin/php
    
    

    Make it executable

    chmod 755 ~/bin/ssl-verify
    

    To verify an SSL and key file, go to the folder where the SSL certificate and key file are present, then run the command

    ssl-verify
    
  • First C Program

    First C Program

    To create your first C Program, open any text editor, copy and paste the following code

    #include 
    
    int main() {
            printf ("Tis is my C program");
    }
    

    Save it as hello.c

    Before you can run this program, you need to compile it. To compile run

    gcc -o hello hello.c
    

    Make the program executable with

    chmod 755 hello
    

    To run

    ./hello
    

  • Codeigniter 3 session not working

    On a web application, session not working. I verified PHP session is working with script

    https://gist.github.com/serverok/8c504205ae0357e0c6488eab880a77bf

    When refreshing the script, the number start increasing, that confirms PHP session works fine on the server.

    As for Codeigniter, we need to check the session settings in file application/config/config.php

    $config['sess_driver'] = 'files';
    $config['sess_cookie_name'] = 'ci_session';
    $config['sess_expiration'] = 7200;
    $config['sess_save_path'] = FCPATH.'ci_sessions';
    $config['sess_match_ip'] = FALSE;
    $config['sess_time_to_update'] = 300;
    $config['sess_regenerate_destroy'] = FALSE;
    

    That looks good. Session will be saved in folder

    FCPATH.'ci_sessions';
    

    FCPATH will be replaced with web application root folder where index.php is present. I verified ci_session folder exists and changed its permisison to 777, but that did not fixed the session problem.

    To verify session is working, i added 2 function to controller

    public function set-session()
    {
        $newdata = array(
            'username'  => 'johndoe',
            'email'     => '[email protected]',
            'logged_in' => TRUE
        );
        $this->session->set_userdata($newdata);
        die("test");
    }
    
    public function read-session()
    {
        $this->load->library('session');
        $name = $this->session->userdata();
        echo "
    ";
        var_dump($name);
        var_dump($_SESSION);
        exit;
    }
    

    I can call these functions to verify is session is setting properly with url like yourdomain/controller-name/function-name

    The problem was due to PHP 7. To fix it, edit file

    system/libraries/Session/Session.php
    

    Find

    ini_set('session.name', $params['cookie_name']);
    

    Replace with

    ini_set('session.id', $params['cookie_name']);
    

    You can find more about Codeigniter session at https://codeigniter.com/userguide3/libraries/sessions.html

  • Redirect a site to www using PHP

    To redirect a site to URL with www using PHP, you can use the following PHP code

    <?php
    
    $domainName = $_SERVER['HTTP_HOST'];
    
    if (strpos($domainName, 'www') === false) {
        header("HTTP/1.1 301 Moved Permanently");
        $urlNew = 'https://www.' . $domainName .  $_SERVER['REQUEST_URI'];
        Header("Location: $newUrl");
        exit;
    }

    Add this code in your index.php file or another file included by index.php

    See Redirect

  • Vue.js

    Vue.js is a popular javascript framework. To install Vue, you need Node.js installed.

    sudo su
    curl -sL https://deb.nodesource.com/setup_15.x | bash -
    apt-get install -y nodejs
    apt-get install gcc g++ make
    

    Install Vue.js with

    sudo npm install -g @vue/cli
    

    Verify Vue.js working with

    vue --version
    

    To create a project, run

    vue create app1
    cd app1
    npm run serve
    
  • PHP script to test IMAP

    Here is a PHP Script to login to IMAP server.

    Mailboxes\n";
    
    $folders = imap_listmailbox($mbox, "{imap.example.org:143}", "*");
    
    if ($folders == false) {
        echo "Call failed
    \n"; } else { foreach ($folders as $val) { echo $val . "
    \n"; } } echo "

    Headers in INBOX

    \n"; $headers = imap_headers($mbox); if ($headers == false) { echo "Call failed
    \n"; } else { foreach ($headers as $val) { echo $val . "
    \n"; } } imap_close($mbox);

    See PHP

  • How to find Laravel Framework Version

    Laravel is a popular PHP framework. In this post, I will show how to check laravel version.

    To find the Laravel Framework version, run

    php artisan --version
    how to check laravel version

    Another way to find Laravel version is to check the file composer.lock present in the Laravel web root directory.

    cat composer.lock

    You will see something like the following

                "name": "laravel/framework",
                "version": "v6.1.0",
                "source": {
                    "type": "git",
                    "url": "https://github.com/laravel/framework.git",
                    "reference": "a365bea7ff0dea9a50f5904be9e41656def9af4b"
                },

    In this case, we are using Laravel version 6.1.0

    See Laravel

  • java

    war file for testing Tomcat/Jetty

    wget http://sourceforge.net/projects/plantuml/files/plantuml.war/download
    mv download ROOT.war

    Put it in webapps folder.