Customizing Google Calendar Colors

<iframe src="/gcalendar-wrapper.php?src=[google calendar embed url here]" style="border: 0" width="100%" height="600" frameborder="0" scrolling="no"></iframe>

Go to https://calendar.google.com/calendar and get the embed code

Hover to your calendar and click the arrow down symbol. Then click Calendar Settings.
You can find the embed code in Calendar Details

Download the file here https://github.com/tolabl/tsuchiya_HP/blob/master/gcalendar-wrapper.php

Require All Application to use SSL

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new AuthorizeAttribute());

        //require application to use SSL
        filters.Add(new RequireHttpsAttribute());
    }
}

After that, we need to update project url. Right click on project > properties > web and update Project Url like https://localhost:44331/

Content Delivery Network or CDN

CDN is a network of servers to deliver resources to its nearest users.

When a website is using CDN, any user trying to access its resources will pass through CDN network. The nearest server that has the requested resources will deliver it to the user. Faster load speed.

Since the request didn’t even reach to the server, there’s reduced load to the server making it available to more users.

Below is an example, www.example.com is hosted in x.x.x.x server

Without CDN

>tracert www.example.com

Tracing route to www.example.com [x.x.x.x]
over a maximum of 30 hops:

  1    <1 ms    <1 ms    <1 ms  192.168.0.100
  2     1 ms     2 ms     1 ms  10.228.209.86
  3     1 ms     2 ms     2 ms  122.2.174.85.static.pldt.net [122.2.174.85]
  4    14 ms    14 ms    14 ms  210.213.130.186.static.pldt.net [210.213.130.186]
  5    13 ms    13 ms    13 ms  210.213.130.162.static.pldt.net [210.213.130.162]
  6    31 ms    31 ms    31 ms  210.14.2.102
  7    35 ms    35 ms    34 ms  106.187.0.65
  8    77 ms    34 ms    34 ms  hkmjbb001.int-gw.kddi.ne.jp [111.87.5.25]
  9    84 ms    83 ms   100 ms  obpjbb205.int-gw.kddi.ne.jp [118.155.199.193]
 10    80 ms    80 ms    80 ms  jc-osa301.int-gw.kddi.ne.jp [113.157.227.82]
 11    83 ms    83 ms    83 ms  113.157.231.42
 12    84 ms    84 ms    84 ms  oshrt1s-drt2-2.bb.sakura.ad.jp [157.17.146.42]
 13    84 ms    84 ms    83 ms  osnrt2b-hrt1s.bb.sakura.ad.jp [157.17.146.10]
 14    84 ms    84 ms    85 ms  osnrt3c-nrt2b.bb.sakura.ad.jp [157.17.148.2]
 15    80 ms    80 ms    80 ms  210.188.211.108
 16    80 ms    81 ms    81 ms  x.x.x.x

Trace complete.


It took 16 hops for my request to reach www.example.com original server

With CDN

>tracert www.example.com

Tracing route to www.example.com [cdn ip address here]
over a maximum of 30 hops:

  1    <1 ms    <1 ms    <1 ms  192.168.0.100
  2     4 ms     2 ms     1 ms  10.228.209.86
  3     2 ms     1 ms     1 ms  122.2.174.89.static.pldt.net [122.2.174.89]
  4    15 ms    14 ms    14 ms  210.213.130.194.static.pldt.net [210.213.130.194]
  5    14 ms    14 ms    14 ms  210.213.130.109.static.pldt.net [210.213.130.109]
  6    14 ms    16 ms    14 ms  210.14.3.102
  7    14 ms    15 ms    14 ms  [cdn ip address here]

  Trace complete.


It only took 7 hops to reach www.example.com using CDN and no request was made to its original server. So faster content delivery and reduced load to the original server.

.htaccess redirect http to https

#check if https is off
RewriteCond %{HTTPS} off
#if the condition above is true, apply this RewriteRule 
#it redirects http to https with 301 or permanent redirect
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#the code below will redirect all domain's non-www to www
#[NC] for case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


Note: Search engines treat non-www and www sites as different sites, so for SEO purposes, select either www or non-www version of your site and do permanent redirect to the other site to avoid duplicate content

Parsing WordPress RSS Feed

Tested in content types
Content-Type:application/rss+xml;
Content-Type:text/xml;

$url = 'https://domain.com/blog/?feed=rss2';
$feed1_xml = simplexml_load_file($url) or die("feed not loading");

To get the first post title
echo $feed1_xml->channel->item[0]->title;

To get the first post published date
echo $feed1_xml->channel->item[0]->pubDate

Returned date is in this format Wed, 20 Apr 2016 04:22:10 +0000
You can also convert it to ‘Y/m/d’
date('Y/m/d',strtotime($feed1_xml->channel->item[0]->pubDate));

New Custom Post Type is not working

-rw-r--r-- 1 user members 842 3月 8 18:21 single-about.php
-rw-r--r-- 1 user members 842 3月 8 18:36 single-course.php

I already have an existing custom post type “about”. It’s loaded using single-about.php
I added new custom post type “course” and copied single-about.php content to single-course.php but the new post under “course” custom post type cannot be viewed.
And there’s also no sign that single-course.php is used.

The solution was to flush rewrite, you can do this by going to Settings > Permalinks and just hit Save Changes and everything works as expected after that.

Adding Roles in ASP.NET MVC 5

//UserStore & RoleStore
//UserManager, RoleManer and SignInManger

var roleStore = new RoleStore(new ApplicationDbContext());
var roleManager = new RoleManager(roleStore);
roleManager.Create(new IdentityRole("Role1"));
UserManager.AddToRole(user.Id, "Role1");

Note: UserManager.AddToRole(user.Id, "Role1")
will add record to AspNetUserRoles but not the cookie. Later when we check the role using
User.IsInRole("Role1")
The above code will return false because it checks the cookie. We need to re-signin the user to update the cookie
SignInManager.SignIn(user, false, false);
Identity information like roles and claims are stored in the cookie when user logs in.

Adding Global Filters to ASP.NET MVC 5

In App_Start > FilterConfig.cs
public class FilterConfig
{
  public static void RegisterGlobalFilters(GlobalFilterCollection filters)
  {
    filters.Add(new HandleErrorAttribute()); //redirects the user to error page when an actions throws an exception
    filters.Add(new AuthorizeAttribute()); //all controllers will require authentication so we don't have to add [Authorize]
  }
}

Sublime Text 3: Adding Auto-Fortmat Hotkey

In Sublime Text menu, Preferences > Key Bindings – User add the code below inside []
{"keys": ["alt+shift+f"], "command": "reindent", "args": {"single_line": false}}
Re-open Sublime Text editor and press ALT+SHIFT+F to format texts