Hosting CodeIgniter inside a sub directory with Nginx


 

As I mentioned in my previous post, I had been setting up an old Codeigniter web app today in my local machine. This app was hosted with the Apache web server and it was running on PHP 5.6.


I tried to put this in a VM in my local machine using Vagrant. However, I faced some issues with this setup as I wanted to make it run with Nginx instead of Apache, and that too, inside a sub directory. The main problem I had was with the URL rewrites. In Apache, I was able to write an httaccess file and that would simply handle the rewrites. 


However after some trial and error efforts, it turned out that it was really easy. Even easier than I actually thought. All I had to do was set the correct paths in the nginx config.

 

My CI app as residing inside a directory called 'admin'. So in my nginx host config file, I provided a location block as below.

 

location /admin {
        try_files $uri $uri/ /admin/index.php?/$query_string;
}


and voila, it started working. 

 

I thought of adding this to my blog as a note to myself. Anyway, If you ever come across this post, and have any issues, feel free to leave a comment.

Fix - CodeIgniter session not set with PHP 7.x


Today I had to do some updates to an old web app I made with CodeIgniter. The original app was made around 5 years back and there has been no changes to it until this one. All this time it was living in one of my old web servers with PHP 5.6 and had no issues at all. However, when I tried to setup this in my local machine (inside Vagrant, with PHP 7.x), I found some issues with the sessions. I noticed the sessions are not getting persisted between the redirects. 

 

After some research, I found out that the issue was actually with the PHP version. Luckily, I didn't have to change it to an old PHP version. There was an easy fix (somewhat surprising), and I got it working in no time. 

 

If you ever face this issue, all you got to do is: in your CI folder, open the CI/system/libraries/Session/Session.php file and look for a line which says:

 

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

and change it to

 

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

 

and that's it. Your app should start working fine :)

Pretty print Curl JSON responses in terminal

Today I wanted to test one of my APIs and used curl to get the response in the terminal. The output wasn't clear at all because it usually returns everything in one line. 

 

Fortunately, there was a way to pretty-print the response, as in real json format. All you have to do is pipe the json_pp command after your curl command.

 

Ex: 


curl --location --request GET 'http://my.api.local/api' --header '...' | json_pp

 

and that's it. You should see a formatted json response.

 

Pretty print Curl JSON responses

Hope this will help someone. Please feel free to comment and let me know if you know any other better way :)

How to turn on Android device without the power button

How to turn on Android device without the power button 

 

I broke the power button in my Android phone and I was keeping it alive by constantly charging it (so the battery won't die and I won't lose any data in my phone in case it get switched off).

 

But when I woke up today morning, I realized I forgot to connect my charger last night, and now my phone is gone forever :( But I wasn't ready to give up it so easily. So I looked for a way to "revive" this dead phone :D and luckily I found some resources which I tried (all of them) and here's what worked for me. 

 

First you need some charge on your phone. Connect it to a charger for a while.

 

Then you need to start your phone in the fast boot mode. This is easy. Just hold your volume down button (or up button? try.) and connect your usb cable while you're holding that volume button. Make sure you actually connect the other end of the cable to your computer as well. If it goes well, your phone should power up and should take you to the fast boot screen. Now its kinda powered on, yay!

 

Now keep it like that and come back to your computer. You need android platform tools. Head over to the following page and download them to your computer. Pick the right one for your OS.

 

https://developer.android.com/studio/releases/platform-tools

 

 Extract the files and copy it to somewhere in your computer. I kept these files in the following folder. 

 

/Users/nimeshkasrimal/Documents/adb/platform-tools

 

Now you need to add this path to your PATH system variable.

 

echo 'export PATH=$PATH:/Users/nimeshkasrimal/Documents/adb/platform-tools/' >> ~/.bash_profile

 

and then 


source ~/.bash_profile

 

to update your current terminal session with the new path. (If you're on Windows, this is a bit different, just Google it and you will find how to do this)

 

Alright, now you are ready. Just two commands away. 

 

Now if you run 

 

fastboot devices

 

It should list your phone in the command output. (If it doesn't show your phone, reconnect it the computer and run the command again.)

 

~/Documents/adb/platform-tools> fastboot devices
84RDU17815002927    fastboot

 

and now, all you need to do is, run the following command.

 

fastboot reboot

~/Documents/adb/platform-tools> fastboot reboot
Rebooting                                          OKAY [  0.022s]
Finished. Total time: 0.022s


This should reboot your phone and you're done! :) I hope this blog post will help someone. Please leave a comment and let me know if you have any problems, suggestions :)