10/6/15

How to Create Patch in Google Summer of Coding | Learn-Fast

In Order to submit Patches for Google Summer of Code , you must be comfortable in reading source code and finding Bugs/issues in them.Well every Organisation will have a Bug Tracker System that has a set of Solved and Unsolved Bugs/Issues which are usually labelled with their difficultly Level ,like first-Timer, Easy, Medium or Hard .In this Post we 'll be covering How to Create a Patch after you detected and worked on some Bugs / Issues .

Learn to Create Patch in Google Summer of Coding


A Patch is simply a file usually a text file that has some additional information that you have added after manipulating your original file .

If you made some changes to your original Code and wanted to share them with the Core Developers of the community (Organisation) then it is very helpful to provide them a Patch file .

Now we know what is a patch , lets create one . Patches can be created in many ways .Some of them which are often used in Google Summer of Coding Contests are listed below :
  • Create Patch using Diff 
  • Create Patch using WinMerge 
  • Create Patch using Git 

Create Patch using Diff :

Diff is simple Linux command that requires two copies of code or a file .The first is the Original code or a file and later is the modified (With some changes you made) Code or a file .

To illustrates this , we put the below code in the Foo.java File .

import java.util.*;
public class Foo {
   public void main(String[] args)
   { 
     System.out.println("Foo");  
   }
}
Next , we modify the above code and save them in Foobar.java .

import java.util.*;
import java.util.*;
public class Foobar {
public void main(String[] args)
{ 
    System.out.println("Foobar Foo");  
}
}

To trace what changes you have made , just type in the Linux terminal

diff -u Foo.java Foobar.java > PatchforFoo.patch
 
The above Code will create patch file called PatchforFoo which shows difference in contents


import java.util.*;
--- Foo.java 2015-10-06 09:40:14.525429605 +0530
+++ Foobar.java 2015-10-06 09:43:29.365427829 +0530
@@ -5,7 +5,7 @@
 public void main(String[] args)
 { 
-System.out.println("Foo");  
+System.out.println("Foobar Foo");  
 }

You can also apply patch in Source Tree or directory .Type in the Linux Terminal

diff -Naur /usr/src/originalfolder  /usr/src/modifiedfolder > patchforgsoc.patch


All those modified differences are placed in patchforgsoc patch file .

Create Patch using WinMerge :

WinMerge is a Simple Open Source GUI based Tool for Windows that is used to compare folder contents and merging. To start with ,first you need to download the WinMerge Software . To create the patch ,click FILE->Open and Open two versions for comparisons .then click TOOLS->Generate patch .

Create Patch using Git :

Many Organisations use Git Version Control has it is more secure and use distributed system for concurrent access . If you are new to Git version Control Try Git . 

To create patch in Git ,just open the Git console and if you want to create a patch file for last commit ,type git show > patchforgsoc.txt .

 If you want to create patch for 2 commits you can type git diff commit1 commit2 > patchforgsoc.txt
So modified differences are placed in patchforgsoc.txt text file .

In general the git patch format looks like this git format-patch [options] . Check the detailed explanation for git patch Options here.


7/19/15

Login Password Protected Page using Sessions | PHP

This Tutorial will teach you to control and protect the access of any web page using PHP Sessions. We basically store some reference or item in the web browser and it is used every time when the user navigates from one web page to other web page , both pages of the same website. So for the first time the user has to go through login and later the user have to login only if browser content is cleared or the user's session expires.

Login Password Protected Page using PHP Sessions


Agenda : 

  1. Set Session Global Variable in your PHP Login Page .
  2. Create checkSession() Function to Validate user is already logged in or not.
  3. PHP Script that is added to every Web Page that you want to Protect from unauthorized Access

Set Session Global Variable in your PHP Login Page .

Just go to your Login Page . Add session_start(); function before all of the other Codes . 

Login Password Protected Page using PHP Sessions

 Next we store item like username or email etc in the Global Session variable .

If your using MySQL Database or any Similar Database to check the Login credentials. Write a query to get username or email or any other item that you want to store in your Session.

For example : If I Want to Store Username in the Session Variable ,please see the below Code.

 $query_session = "SELECT username FROM Table_Name WHERE (email='".$email."' AND password='".$password."')";
 $result = mysql_query($query_session);
 $result_session = mysql_fetch_array($result);
 //Assign the result of this query to SESSION Global Variable named username
 $_SESSION['username']=$result_session['username'];


Session Global Variable $_SESSION['username'] or $_SESSION['email'] is used to Store with Database retrieved Data .

Now save your Login Page as .php file .Any other format will not work or render properly . Don't Worry if it is in .html format or any other format ,the (PHP) Server will render html format or any other format as-well .

Create checkSession() Function to Validate user is already logged-in or not 

<?php
// This is a Generic Session Function to check Session username or Email or variable
function checkSession()
{
if(!isset($_SESSION)){ session_start(); }

         $username=$_SESSION['username'];
         
         if(empty($username))
         {
            return true;
         }
         return false;
}
?>

Save the Above Code as Session.php . If  $_SESSION['username'] or $_SESSION['email'] or any other reference or item that you've store in the step 1 is empty  then return true or else return false.

PHP Script that is added to every Web Page that you want to Protect from unauthorized Access


<?PHP
require_once("session.php");
if(checkSession())
{
    header("Location: http://your_Login-in_page.php");
    exit;
}
?>

Above Code must be added to first Line of any Web Page that you want to protect it from unauthorized access. The checkSession() will return true if $_SESSION['username']; is empty which means the user is not logged-in ,So send that user to Log-in Page . If checkSession() functions returns false then the user is logged-in which means $_SESSION['username']; Global Session Variable is not empty . Please See the Image below .

Login Password Protected Page using PHP Sessions

Please note :Save your file as .PHP format.

All done .Now any Web Page which has checkSession() PHP script installed is protected from unauthorized access .

Updated :

Disclaimer: Storing username & password directly as plain-text is not good practice and may lead to security threats , so MD5 (encrypt) them then store it .
This is just a plain simple concept of using sessions with PHP .


Also Read :  How to Extract the IP Address of the Visitor (Client)