ad

Friday, 26 February 2016

CodeIgniter Beginners Series - Lesson 2 Controller | Routes and View


Welcome to codeigniter beginners series day 2. Today we'll be installing codeIgniter, go over some very basic codeigniter concepts, we'll try loading some basic pages and make changes to it then learn about routes, passing data and setup a route for our new page aswell.

Lets get started. First go head and install codeIgniter, here's a tutorial I wrote on installing codeIgniter on linux. http://www.webdevtown.com/2016/02/installing-codeigniter-on-linux-ubuntu.html If you're on windows it's still the same steps you'll follow to install. Here's codeIgniter official documentation its installation. http://www.codeigniter.com/user_guide/installation/index.html

With that aside, lets move on to more useful stuff. Open 'localhost' on your browser, make sure apache server is running. You should see a basic welcome page. This is codeigniter's default route. Means anyone who loads localhost will see this page.

Some code!

Lets open our controller folder and see what's inside Welcome.php controller file. I'm assuming you've read MVC if you haven't then you should do so to understand this tutorial. Read it here http://www.webdevtown.com/2016/02/mvc-model-view-controller-seriously.html Alright, we're in Welcome.php file, this a a file that is loaded by default. Whenever someone opens localhost, this file is run. This file contains a class that extends from CI_Controller class, means all CI_Controller's method and properies are available to this class. If we want to create a controller we just give our class a name and extend it form CI_Controller class.

General URL structure is like this. domain/CONTROLLER_CLASS/CONTROLLER_CLASS_METHOD/PARAMETERS. In our case controller class is Welcome, method is index and parameter is welcome by default unless user gives one.

class Welcome extends CI_Controller{
function index($page = 'welcome'){
$this->load->view($page);
}
}



Proceeding, we're $this->load->view('welcome'), when the index method is run, it will run the view method giving it welcome argument, welcome is name of our view page which is inside view folder. If user provided no argument in URL welcomepage would be loaded by default, else whatever page user asked for in argument.


What're routes?

Route is a the route to controller. It means what controller to load depending on what information user provides in URL. As I mentioned about typical URL consists on following division domain/CONTROLLER_CLASS/CONTROLLER_CLASS_METHOD/PARAMETERS. Lets create an test URL. http://localhost/index.php/data/names/jon. Now this url is suppose to return information about user jon. As, jon being parameter, names the method and data the controller class name. Lets say you don't like this long URL you want jon's information when user types http://localhost/user/jon this is where routes will come into play. We'll tell our route file, whenever use types /user/USERNAME in URL, run the data class, username method and give it arguement. Note you can put your data into $data() array and pass it to view like so, first put data in $data array, $data['some_info] = $information; then pass it to view, $this->load->view('page_name', $some_info), and then in view file 'page_name' you can show that variable's value like echo $some_info.

So here's my controller, that takes runs data controller, username method in it and passes it brown argument. Then we pass that brown argument to view to display it. Not that in real sanario we'd also get user info off data base. But since it's just 2nd lesson we'll just pass users' name to view. http://localhost/index.php/data/username/brown.













 Lets back off a little and see how we can just use http://localhost/user/jon and still get this working. Switch to routes.php file scroll down and delete pre existing entries. They're at the very bottom after all the commentary. They look like $route['default_controller'] = 'welcome.php'; Delete that too lets create an entry. Note that codeigniter uses *wildcard symbols or keywords for url matching. For example, (:any) means any URL. jon/brown will mean one or another in URL.. if any of it matches, direct user to specified URL.

Our route will look like this $route['user/(:any)'] = "data/username/$1"; It says, whenever user types user/ANY_USERNAME redirect user to data/username/$1 $1 being the argument's value. When user is directed to that page, our controller takes over and process the information showing us the view page.

This is really important content used in almost all frameworks, being as important as it is it requires quite a bit of practice. Best way to practice is to look at different website URL, and make them user friendly using routes.

In next tutorial we'll be learning about model.

Thursday, 25 February 2016

How to hire a good coder/programmer

Coding as we all are well aware is a complex stressful and time consuming process for any coder out there which is why it's extremely important that both coders and the client work together. Hiring someone and let them do the job is never the route to follow, whether it's a retail, pest control, site administration or anything for that matter requires both parties to work together to develop a good end product.

If you're a coder it's really important that you know exactly what you're capable of(never sell yourself short though) and what you're not. Being greedy, taking client's money and NOT delivering the product on time is nothing but a reputation destroyer, just as good word spreads quick bad word spreads quicker. The important factor that we all can agree upon here is communication, as tiring and time consuming coding is, it as a coder is your responsibility to be the first one to get the conversation started and update the client as often as possible so if there's any issue or change in plans the client wont be as surprised. The client too will think twice before telling you to make major changes.

Here's a few thing to ask the coder before you hire him:
- Portfolio
- If worked on similar project
- Rough deadline
- TOS(Extremely important, discuss what they intend to do if project is not finished on time, how they plan on making changes, if there's an additional fee etc..).
- Logical process coder plans on taking. How to plan on achieving what you require.
- Name
- Phone number
- Address


Here's a few thing to ask the client as coder:
- Name
- Phone number
- Address
- NDA(Non disclosure agreement, if there's any)
- Availability(times they're available for discussion)
- Budget(Make it clear what they pay in advance and what after project completion).

It's also very important that you have clear idea of your own project. Being totally illiterate to technology wont help. You should almost always ask the coder certain somewhat technical questions. They should be able to answer it in a simple way that you understand, If you're having trouble understanding it's either you didn't study about your own project or this coder's not the right person that will update you frequently in non technical manner that you understand and make decisions if necessary.

Pricing is an interesting yes quite simple to guess, all it takes is a word with different coders asking for price and figure out whichever makes most sense to you. Do note that there's hundreds of things that determine the price for example coding style, deadline, experience(even shitty coders but with lots of experience can charge you alot), maintenance and whatnot..

Last thing's just like everything if you plan properly everything will go right, hurrying in any investment is never a good idea. Luck aside, most of the you'll end up losing money and time and give up, not because coder didn't do the job properly but because you chose the wrong service that wasn't really suitable for your project.

CodeIgniter Beginners Series - Starting up

CodeIgniter Beginner Series - Starting up




If you've been developing web application in PHP for a while now and have a good working knowledge of OOP this may be the right time to pick up a framework that makes you life easier. If your goal is to learn then forget about framework and keep focusing on developing stuff from scratch but if you're past that point where you feel tired rewriting code and just want to be super productive and make money then you got to use a framework. Learning curve is everywhere so don't expect it to like writing your 'Hello world!".

This is a Codeigniter tutorial series and in this series I'll from beginner to intermediate tutorials covering almost everything that you need to know. Things that are not so important, I won't even touch. If you're past intermediate projects and feel the need to learn it advanced concepts, you're free to as you'll have solid foundation to understand any new concept or pick any new framework.

Before we proceed it's important that you understand what MVC is because Codeigniter is based on it. You can read about MVC here http://www.webdevtown.com/2016/02/mvc-model-view-controller-seriously.html. Today's task is nothing more than reading and understanding MVC, I won't teach you anything, we won't do any code. It's all MVC, search and read till you understand. Once MVC concept is somewhat clear then come back here tomorrow. I'll be teaching some basic stuff like CodeIgniter installation, loading pages and understand what routes are and how they work.

See you tomorrow! We'll ignite some code!

Monday, 22 February 2016

How to Block Tor Traffic

How to block Tor Traffic

Blocking Tor Ip's is pretty straight forward. I'll give you step by step process to follow to block Tor IP's but before that I'll give you guys a break description of how Tor Network work.

Tor is a network of relays/servers. When you try connecting to a certain website it goes through these replays, your computer knows about the first replay it's connected to, and the website you're connecting to knows about the last relay. These relays are other people's computers, they install the software and become relay.

Exit relay is the last relay that your data goes through before the request is sent to the site you're trying to connect. ALL THESE EXIT RELAY IPs ARE PUBLISHED.

To detect Tor IP all you need to do is check whether the IP connecting to your server matched any IP in that Tor exit relay list. If it does match, means it's coming off a Tor exist relay.

For example, here's a TOR relay https://globe.torproject.org/#/relay/1C90D3AEADFF3BCD079810632C8B85637924A58E and its IP address is 62.210.82.44 which mean if you query Relay database with that IP you should get positive response back with some data. If it returns negative response, means it's not a Tor IP address.


Lets get practical. Visit this website and put a random IP address. You can put your too and press enter. https://check.torproject.org/cgi-bin/TorBulkExitList.py It will show the list of Exit nodes that are allowed to connect to your computer/server. Generally you'd send your server's IP address to this website with port and it will show you the list of exit relays that can cannot to your server. Then just iterate through these IPs and if the suspicious IP/IP address trying to connect to your server exists in that list. It means it's a Tor computer address.

Here's an example.
https://check.torproject.org/cgi-bi/TorBulkExitList.py?ip=IP_ADDRESS&port=PORT_NUMBER

Example with info:
https://check.torproject.org/cgi-bi/TorBulkExitList.py?ip=216.239.34.10&port=80


That said and explained do make sure that you aren't blocking the legit user, someone using Tor network doesn't automatically make them an abuser, and it's important to keep in mind that someone could be using their PC to connect to website in general way but also use the same IP for Tor, meaning if you query their IP in Tor relay IP's it will say it's a Tor IP even though user is visiting your site without going through Tor relays.

Wednesday, 17 February 2016

CodeIgniter Documentation Example Code Error | Explained and Fixed

CodeIgniter Documentation sample code error. 404 Page Not Found The page you requested was not found.


First of all go have a look at CodeIgniter Static pages documentation.
http://www.codeigniter.com/user_guide/tutorial/static_pages.html

Here's their Example of Controller class Pages:
class Pages extends CI_Controller {
   public function view($page = 'home') 
      {
          if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))
          {
             // Whoops, we don't have a page for that!
            show_404();
         }

       $data['title'] = ucfirst($page); // Capitalize the first letter

       $this->load->view('templates/header', $data);
       $this->load->view('pages/'.$page, $data);
       $this->load->view('templates/footer', $data);
   }
}

Now as you can see the 'APPPATH' CONSTANT on line 3 is path to the 'application' folder and it's defined in our main index.php file.

If you go and create template folder, put files in, same with pages and whatnot and run it, it wont run. It'll instead invoke show_404() function saying page doesn't exist. Specifically tell us, "APPPATH.'/views/pages/'.$page.'.php'" doesn't exist. It's really a pain because it does indeed exist even after setting up everything correctly and granting full perms.

So after searching in Google for about 4-5 hours and trying to resolve it myself I decided to check the exact value of that 'APPPATH' CONSTANT. That's how it's defined: "define('APPPATH', $application_folder.DIRECTORY_SEPARATOR);".

if ( ! is_dir(BASEPATH.$application_folder.DIRECTORY_SEPARATOR))
{
    header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
    echo 'Your application folder path does not appear to b$
    exit(3); // EXIT_CONFIG
}


It seem perfectly fine at first glance but what's that 'DIRECTORY_SEPERATOR' CONSTANT doing there? well I guess it's just adding a directory separator '/'. Perfectly fine, right? Now if you've been following their documentation they've a Directory separator '/' before 'views' aswell, '! file_exists(APPPATH.'/views/pages/'.$page.'.php'', which means APPPATH will add one directory separator, and if you follow the documentation they've added another directory separator in total two separators, making file_exist function return false and thence error message!!

This is what we're basically asking file_exists() function to check. Does /var/www/application/.'/views/pages/'.$page.'.php'. exist? It'll always return false due to 2 slashes! To fix the issue you need to remove the clash before views.

Monday, 15 February 2016

MVC Model View Controller | Seriously Simplified Version

Model View Controller Seriously Simplified

I've written about MVC in past..
http://www.webdevtown.com/2015/04/model-view-and-controller-with-example.html
So, recently I was going through that past tutorial and decided to re-read it, to my surprise it came across as not very easy to understand, so here's another quick attempt. Hope this one helps it understand.

Okay. You're a web developer who develops everything by himself, from design to complex coding, it's all done by you. No big deal, you get the idea of what you want to develop and start writing the code, function by function, time comes when you've got 100s of files thousands of functions and it's a freaking mess, isn't it? where do I change code to update row in table? I want to make sure data is cleaned. Wait, what's this HTML doing here? hmm I need to make it easier to get usernames and other data from DB.. okay I might update the code, but it'll require my to update the logic too.. hmm it's a freaking mess.. wait hmm.. I'll be selling this product to a fellow developer.. I wonder how he's gonna deal with all this tight coupling.. I've glued everything together.. Hope he doesn't hire designer to update the template.. it'll be nightmare, he might come back to my saying code's a crap.

That's why you need MVC. Writing code alone or with someone, it it isn't clean it's just.. pain..

There's three things you need to know and know it well enough. Here's the start:

Model

Model's a class that represents all your data. It has methods that update, delete, retrieve data from DB. You could have class that'd retrieve recently signed-up users from DB, or any other data. You want data, create object and get it. You want to delete user, pass user name and it deletes user. Everything nice and tidy. It's mostly about the DATA!! Not manipulation or other stuff but about Data!

View

View is what you see on webpage, HTML, forms, titles, tables, buttons. It all belongs to View page. It has minimal script. It's mostly HTML. Don't add bunch of code to View unless you want to piss yourself or designers in future and hear them telling others your code sucks!

Controller:

It's piece of code that sits between View and Model. User hits Submit button from View, Data goes through Controller, you know, you might want to do some validation, manipulate, twist it and whatnot. It all happens here, once controller's job's done. It can send data to model if it requires database manipulation or information retrieval, or it can return data back to View, where View will present it to user. If app required data from Model or needed to make some changes to data, it will be sent to Model and then Model class would send it back to Controller, then Controller to View for Presentation. View wont interact with Model directly, it has no business to do so..

Whole purpose of this MVC pattern is to simplify(Ahm! OOP) things up and make it easy to manage the code in future. Before you start with MVC it's necessary you understand OOP and have created an app or two using it's fundamentals. MVC isn't really a need per say, but someone who's just writing couple hundred lines of basic code it's good to know that MVC exists so when you feel your code's getting messy, you know what to do about it!

Friday, 12 February 2016

Installing CodeIgniter on Linux Ubuntu Server for Beginners

Installing CodeIgniter on linux

*Basic Linux CLI knowledge required*

CodeIgiter is a PHP Framework or tool-kit to develop full featured or simple application. If you've been working with PHP for a while now you should definitely try new frameworks now, though today we'll be learning how to install Codeigniter framework on Linux.

To download CodeIgniter navigate to Apache public/root directory and run the following command in Terminal. You can also use SSH to connect to your server shell and follow the exact same steps. Once you're in 'var/www/html' directory(root), run 'wget https://github.com/bcit-ci/CodeIgniter/archive/3.0.4.zip' in Teminal to download the Framework.

When the download is done. Stay in same directory and extract the file using command 'unzip CodeIgniter-3.0.4'. Make sure though that you've got unzip program installed to unzip the file. You can check if you've command installed using command 'whereis', 'whereis unzip'. It should show the bin directory path where command code is.

Now lets move index.php file out of CodeIgniter folder to our root using mv command 'sudo mv CodeIgniter-3.0.4/index.php /var/www/html'. Remember that there's only two main folders, 'application' and 'system', and we need to move these outside the root folder 'html' to 'www' so user cannot access them. We move those two folders to www using mv command again. 'sudo mv CodeIgniter-3.0.4/system CodeIgniter-3.0.4/application /var/www'.

That's our main job done, we can now delete codeIgniter folder as it's no longer needed. To delete the folder use 'rm' command with '-r' options, r stands for recursive. 'sudo rm -r CodeIgniter-3.0.4/'.

Lastly we need to tell CodeIgniter where our 'system' and 'application' folder is. This requires full path. Open index.php file and edit following variables $system_path, $application_folder and $view_folder. For example, my 'application' folder is inside var/www so I do 'gedit index.php' to start editing it, and replace '$application_folder''s value to /var/www/application.

Remember that permissions to edit, read and execute are required for root folder and read and write permissions for 'application' and 'system' folder and its subdirectories. You can learn more about in my permissions introductory tutorial. http://www.webdevtown.com/2016/02/what-is-file-permission-in-linux-and.html

So that's all you do to install CodeIgniter on linux! If there's any questions feel free to ask in comments.

You can read more about codeIgniter framework on their official website.
http://www.codeigniter.com/

Thursday, 11 February 2016

Learn new technologies daily

Learn new programming stuff daily

If you're a very beginner programmer and regularly asking advanced coders for help then I'm sure you must've noticed the wisdom these coders have where they tell you something that makes you wonder how does he knew this.. I wish I knew it. They seem to know stuff you never heard off, most of which is gained from experience. But as a beginner coder you too can learn new stuff. Most coders live in their own world and miss on plenty of stuff.

What you do is search for computer science dictionary online and read couple words daily, whatever is related to your field read more about it and explore more about that subject on google. This is a really good way to learn about new stuff for people who don't know many coders. Keep in mind though dictionaries contain thousands of words, you don't necessarly need to go from 1 to x. Read what ever interests you and apply it to your new projects. If it's all read it's a waste of time. Make sure to put everything to practice.

Sunday, 7 February 2016

Steel Plate user management system | Free | Git link

User management system script

I'm dropping this incomplete project I'd been working on to switch to something better that'd bring positive return of investment. So here it is for everyone, I've uploaded the whole project on github. It's obviously not completed but I'm sure people will reuse stuff in their projects. So goodluck everyone! and obviously if you've any questions about the code feel free to ask me I'd be happy to help you. Oh and if you've got any work feel free to message me.

Contains:
  • Login
  • Registration
  • Email confirmation
  • Bot detection
  • Mail to Inbot not spam
  • HTML mail support
  • Admin CP
  • Ban user
  • Ban IP
  • Add Captcha info
  • Update user details
  • IP ban check
  • Blacklisted IP's check
  • User ban
  • Multi check
  • Multi allow on IP feature
  • Google captcha support
  • Token use for secure submits
  • Secure form validation
  • BCrypt Support
  • Mobile friendly(Bootstrap)
  • Social login(Google) support
  • OOP clean code
https://github.com/phpdevsami/Steel-plate-user-manager

Thursday, 4 February 2016

What is File Permission in Linux and what's necessary to know as a programmer or developer

What is File Permissions in Linux and why I need to know about it as a programmer or Develope

Permission is a very important function to keep things organized and secure. Even as the owner you do not want to have full permissions over certain files. Imagine you're working on a project and somehow accidentally deleted the file before you'd the change to upload it to git or to some secure place. Same with other users, you do not want them to be able to accidentally or deliberately delete, write or execute particular files.

Permission modes

There exist 3 permissions or modes, and 3 type of people whom you may grant them.

Three permissions are: Read, Write and Execute and they're represented by r, w, x. They're always in the same order(you'll see when we use terminal). Read is where system will allow them to read/open the file. Write is permission to write/edit the file, and last buy not least Execute is permission to execute or run a file.

The 3 type of people I mentioned above are: Owner/User, Group and Other. Represented by u, g, o. Also there's All, means everyone indicated by letter a. Owner is the person/user who created the file, Group is collection of users, others is everyone else.

ls -l to see permissions

To check permission details just navigate to directory and type ls -l. The -l argument shows detailed information about the file including it's permissions. I typed the command and here's the permission it returned. drwxr-xr-x

Starting to read from very left to right, the d even dumbest person can can represents directory. Now lets go 3 letters at a time, keep the order in mind(u, g, o) first 3 letters rwx are for Owner, that's the permissions the owner has, he can read, write and execute. Next 3 letters are r-x, and they're for group, users in group can read, - means the permission is not set, ad execute. Lastly we've permissions for Others r-x, they can read, - permission is not set, and execute.

Along with permissions you'll also have other rows,  next to permission will be number of files or links in that directory, then then of the owner or user who owns the file, then the group the file belongs to.

nautilus

Permissions on ubuntu can also be set or changed using the Gui program nautilus, but only if you execute it as administrator. If you type sudo nautilus in terminal a new window file manager/exporer window will pop up where you can navigate to desired directory, right click on file or folder and set the permissions or each individual person.

chmod

chmod means change mode, it allow you to change permissions/modes on files and directories. The format for this command is "chmod options permissions filename". Options are arguments it can take, permissions are as usual r, w, x and filename you should already know. Note that options aren't always necessary.

Options for chmod

-f, --silent, --quiet suppress most error messages
-v, --verbose output a diagnostic for every file processed
-c, --changes like verbose but report only when a change is made --reference=RFile use RFile's mode instead of MODE values
-R, --recursive change files and directories recursively
--help display help and exit --version output version information and exit

Permission setting Example

Lets say I want to grant whichever group Documents directory belong to permissions to read, write and execute. Note that all the users will then be able to read, write and execute.

Here's what we do:
1. Enter chmod command first of all.
2. For permissions we tell who we want to grant permissions to , u, g, or o.
3. We use + or - symbol letting the command know whether we're setting or removing permissions. For example, if you want to give user permission to only read and execute, but not edit. We do +r-w+x.
4. We type the permissions.
5. Write file name at the end.

1. chmod | 2. g | 3. + | 4. rwx | 5. Documents
Here's my permissions for Documents folder "dr-xr-xr-x". Lets say I want to give:
- User permission to read, write and execute.
- Group permission to only read.
- Other permissive to only read aswell.

chmod u+rwx,g+r-w-x,o+r-w-x Documents

This will change the permissions as we needed. If you go ahead and do ls -l you should see these permissions for Document directory "drwxr--r--"

If you'd like to know more about chmod I suggest learning about using shorthand numeric values as permissions instead of messy -rwx+rx letters.

I hope this guide's been information. See you guys next time.