How to search string in all files in a folder and sub folder recursivelly

How to use “grep” command in linux or macos to find text inside all files in a folder and including subdirectories

Command for the same is
grep -rl "string to search" /absolute-path/to/folder/

-r (or –recursive) option is used to traverse also all sub-directories of /path
-l (or –files-with-matches) option is used to only print filenames of matching files
-i ( for ignore case sensitive)

If you’re looking for lines matching in files, then use the below command

grep -Hrn "string to search" /absolute-path/to/folder/

-H causes the filename to be printed (implied when multiple files are searched)
-r does a recursive search
-n causes the line number to be printed
/absolute-path/to/folder/ can be . (dot) to search in the current directory

php session handler using mysql on multiple server setup or load balancer setup

Many a time you have to host your website on multiple server such as load balancer to server high traffic website. It is obvious that if i have a load balancer or multiple servers in use, the user’s session is coming from 1 server so if the user is pushed to say server 2 or server 3 then does that mean the user’s session will expire or how to make it work as normal?

I have multi-session checks, so if a user logs-in from a different location it will auto sign them out like we see on bank websites. And this will affect the user journey and create issue to the users? Is there anything I need to do at the database level?

Yes you can do it by using custom session management using MySql or any other database.

Option 1: You can use the common recommended approach suggested by many tech gigs as here https://www.php.net/manual/en/function.session-set-save-handler.php

But sometime, the server configuration doesn’t allow to overwrite the server default session handler, Hence you need to go with some different approach.

Option 2: The way i found the solution.

I created a session handler class using mysql database, create method to start, write, read, unset and destroy the session variable.

Below is the example usage of the same.

Usage Example Just include this file at the top, where you want to manage the session using session data

require_once ‘handleSession.php’; // Make sure to set the Mysql Database credential appropiately

To set session data in DB

Case 1: Set a single session data, pass the key and value in array $cstmSessionHandler->cstm_sess_write(array(‘developer1’=>’1vinod’));

Case 2: To set two dimensional session data, pass the key and value in array as below $newData[‘aaa’][‘ffff’] = ‘1111’; $newData[‘aaa’][‘bbb’] = ‘222’; $cstmSessionHandler->cstm_sess_write($newData);

To read session data

Case 1: To read only a single session variable Pass the session key. $sessionValuesArr = $cstmSessionHandler->cstm_sess_read(‘aaa’);

Case 2: To read all the session which are active. Dont pass any argument $sessionValuesArr = $cstmSessionHandler->cstm_sess_read();

To unset specific session variable $cstmSessionHandler->cstm_sess_unset(‘aaa’);

To destroy all the session variable $cstmSessionHandler->cstm_sess_destroy();

You can download the source code and example from gitlab

https://gitlab.com/vinodkram/php-session-handler-using-mysql-on-load-balancer-setup

WordPress permalink pagination not working

Say for example i have created a custom post type. I have implemented pagination for the same.

When i call this url

http://localhost.dm/news/multi-media-gallery/page/2/, it show page not found.

But if i try with

http://localhost.dm/news/multi-media-gallery?page=2,  it work.

Now to make the pagination work with permalink you need to add the following rewrite rule in your theme functions.php

add_action(‘init’, function ()
{
add_rewrite_rule(‘(.?.+?)/page/?([0-9]{1,})/?$’, ‘index.php?pagename=$matches[1]&paged=$matches[2]’, ‘top’); flush_rewrite_rules();
}, 1000);

Jquery set input value and trigger on blur event

I want to trigger an event every time i click on select on any element by id

First i will try to set the input value by the following code

pickedvalue = jQuery(‘#someElementId’).val();

jQuery(‘#anotherInputElement’).val(parseInt(pickedvalue)); // use parseInt if the input type is “number” else not required.

After setting the new value to the other element i wanted to fire “on blur” event so to achieve the same use the below code.

jQuery(‘#anotherInputElement’).val(parseInt(pickedvalue)).trigger(“change”).blur();

 

–Happy Coding

Add default blank value for select option in contact form 7 wordpress

Normally when you use contact form 7 plugin , for dropdown select option, there is no option to set blank value for the first option to validate the select field.

For example, what we have in wordpress contact form 7

<select name=”your_field_name”>
<option value=””>—</option>
<option value=”yes”>Yes</option>
<option value=”no”>No</option>
</select>

But in actual scenario, i want to display the label for the first option as “–Select Value–” with empty value.
To make the same change, you can use the dropdown code as below
[select* your_field_name_ class:form-select class:required first_as_label “–Select Value–” “Yes” “No”]

In the above code “first_as_label” is important parameter. It instruct the plugin to use the first option as label with default empty value. So the generated output look as below

<select name=”your_field_name”>
<option value=””>–Select Value–</option>
<option value=”yes”>Yes</option>
<option value=”no”>No</option>
</select>

If you have any queries, you can write back to us or comment in the comment section.

How to change the php version for the console

Many a times when you view the php version in the browser by displaying the phpinfo() information and the php version that is displayed when you see by typing the command “php -v” in the console or command prompt, You will notice that there is difference in the two.

So how  to set the php version same as the version that you see in your phpinfo( ) function in the browser.

Step 1: Find the path to the executable of the php from the phpinfo() displayed in the browser. For example, if you have ampps installed you will find the path as “/Applications/AMPPS/php-7.1/bin/php”. 

Incase you have multiple version of php configured then choose the binary/executable path to the required version of php.

For example for php5, the path would be “/Applications/AMPPS/php5/bin/php”.

Step 2: Setting the php version for the console

Once you have decided upon the required php version for your console application execute the below command.

alias php=’/Applications/AMPPS/php-7.1/bin/php’ => This will set your php version to 7.1 for you console command line instruction.

Similarly for php5

alias php=’/Applications/AMPPS/php5/bin/php’

To make this work,You need to double check the location of the required php executable path, then only it will work. If you don’t have the required version installed. you could installed it as per your need and the operating system you are using.

 

How to download latest copy of a file from gitlab branch

I had code on my gitlab repository, Another person is also working on the same code . He had committed , pushed and merged his changes on the master branch. Now i just have to update only one file in my local working copy.

So this is possible by using the below two command.

git fetch
// git fetch will download all the recent changes, but it will not put it in your current checked out code (working area).

git checkout origin/masterpath/to/file
// git checkout <local repo name (default is origin)>/<branch name> — path/to/file will checkout the particular file from the downloaded changes (origin/master).

You can also pull specific files to your working copy from another branch as well. You just need to replace “origin/master” with your desired branch name.

When you fire git status command

git status
// it will show the fetched files as modified

issue with submit form in ajax success response

Usually when we try to submit the form after ajax call on success response, the form does not’t get submitted.

Like for example in below code:-

$.ajax({
type : “POST”,
url : “to/some/url”,
data : {
userName:userName,
mobileNumber:mobileNumber,
otp:otp
},
dataType : “json”,
success : function(data)
{
if(data.result == true) {
jQuery(“#form_id”).submit();
}else {
return false;
}
}
});

Now why this occur. Since you are calling the submit function from the ajax response, you need to remove the “submit” handler before submitting the form again. I.E. You need to submit the form without doing the ajax call again. Refer below:-

$.ajax({
type : “POST”,
url : “to/some/url”,
data : {
userName:userName,
mobileNumber:mobileNumber,
otp:otp
},
dataType : “json”,
success : function(data)
{
if(data.result == true) {
// Submit this form without doing the ajax call again
jQuery(“#form_id”).unbind().submit();
}else {
return false;
}
}
});