ad

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.

Saturday, 23 January 2016

Learn linux interactively for beginners

Knowing how linux works is really an important skill to have, as a website developer you'll often have to use linux operating system, install GUI, setup web development or general environment where you can run a web server, we all know majority of servers run on linux OS so keep that in mind too.

Website I'm about to introduce to you really makes learning linux commands and get basic idea of OS pretty simple for beginners. Linux might seem complex but you'll really be using same commands over and over again. The most important thing to understand how it work and differ from yur day to day operating system, once you understand that, it'll just come down to googling for commands and executing them.

You gotta know what Kernel is.
Kernel is a nothing but a program  that helps hardware interact with software. So basically there's applications, then Kernel in the middle and hardware and other devices at the bottom. It's a basic layer model.

Do you know what a shell is in linux?
Shell is just an interface like you've in windows, it's basic screen with text written on it where you can enter commands. If you don't like shell you can just install some interface and use it instead. But shell give you more flexibility and it's must you know how to use it. There's plenty of shell softwares available for linux, common one is BASH.

You'll keep hearing about UNIX
Unix is an old operating system that linux is based on.You could say Linux OS is version of Unix OS. That's pretty much all you need to know.

That's basic info there, you can learn more about linux here interactively
http://linuxsurvival.com/wp/?page_id=5&id=0



Writing clean code PSR standards

Writing clean code is all about following conventions and standards that are generally understood by others. Imagine writing a program and sharing the source code with other developers out there, they will often go through your code and start scratching their head wondering what the hell this chunk of code does, trying to fix something they break everything else. Writing clean and tidy code is all about not making them tightly coupled(shouldn't depend on each other so badly that it breaks entire program if something's missing), write clean code that a somewhat experienced program should be easier able to read and undnerstand, and finally follow coding standards. I personally follow PSR 0, 1 and 2 standards.

PSR 0 is about auto loading and class names
PSR 1 is basic coding standards
PSR 2 extend and contrinues talking about standards.

Before you start reading PSR standards it's important you know what you must do, what's just a recommendation etc.. generally most standards use certain words what they mean is defined in RFC 2119 document http://www.ietf.org/rfc/rfc2119.txt

Once that read you should start reading the standards starting from 0. It's upto individual needed but those first 3 are must for anyone.

Example(Code must not product side effects, it should either declare class/functions or produce side effects - change variable value, echo something etc), if you're including a file it shouldn't just run and echo stuff on screen and run methods etc.. it should do one or another.

What you shouldn't do:
// side effect: change ini settings 
ini_set('error_reporting', E_ALL); 

// side effect: loads a file 
include "file.php"; 

// side effect: generates output 
echo "<html>\n"; 

// declaration function foo() {
 // function body 
}

Declare function is okay but echoing HTML in same file is not. Should only have function declaration in one file and other stuff in another. Upon including the file, your class shouldn't start executing unrelated logic that affects other code on page.
// declaration 
function foo() {
 // function body }

 // conditional declaration is *not* a side effect 
if (! function_exists('bar')) { 
function bar() { 
 // function body
 }
 }

You can read more about accepted PSR standards
https://github.com/php-fig/fig-standards/tree/master/accepted