Posts

What is Laravel localization? When we can us this?

When we are developing a multi-language site at that time we use laravel localization. Localization in Laravel in it's simplest term means changing the application's default language to the language preferred by the user. There are two different ways we can achieve localization in Laravel: 1. Using Short Keys 2. Using Translation Strings as Keys

What is the difference between self and static in PHP?

The main differences is that static allows late static bindings. One of the most useful scenarios that I found was for creating Base classes for Singleton Classes: class A { // Base Class     protected static $name = '';     protected static function getName() {         return static::$name;     } } class B extends A {     protected static $name = 'MyCustomNameB'; } class C extends A {     protected static $name = 'MyCustomNameC'; } echo B::getName(); // MyCustomNameB echo C::getName(); // MyCustomNameC Using return static::$name in the Base class will return what was statically attached when it was extended. If you were to use return self::$name then B::getName()  would return an empty string as that is what is declared in the Base class.

Why we need interface in PHP?

An interface allows unrelated classes to implement the same set of methods, regardless of their positions in the class inheritance hierarchy. An interface enables you to model multiple inheritance because a class can implement more than one interface whereas it can extend only one class. Interfaces are 100% abstract classes – they have methods but the methods have no ‘guts’. Interfaces cannot be instantiated – they are a construct in OOP that allows you to inject ‘qualities’ into classes .. like abstract classes. Where an abstract class can have both empty and working/concrete methods, interface methods must all be shells – that is to say, it must be left to the class (using the interface) to flesh out the methods. Interfaces allow you to define/create a common structure for your classes – to set a standard for objects. Interfaces solves the problem of single inheritance – they allow you to inject ‘qualities’ from multiple sources. Interfaces provide a flexible base/root s

What is autoload in PHP?

Autoloading is the process of automatically loading PHP classes without explicitly loading them with the require() , require_once() , include() , or include_once() functions. It's necessary to name your class files exactly the same as your classes. As of PHP 7.2.0 the __autoload() function has been deprecated. Now it is recommended to use the spl_autoload_register() for that purpose instead.

How can we prevent XSS attacks?

Preventing cross-site scripting is trivial in some cases but can be much harder depending on the complexity of the application and the ways it handles user-controllable data. In general, effectively preventing XSS vulnerabilities is likely to involve a combination of the following measures: Filter input on arrival - At the point where user input is received, filter as strictly as possible based on what is expected or valid input. Encode data on output - At the point where user-controllable data is output in HTTP responses, encode the output to prevent it from being interpreted as active content. Depending on the output context, this might require applying combinations of HTML, URL, JavaScript, and CSS encoding. Use appropriate response headers - To prevent XSS in HTTP responses that aren't intended to contain any HTML or JavaScript, you can use the Content-Type and X-Content-Type-Options headers to ensure that browsers interpret the responses in the way you intend. Co

How does XSS work?

Image
Cross-site scripting works by manipulating a vulnerable web site so that it returns malicious JavaScript to users. When the malicious code executes inside a victim's browser, the attacker can fully compromise their interaction with the application.

XSS - cross site scripting

What is the difference between XSS and CSRF? XSS involves causing a web site to return malicious JavaScript, while CSRF involves inducing a victim user to perform actions they do not intend to do. What is the difference between XSS and SQL injection?  XSS is a client-side vulnerability that targets other application users, while SQL injection is a server-side vulnerability that targets the application's database. How do I prevent XSS in PHP?  Filter your inputs with a whitelist of allowed characters and use type hints or typecasting. Escape your outputs with htmlentities and ENT_QUOTES for HTML contexts, or JavaScript Unicode escapes for JavaScript contexts. How do I prevent XSS in Java? Filter your inputs with a whitelist of allowed characters and use a library such as Google Guava to HTML-encode your output for HTML contexts, or use JavaScript Unicode escapes for JavaScript contexts

Difference between jQuery parent() and parents() method?

Image
The basic difference is the parent() function travels only one level in the DOM tree, where parents() function search through the whole DOM tree. To understand this, let's look at the below-given HTML code. When you make a call to parent() function like $("#spnText").parent() It will give you "P" as the output.parent() function selects the first parent in the DOM tree. Now,if we call parents() function like $("#spnText").parents() It will give all parents in DOM tree which are, p->dvChild->dvParent->form-> body->html. You can pass a filter in parents() function  as well to select specific parent like if you want to select both the divs then $("#spnText").parents('div');

What are magic methods in PHP?

PHP supports multiple magic methods, those methods can be identified by two underscore prefix(__).  These are special functions should be defined by the user but no need to call them explicitly. It will be called on an appropriate event occurrence. For example, class __construct() will be called while instantiating the class. PHP magic methods must be defined inside the class. Note : Declaring the constructor method private prevents external code from directly creating an object. This is handy for creating singleton classes that restrict the number of objects that can exist. PHP Magic Methods and Purposes Below are the magic methods invoked on creating Class Instance - __construct() -  The __construct() method is most commonly used magic method. Here you can do initialization you need when an object is created. You can define any number of arguments that will be passed when creating objects. __destruct()  - The __destruct() method is called when the object is destroyed

Traits in PHP

What are the Traits in Laravel? Traits are a simple group of methods that you want to include in another class. Why we use Traits? A Trait, like an abstract class, cannot be instantiated by itself. The trait is created to reduce the limitations of single inheritance in PHP by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.