html select only one checkbox in a group

This snippet will:

  • Allow grouping like Radio buttons
  • Act like Radio
  • Allow unselecting all
// the selector will match all input controls of type :checkbox
// and attach a click event handler 
$("input:checkbox").on('click', function() {
  // in the handler, 'this' refers to the box clicked on
  var $box = $(this);
  if ($box.is(":checked")) {
    // the name of the box is retrieved using the .attr() method
    // as it is assumed and expected to be immutable
    var group = "input:checkbox[name='" + $box.attr("name") + "']";
    // the checked state of the group/box on the other hand will change
    // and the current value is retrieved using .prop() method
    $(group).prop("checked", false);
    $box.prop("checked", true);
  } else {
    $box.prop("checked", false);
  }
});
 src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">


 


  

Fruits

 


  
     type="checkbox" class="radio" value="1" name="fooby[1][]" />Kiwi
  
     type="checkbox" class="radio" value="1" name="fooby[1][]" />Jackfruit
  
     type="checkbox" class="radio" value="1" name="fooby[1][]" />Mango

 

 

Animals

type=“checkbox” class=“radio” value=“1” name=“fooby[2][]” />Tiger type=“checkbox” class=“radio” value=“1” name=“fooby[2][]” />Sloth type=“checkbox” class=“radio” value=“1” name=“fooby[2][]” />Cheetah

or

$('input[type="checkbox"]').on('change', function() {
   $('input[type="checkbox"]').not(this).prop('checked', false);
});

or

 
$('input[type="checkbox"]').on('change', function() {
$
('input[name="' + this.name + '"]').not(this).prop('checked', false);
});

Advertisements

Loading state button in Bootstrap 3

<button type=”button” data-loading-text=”Loading…” class=”btn btn-primary”>
Loading state
</button>

body {
padding: 10px;
}

$(“button”).click(function() {
var $btn = $(this);
$btn.button(‘loading’);
// Then whatever you actually want to do i.e. submit form
// After that has finished, reset the button state using
setTimeout(function () {
$btn.button(‘reset’);
}, 1000);
});

Virtual private network (VPN)

A virtual private network (VPN) extends a private network across a public network, and enables users to send and receive data across shared or public networks as if their computing devices were directly connected to the private network. Applications running across a VPN may therefore benefit from the functionality, security, and management of the private network.

CSRF Protection in PHP

Cross-site request forgery [CSRF] is a type of attack where a user is tricked/forced into performing an unwanted action on a friendly website that they are authenticated with. For example, if a user is logged into their bank and then visits a malicious site, it is possible that the malicious site can use the user’s session to make requests to the bank server. Essentially, the malicious script inherits the user’s credentials and authorization to the bank’s site and can act on the user’s behalf. Since every request that the user makes to the bank’s server includes the session and cookie data, a request from the user’s browser that is initiated from another site to the bank will include this information as well. Since a CSRF attack uses the user’s browser and session, the bank server cannot identify that the request is malicious.

A simplified example of a CSRF attack is a user being logged into their bank and then visiting a page that has this image element:

To combat these attacks, the Open Web Application Security Project suggests in their Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet to use the synchronizer token pattern. This method requires a unique random challenge token to be sent with each request that only the server can identify as being a valid request and can be sent to the server with a form using a hidden form field or included as a variable in a request. It is also critical that the token be used only in POST requests, so that it is not exposed, such as in the referrer section of an http request to a malicious site or when a user copies and pastes a url to share with a friend.

A common technique is to use a unique algorithm using the session id combined with the form/request that is being validated, meaning all tokens are form/request specific and expire when a user logs out. Using the session id alone would not be enough, since it can be discovered and used. One algorithm to generate a token could be concatenating the name of the form/request with the session id and running that through a hashing function like md5 or sha1 like this:

This can be taken one step further and add a secret key to make the token that much more difficult to duplicate:

The next step is to add this token into the form using a hidden form field:

Now to validate the incoming form is valid, just check the token:

 

While your site may or may not be a high CSRF target, CSRF prevention is very easy to implement and should be used in any application that handles a form or request. CSRF prevention must go beyond using just a static secret key or limiting form submission to POST requests, both of these solutions are still vulnerable to CSRF attacks. A unique identifier that cannot be predicted is critical to successful CSRF security. It is also important to safeguard all POSTed requests, not just form submissions.

Composer

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

Dependency management

Composer is not a package manager in the same sense as Yum or Apt are. Yes, it deals with “packages” or libraries, but it manages them on a per-project basis, installing them in a directory (e.g. vendor) inside your project. By default it does not install anything globally. Thus, it is a dependency manager. It does however support a “global” project for convenience via the global command.

This idea is not new and Composer is strongly inspired by node’s npm and ruby’s bundler.

Suppose:

  1. You have a project that depends on a number of libraries.
  2. Some of those libraries depend on other libraries.

Composer:

  1. Enables you to declare the libraries you depend on.
  2. Finds out which versions of which packages can and need to be installed, and installs them (meaning it downloads them into your project).

Basic Usage Of Doctrine With CodeIgniter

Lets do a contact form example to understand how to use doctrine ORM within CodeIgniter. Lets create a table on our MySQL database named ‘pd_contact’ as like follows:

mysql contact table schema example

This will generate and entity class as like follows:

<?php
/**
* PdContact
*
* @Table(name=”pd_contact”)
* @Entity
*/
class PdContact
{
/**
* @var integer $id
*
* @Column(name=”id”, type=”integer”, nullable=false)
* @Id
* @GeneratedValue(strategy=”IDENTITY”)
*/
private $id;

/**
* @var string $name
*
* @Column(name=”name”, type=”string”, length=50, nullable=false)
*/
private $name;

/**
* @var string $email
*
* @Column(name=”email”, type=”string”, length=50, nullable=false)
*/
private $email;

/**
* @var string $subject
*
* @Column(name=”subject”, type=”string”, length=100, nullable=false)
*/
private $subject;

/**
* @var text $message
*
* @Column(name=”message”, type=”text”, nullable=false)
*/
private $message;

/**
* Get id
*
* @return integer $id
*/
public function getId()
{
return $this->id;
}

/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}

/**
* Get name
*
* @return string $name
*/
public function getName()
{
return $this->name;
}

/**
* Set email
*
* @param string $email
*/
public function setEmail($email)
{
$this->email = $email;
}

/**
* Get email
*
* @return string $email
*/
public function getEmail()
{
return $this->email;
}

/**
* Set subject
*
* @param string $subject
*/
public function setSubject($subject)
{
$this->subject = $subject;
}

/**
* Get subject
*
* @return string $subject
*/
public function getSubject()
{
return $this->subject;
}

/**
* Set message
*
* @param text $message
*/
public function setMessage($message)
{
$this->message = $message;
}

/**
* Get message
*
* @return text $message
*/
public function getMessage()
{
return $this->message;
}
}

Gas ORM

A lightweight and easy-to-use ORM for CodeIgniter. Gas was built specifically for CodeIgniter app. It uses CodeIgniter Database packages, a powerful DBAL which support numerous DB drivers. Gas ORM provides a set of methods that will map your database tables and its relationship, into accessible object.