Category Archives: programming

PHP date function

Some useful date hacks

  1. Display only date when receiving full datetime type from MySQL
echo date('Y-m-d',strtotime($mysql_datetime));

2. Add or subtract x days or months to a date.

link

Laravel Notes

Useful Resources

  1. Official website
  2. Laravel 5 fundamental tutorials on laracasts
  3. Composer – dependency manager for PHP
    1. click on “Browse Package” to go to packagist.org to search for packages
  4. List of laravel resources – base project, cheat sheet, community etc
  5. Laravel cheat sheet
  6. Laravel cheat sheet 2
  7. Model & migration generator
  8. View generator

Quick notes (I’m using a Mac)

Downloading composer

  1. Run this in terminal
    1. curl -sS https://getcomposer.org/installer | php
  2. Once successful
    1. You got a message
    2. A file composer.phar on current directory
    3. To run the file, type
      1. php composer.phar
  3. Or move to globally accessible directory
    1. mv composer.phar /usr/local/bin/composer
  4. To download a package (example). This download requires Git to be installed
    1. composer require laravel/laravel
    2. wait for composer to download all the packages and update the .json file

Create a laravel project

  1. Run this command on terminal. You better set your current directory to web directory
    1. composer create-project –prefer-dist laravel/laravel blog

Commonly-used Artisan Command

  1. php artisan list (to list all artisan commands available)
  2. php artisan –version (to view laravel version)
  3. php artisan help <command_name> (to view help for a command)
  4. php artisan make
    1. php artisan make:controller <fooController> –resource (create a resource controller with default methods. i.e. index, show, create, store, edit, update, destroy)
    2. php artisan make:model Student –migration (will create a model of student and database migration of students table)
    3. php artisan make:migration create_users_table (will create a migration file in database/migration directory)
  5. php artisan migrate (to migrate database)
    1. php artisan migrate –seed (migrate database and load seeder)
    2. php artisan migrate:status
    3. php artisan migrate:refresh (remove all and remigrate)
    4. more about migration
  6. composer dump-autoload – to regenerate autoload files. especially when error run migrate or db seed. 

Package browsers

  1. packalyst.com (laravel only)
  2. packagist.org (php packages)

Some useful packages/sample applications

  1. laravel admin package – voyager
  2. open source laravel crm – flarepoint

Laravel Homestead and Valet

  1. Haven’t had time to go through yet
  2. Valet is not using vagrant and very simple
  3. Only run on mac

Other resources

  1. Laravel cheat sheet

DigitalOcean – Notes

This is my notes on digital ocean.

Click here to register with DigitalOcean and get free USD10 to start using the service.

I used to install Ubuntu 14.04 with LAMP on it. With this also get SFTP ready.

Manage users and groups

Change root password. If logged in as root

  • passwd

Add new user

  • adduser is perl script to simplify original useradd function
  • command – adduser username_here
  • just answer all questions asked
  • This add user function will …
    • Create the user named username.
    • Create the user’s home directory (default is /home/username and copy the files from /etc/skel into it.
    • Create a group with the same name as the user and place the user in it.
    • Prompt for a password for the user.
    • Prompt for additional information on the user.
  • allow user for sudo mode (optional) – usermod -a -G sudo <your username>
  • add user to group – adduser username groupname
  • more on adduser
  • and more

Manage groups for user

  • list group – cat /etc/group
  • add user to group – adduser usrname groupname

Manage services

Connect to server via SSH, in terminal type the following:

  • ssh username@yourdomain_or_ip

To get default MySQL root password, write this in terminal. Remove the file once done change the MySQL root password.

  • cat /etc/motd.tail

To enter mysql console

  • mysql -u root -p

Secure MySQL server. Run following command and answer all the questions.

  • mysql_secure_installation

To change MySQL root password:

  • mysqladmin -u root -p’oldpassword’ password newpass

To only allow certain IP to access directly to database

  • edit file /etc/mysql/my.cnf
  • comment line with IP 127.0.0.1
  • restart service – service mysql restart
  • enter following command in mysql command line
    • type mysql -u root -p
    • enter password when asked
  • mysql> GRANT ALL ON database_name.* TO user@xx.xxx.xx.xx IDENTIFIED BY ‘your_password’;
    • xx.xxx.xx.xx is the remote IP to access the server

Enable .htaccess (mod rewrite)

  • enable mod rewrite – sudo a2enmod rewrite
  • update file /etc/apache2/apache2.conf
  • change Override none to Override all for web root directory
  • restart service

To restart services (can also use stop and start)

  • service mysql restart
  • service apache2 restart

Install sendmail service (used by PHP mail function)

  • apt-get install sendmail
  • Run the sendmail config and answer ‘Y’ to everything: sendmailconfig

Server general settings

Change permission for directory (especially for ‘upload’ directory)

  • chmod 755 /path/directorypath

Change the timezone. By default using US time zone

  • sudo dpkg-reconfigure tzdata
  • follow instruction on screen
  • check if the date is correct by typing – date

To turn off server

  • sudo shutdown -h now
  • OR
  • sudo poweroff

Check for disk utilization

  • to check for disk utilization
    • df -h
  • to check for huge files location
    • sudo du -a / | sort -n -r | head -n 10
  • check for huge file (another option)
    • find / -size +50M -ls

Securing the server

To update/upgrade OS

  • aptitude update
  • aptitude upgrade

Disable root login on SSH

  • edit /etc/ssh/sshd_config
  • set PermitRootLogin no
  • restart ssh – service ssh restart

Block IP’s from accessing certain services

Other measures:

  1. Disallow access to server via root username
  2. Disallow access directly to database from outside
  3. Close all unused ports. Left only web, SFTP, MySQL, SSH
  4. auto patch?

To explore

  • using VPN
  • using SSH key
  • creating ftp user to access only certain directory
  • virtual host (multiple domains for a server)
  • security for 777 chmod directory
  • change ssh port to different port (but still below 1024)
  • install fail2ban to protect against brute force attack

Additional tools

  1. Ansible.com
  2. Docker.com

Other reading and references

  1. Securing the server (SSH, VPN etc)
  2. Initial setup for Ubuntu 14.04

Editors

vi, vim, nano

Click here to register with DigitalOcean and get free USD10 to start using the service.

CodeIgniter – How to remove index.php in URL

2 simple steps.

1. Create or update the .htaccess as follow

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

OR

RewriteEngine On
RewriteBase /app/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /app/index.php [L]

OR
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

ErrorDocument 404 /index.php


2. Update the config/config.php file to this

$config[‘index_page’] = ”;

Minimum bootstrap template using CDN

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
 
    <title>Title here</title>
 
    <!-- Bootstrap core CSS -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
 
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
 
  <body>
 
    <div class="container">
 
      <div>
          <h1>Heading here</h1>
          <p>Content here</p>
      </div>
 
    </div><!-- /.container -->
 
 
    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  </body>
</html>

CodeIgniter Quick Reference

Some of useful functions but not all the time use. Will be a quick reference everytime want to use them.

URI segment

http://example.com/index.php/news/local/metro/crime_is_up
$this->uri->segment(3); //will produce: metro

DB SQL

$query = $this->db->query("YOUR QUERY");

if ($query->num_rows() > 0)
{
        foreach ($query->result() as $row)
        {
                echo $row->title;
                echo $row->name;
                echo $row->body;
        }
}
$this->db->select('title, content, date');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$this->db->where('name !=', $name);
$this->db->where('title', $title);
$query = $this->db->get('mytable');

end

Display all post title in blogspot

Use this code to display all post titles in your blogspot.com. You can place this code using widget in sidebar.

Replace YOUR___BLOG with your own blog url.

<script language="JavaScript" src="http://feed2js.org//feed2js.php?src=http%3A%2F%2FYOUR___BLOG.blogspot.com%2Ffeeds%2Fposts%2Ffull+&utf=y" charset="UTF-8" type="text/javascript"></script>

<noscript>
<a href="http://feed2js.org//feed2js.php?src=http%3A%2F%2FYOUR___BLOG.blogspot.com%2Ffeeds%2Fposts%2Ffull+&utf=y&html=y">View RSS feed</a>
</noscript>

Sample use of CI query builder for CRUD

Sample insert

$data = array(
        'title' => $title,
        'name' => $name,
        'date' => $date
);

$this->db->insert('mytable', $data);

Sample update

$this->db->update('mytable', $data, "id = 4"); OR
$this->db->update('mytable', $data, array('id' => $id));

Sample delete

$this->db->delete('mytable', array('id' => $id));

Sample to get data

$where = "name='Joe' AND status='boss' OR status='active'";
$this->db->where($where);

More on getting data with CI

CI Query builder reference: CI2, CI3

Tools to develop cross-platform mobile application

Now I’m doing some research to start developing mobile applications.

There are several tools came out when I was doing the search.

  1. kodular.io (previously makeroid.io). free
  2. mit appinventor – almost similar to kodular and thunkable. free
  3. thunkable.com – tested and ok. complete video tutorial. free for public project. usd200/yr for private (promo at usd100/yr)
  4. flutter.io by google
  5. kikApp – develop with PHP
  6. phonegap – develop with HTML, CSS and Javascript. Pay to build?
  7. monaca – build online, use onsen ui
  8. Appcelerator – use Javascript. Free to join and develop. Pay only when go live. Minimum USD39/mo
  9. Xamarin – with free package with limited app size
  10. Corona SDK – with free package
  11. Meteor – open source, javascript
  12. AppsMoment – online GUI tool
  13. buildup.io – app prototyping

Drag and drop tools

  1. appinstitute.com

Website to APK conversion tools

  1. appstrand – free tool. no ad
  2. web2apk – free with donation. claim with no ads
  3. websitetoapk – start with free and no ad, premium with more features like push notification etc

Registration to place app on market

  1. Apple app store – USD99 (per year)
  2. Google play – USD25 (one-time)

Other related tools

  1. android app launch icon generator
  2. onsen.io – html5 hybrid template
  3. framework7 – html framework for html5
  4. one signal – for push notification
  5. firebase – can also use for push notification
  6. progressive web app – web apps but fast, responsive

Other tools (need to filter all the ads etc)

  1. freeweb2app.com – convert your website to app in 3 easy steps. – name your app, enter url, add icon – charge per conversion is USD9 (one time) for android app and usd249 for ios
  2. instappy.com
  3. buildfire.com
  4. appypie.com
  5. kinetise.com
  6. appsgeyser.com
  7. free app building with revenue sharing from ad
  8. onsen.io

html5 mobile app

  1. appmakr.com – free 30-day trial for all package
  2. mobiroller.com
  3. free native app with ad supported, 5 push notification
  4. mobincube.com – free native app with ad supported.