Check if a Certain Port is Occupied on Mac

How to check if a certain port is occupied on Mac?

CLI:

Open a Terminal window, and use the command below to check if 80 port is occupied.

Built-in Network Utility Tool

You can also use the Network Utility tool that every Mac comes with.

Just press Command+Spacebar to open the Spotlight Search. Then type the Network Utility to locate the tool. Click the keyboard return/enter key to launch the Network Utility app.

You will find there is a Port Scan tab at the top.mFor example, below I am using the tool to scan all the ports from 0 to 1023, which are also known as “Well-Known Ports”

Mac find port is occupied

How to Compress Images with Jpegoptim

Jpegoptim is a utility to optimize/compress JPEG files. You can compress JPG format image losslessly with it, or you can set a maximum quality factor to compress the images into even smaller files.

Install Jpegoptim in Ubuntu or CentOS

Ubuntu

Run the commands below to update the package list and install jpegoptim on Ubuntu.

You can check if the installation is successful by running command below to check the version.

Centos

Run the command below to update the package list and install jpegoptim on Centos.

If you are running into an error saying “no package available”, make sure you have repo epel installed and enabled because jpegoptim is a part of epel on Centos.
You can use the command below to list all the enabled repos:

If you don’t see the epel repo in the list, try the command below to enable it.

or install it

Now let’s see some common use cases below:

Losslessly Compress a Single Image

Compress Quality to 80% for all Images in a Folder

Compress Quality to 60% for all Images in a Folder That are larger than 500kb

If you will need a detailed user manual, here is a link https://www.systutorials.com/docs/linux/man/1-jpegoptim/

Magento 2 Update Product Attributes for All Products in a Certain Category

Update Product Attributes in Bulk Using Product Grid

Magento 2 gives you the ability to update product attributes in bulk. Just go to the product grid at back-end(Catalog > Products), and you can select multiple products for which you want to update the product attribute value. Then from the action drop-down menu(upper left), select “Update Attributes”. You will be brought to a product edit page, instead of updating a single product, this time you are updating for all products you have selected. Change the value of product attribute by clicking on “Change” checkbox right below the attribute you would like to update, then the attribute will become enable to accepts your updates. When you are done editing, click on save button, all products will be updated with new attribute value.

You can even apply attribute filters, e.g. availability, qty, product type, etc.

Magento 2 Update Product Attributes for All Products in a Certain Category

However Magento 2 does not allow you to search product by categories in the product grid, you are reading this post because you want to update product attributes for all products in a certain category, for example you want to disable all products in one category.

Below is a simple PHP script to do that:

  • $categoryId is the Id of the category
  • attribue_code is the attribute code(string)
  • $value is the attribute value, in the script above, I was updating a YES/NO attribute, 1 = YES, 0 = NO

Replace $categoryId, $value, attribue_code for your needs, then run the script from CLI, or directly execute it in your browser.

Magento 2 Different Ways to Load Products

How do you load product by product ID in Magento2?

Using Repository to Load Products – Recommanded

Using Factroy to Load Products

The reason to use ProductRepository’s instead of a ProductFatory’s to load the product is because the former is of a higher level than the latter.

SO, you should use the API layer whenever possible, because:

  • Api/Data layer is used in the Web Api, as well
  • models can – and probably will – be refactored at some point; Api/Data/Product will not.
  • To get a product in your classes, you need to inject either a concrete factory (ProductFactory) or an interface (ProductRepository). I don’t think you want your module to rely on anything but an interface. Hence I disagree with this type of injection.

Using Collection to Load Products

If you need to load multiple products, you should use prodcut collections.

Using Product Mode to Load Products – Deprecated

How To Install Redis and Configure Multiple Redis Server on Centos 7

What is Redis

You are reading this post, I assume you already have a bit idea what is Redis. But just a short introduction: Redis is an open source in-memory database. It supports basic data type like strings as well as advanced data structures like lists and hashes.Redis is very simple to setup and use. Below is simple instruction on how to install and configure Redis on CentOS 7.

Redis official documentation here

Installation

Run the command below to stall Redis server on Centos.

If you are running into an error saying “no package available”, make sure you have repo epel installed and enabled because redis is a part of epel on Centos.
You can use the command below to list all the enabled repos:

If you don’t see the epel repo in the list, try the command below to enable it.

or install it

Start the Redis Server

Now you can start Redis server and enable to auto-start on system reboot by using the commands below.

By default, Redis server listens on 6379 port, and you can run commands below to check if it is alive.

Install and Run Multiple Instance of Redis on Centos

Sometimes you will need multiple Redis instances, for example I was configuring a server for Magento 2 hosting, and I need 1 server for page cache, 1 for config cache, 1 for session storage.

You can run multiple Redis instances on different ports. By default Redis is installed under /var/lib/redis. This is considered as the work space for the default Redis insane. Memory dump is stored here. You will see a file named dump.rdb if there is any data dumped from memory.

First, to setup another Redis instance, we will need to duplicate the directory for a new instance. You can do so by running the commands below:

Second, we will need to create a separate configuration file. The default config file is /etc/redis.conf

And update configuration in the file to let the new instance run on different ports.

Lines need to be updated:

As you can see above, this time we are using port 6380 to host the new instance.
Then create separate service file for Centos.

Lines need to be updated in the new service file

Now it is time to start the new instance and enable to auto-start on system reboot just like what we did when we install the first instance.

Check the status or our second instance:

Now we have 2 Redis servers running separately on porst 6379 and 6380.

Linux How to Use du Command to List Files, Directories, and their total sizes

We know we can use ls command to list out all the files and directories under a given directory.

But it doesn’t give us size information. Sometimes, size information is important, for example we are examining a code repository, and need to find out and clean up large files and directories.

Linux List Files, Directories, and Their Total Sizes

du command, short for disk usage, can be useful in case like this. It is used to estimate file space usage.
To list all files, directories, and their total sizes, first cd to the directory, and run the command below.

It will give us an output like below:

List Files, Directories, and their total sizes

Linux List Total Size of Current Directory

To simply list the size of current directory, first cd to the directory, and run the command below.

It will calculate the current directory size:

Linux List Total Size of Current Directory

Linux List Files, Directories, and Their Total Sizes in Descending Order

We’ve already known how to list the size for all files and directories in a certain directory, but can we take one more step further to sort the results.

Then answer is YES! See the command below:

We will need to drop the -h argument, since it will give you human readable output, e.g. 10K, 2.5MB, and they cannot be sorted when we pipe the output to sort command.

Linux List Files, Directories, and Their Total Sizes in Descending Order

Linux Find the Top 10 Largest Files or Directories in Current Directory

We can pass the output from previous command to a head command to accomplish this.

Linux List the Top 10 Largest Files or Directories

How to Hide/Show Element with JavaScript and JQuery

How to toggle between hiding and showing an element using Javascript?

JQuery hide() and show() Methods

With jQuery, you can hide and show HTML elements with the hide() and show() methods:

The optional speed parameter can take the following values: “slow”, “fast”, or milliseconds.

The optional callback parameter is a function to be executed after toggle() completes.

Note: show() works on elements hidden with jQuery methods and display:none in CSS (but not visibility:hidden).

JQuery toggle() Method

You can also toggle between hiding and showing an element with the toggle() method.

Hide or Show an Element on Click

Want to trigger hide/show on a click event?

JQuery css() Method

Below is another example, show or hide an element depending on its current state. Instead of using toggle() method, we can also use css method and update the display attribute based on if the element is currently showing or hidden.

Pure JavaScript Implementation

Common PHP String Functions, Check is Empty, Split by Character, Length

Below are some common and useful PHP string functions.

PHP check if string is empty

PHP string split by character

PHP check if string contains

PHP check string length

PHP replace part of a string

PHP get substring of a string

PHP insert insert a string into another string at position

PHP trim spaces

Trim leading and trailing spaces from a string

Can Your Store Customer’s Credit Card in Magento

The question of whether you can store credit card information within Magento comes up a lot. The answer to this question is unfortunately not very clear when looking for an answer elsewhere including on the Magento website. To clarify the answer to this question, there are a few things that need to be understood including the PCI-DSS, PA-DSS compliance and the difference between them.

What is PCI-DDS?

PCI-DSS is the compliancy of your entire online environment which includes your systems, practices, software, etc. This is the standard that is required to be able to process on-site payments. A software application can never be “PCI compliant” by itself. Magento IS PCI-DSS compliant when the rules of PCI-DSS are followed which include:

  • Build and Maintain a Secure Network
  • Protect Cardholder Data
  • Maintain a Vulnerability Management Program
  • Implement Strong Access Control Measures
  • Regularly Monitor and Test Networks
  • Maintain an Information Security Policy

What is PA-DDS?

PA-DSS is a standard for *software applications* dealing with payment processing. PA-DSS was designed to provide the definitive data standard for software vendors that develop payment applications which can store cardholder data securely and prevent them from storing prohibited cardholder data (full mag stripes, CVV2 info, pin numbers, etc).

PCI-DSS and PA-DSS Compliance

PCI DSS standards apply to each merchant who accepts payment via credit cards. There are various rules regarding the securing of credit card information depending on whether you record it on paper, in a computer, or both, and physical security rules also apply if you have credit card terminals.

PA-DSS is the certification being required for commercial applications that process credit cards. This requirement is on the software developers, not the merchants.

An application can be PA-DSS compliant, but the environment may or may not be PCI compliant.

By using a 3rd party plugin or merchant whose software and system IS PA-DSS compliant that can store cardholder data on external systems (Magento Payment Bridge included), it removes the PA-DSS requirement from Magento itself and allows you to be PCI-DSS compliant. This of course holds true if and only if all of the systems and networks this cardholder data traverse are protected. In other words, let’s say that your Magento store isn’t storing cardholder data, but is instead using a PA-DSS compliant third party. Let’s then say that the connection between your Magento store and the third-party is not encrypted or you have debugging enabled for the payment gateway. In such cases, you would NOT be PCI compliant.

Is Magento PA-DDS Compliant

Magento by itself is NOT a PA-DSS certified application. To reiterate, Magento does have the built in ability to store cardholder data in its own database, but you will never be PA-DSS compliant in doing so which prevents you from being PCI-DSS compliant. The Magento application (at any level: CE, PE, EE) has not been PA-DSS certified. Remember, PA-DSS applies to software only, and not the infrastructure. Storing cardholder data in a non-PA-DSS compliant application like Magento will invalidate PCI compliance.

If you do want to store credit card data for any time frame, even if for only a few minutes, you must use either Payment Bridge or a 3rd party plugin / service which is PA-DSS compliant and stores the cardholder information on their servers for later retrieval. In addition to this, the entire flow of cardholder data must be secured. This means all debugging must be off and the connections carrying cardholder information must use some form of SSL/TLS.

Mageno PCI Compliance

Magento Enterprise Edition

Magento Secure Payment Bridge is the easiest way to make your Magento website PCI compliant. The solution is separate from the Enterprise platform, so you don’t need a full website to be compliant. Therefore, you can easily update your ecommerce store without affecting the compliance of Bridge.

The solution stores credit card data and sends a token to the Magento instance. The token makes your system secure, as payment bridge credentials are not enough for getting access to customer data. In case of threats related to your payment bridge, you just need to setup a new instance and get new credentials. Thus, credit card information will remain secure.

Despite the Secure Payment Bridge application meets the above PCI requirements, it is not enough to make your Magento website absolutely secure, since the app must be installed in a PCI DSS compliant environment.

Magento Community Edition
Unfortunately, Secure Payment Bridge is not compatible with this edition. But there are several ways to make your Magento website PCI compliant:

You can use a third party payment methods, for example PayPal express, Authorize.net, etc.

If you choose this option you won’t have to be PCI compliant yourself, because you don’t have to store credit card information on your server. In this case you have to consider that your customers will be redirected to the site of the payment processor and will have to leave your website, which might be inconvenient and interrupt the buying process.

Magento’s Saved Credit Card Option

Again, let me first start off with the fact that this is not a PCI-compliant solution. It’s not ideal. I’d say that this is for emergencies only. And it might even be too risky for your company.

Magento Store Credit Card Option

But if a payment gateway goes down completely, and you’re not able to take orders, my recommendation is to do a temporary bypass and turn on the Saved Credit Card Option.

Then, you can use a virtual terminal or other means to manually run the credit card for each order later.

Normally, the saved credit card option is something we use just for testing. It’s not as secure as using a payment gateway. It most likely goes against your merchant agreement that you signed.

But if a payment gateway like Authorize.net is down, my recommendation is to turn this on, save the credit card numbers on the site, and then run them when you are able.

You’ll want to then later remove the stored credit cards from the server, so that you can mitigate any risk of storing credit cards.

Create a Popup Overlay with CSS

Adding a popup to a website is a very common requirement when we design a website. In addition, some additional requests may include:

  • The popup should be centered both horizontally and vertically.
  • Using a fly-in effect.
  • It should overlay the existing HTML page and all elements.
  • Hide the if click detected anywhere outside of it.

In this post, we will follow a step-by-step walk-through to design a popup and meet the requirements above.

First, let’s start from placing a simple div that will center both horizontally and vertically.

Adding a div with id=popup in the body of the HTML code. Feel free to add some content in the div, of course.

Center the popup both horizontally and vertically

Using the CSS style below, we are first trying to center the popup.

Now take a look at the box model of the div, just to make sure the div is centered.

css box model

 

 

You can adjust the width and height for your div based on the content in your pop up. background-color property is set for purpose of testing.

 

Add a fly-in effect on the popup

Next, we can work on a fly-in effect by designing a fly-in class.

Add a Shadow Overlay

Sometimes you need your entire browser window with a black overlay in the background. So the popup in the front can draw more attention to the visitors.

We already added a fly-in class to the div to implement the fly-in effect. Now let’s create another class to do the show overlay.

Hide the popup when the user clicks outside of it

You may put a ‘Close’ or ‘X’ button in your popup, which is pretty straightforward.  So visitor can close it if they think it is disturbing.

In addition, to be more user-friendly, we want the popup to disappear from the screen when someone clicks anywhere outside the popup div. This is where javascript kicks in.

Put the javascript code below in the $( document ).ready()  function.

Basically, it adds an event handler for mouseup event on the HTML page. and if what is being clicked is not the popup div or any descendant element of it, then hide the it.