PHP file_get_contents usage

Below is a standard command in PHP to get contents of a file in a string variable.

file_get_contents($url)

Lot of people might wonder why would you get contents of a file in a string if you can get the contents using a simple Include statement.
I would like to add that it is not just about getting the content but also converting the content or processing it as per your requirements.
file_get_contents is a powerful tool and you can do a lot of things with it. I have only listed a few of them which I have tried using in my project.

Evaluating PHP code within the file
I have a CMS environment which does not execute a PHP code. I used the CMS to store my PHP code and used file_get_contents() to get the file contents in a string variable and then execute the file as PHP using the below method.

The above code first converts the file contents in a string and then executes the PHP from it.
Note that the above can also be done by a simple Include statement i.e.
include(“myotherpage”) and that will also execute the PHP code.
So you would wonder what am I achieving through this method?
Converting the file contents to a string variable, you can do a lot of other things as below

Showing all PDF document links with a PDF icon

You can then style the pdf id in a seperate stylesheet.

The above is probably not a good example to show what you can do with file_get_contents

Have a look at the below piece of code.
It will allow you to get content between a set of tags/text.

Furthermore, you can also replace URLs within the file

There are quite a few other cool things you can do with this.
However, note that these snippets of code will be processed every time you load the page. It first converts it to string and then does the processing and finally evaluates it as PHP. This can add a bit of overhead compared to a single Include statement. However, in my project I am using all the above tricks and I don’t see any performance problems and ofcourse you don’t need to convert to string everytime. You can convert the file to string, stack all the statements and then execute the PHP.

Select records from a table where no corresponding records available in other table

Sometimes there is a need to select records from Table A where there are no corresponding records in Table B

e.g. If you have a products table with productid as primary key and you need to find products which did not get sold in a particular month from the sales table you can use following query

 

Import CSV data to MySQL table

Here is a simple code to import CSV data into a MySQL table.
There is no need to use MySQL frontend.
Below is web based form to select a CSV file and import the records in a MySQL table

Step 1: add.php First of all create a form to select the file
Note that the values of the select box should match the table names in the MySQL database e.g. table_name_A, table_name_B, etc

Step 2: config.php Create a config file
Always a good idea to create a config file to declare your database connection and file import settings.

Step 3: import.php Import the data

 

How to show WhatsApp button only for mobile users on your website

Create a javascript file identifydeviceos.js with below code to identify the device OS

The above JS returns the userAgent based on the device. Call the above JS in your header.

Above JQuery just shows or hides the WhatsApp button based on the OS. The button needs to applied .whatsapp_btn class.

How to create a Prestashop module

Creating a new prestashop modules is not difficult as it seems. To create a simple module there are 4 easy steps to follow.

Let us create an image slider for the home page

1. Add a new row for the new module in the ps_module table

name: blockslider

active: 1

2. Create the .php file

Create a folder named blockslider in the prestashop modules directory and create blockslider.php file within the directory with below code.

3. Create the .tpl file

Here you can use any JQuery based slider or create your own. Just remember to copy the relevant JS and CSS files along with any images to the modules folder and just call the JS and CSS files in the .tpl file.

Create blockslider.tpl file in the same directory as above.

4. Transplant the module

Once the module is created, it will appear in the Front Office Features section.

Make sure it is installed and enabled

Just transplant the module to Homepage content

LDAP – Create a list of Groupwise users inside an OU

This is just an example illustrating how to connect to the LDAP and get users and groups within a particular OU.

  1. The first part connects to the LDAP
  2. Second part gets all the groups within the specified OU and puts them in an array
  3. Last part gets loops through the groups and gets all the users within each group and puts them in a array

Fill your LDAP connection parameters in this part

This part will get all groups within the OU. You may need to change OU=User Accounts part if your User Accounts are in a different OU e.g. Employee Accounts


This part will get all users within each of the groups


Echoes all the groups and users


 

MySQL remove duplicate data using DISTINCT

In order to explain how DISTINCT works let us see an example

Let’s say we have table of  books storing Book Title and Author details. It is possible that one author can write several books.

book_title——– author

Book A————–Author A

Book B————–Author B

Book C————–Author A

Book D————–Author C

Now we want to write a query which will return all authors

If we run following query

SELECT author from books

We will get

Author A, Author B, Author A, Author C

Note that Author A appears twice

To correct this problem we run below query

SELECT DISTINCT author FROM books

This will return

Author A, Author B, Author C

Clean up Microsoft Word mark up in HTML

People are so used to using Microsoft word to type their content that it is not practical to even think of asking them to change and start thinking from a web perspective to produce a clean HTML and standards compliant markup using Adobe Dreamweaver or some other useful editor.
But the basic purpose of a CMS is to decentralize the content uploading process and to be able to achieve this Microsoft Word is the biggest hurdle CMS users all over are facing.

Till date there is no foolproof solution to this problem. Tools like HTML tidy, htmLawed etc do a descent job however it largely depends on how those tools are configured.
HTML tidy can work at the Apache level which is the most cleanest way whereas htmLawed works at the application level.

I liked htmLawed as it is very simple to use and quite flexible to change its configuration for different applications.
Here are some steps to use htmLawed

  1. Download latest source code of htmLawed and unzip to a local directory
  2. Upload the file htmLawed.php
  3. Include the above file on the page where you will be entering the wysiwyg data in the CMS database using an include statement include(“/path/to/file/htmLawed.php”)
  4. Depending upon the type of application, you can use any of the sample configurations. For other configuration, read the documentation.
    a. For a blog application where you want to accept only a few tags in comments like strong, paragraph, links, underline and italics

    b. For the rest of the articles

How to solve “FATAL ERROR: Cannot redeclare class/function”

This error occures when you have a function or class declared more than once

Scenarios

1. I have a file with all the class declarations which I include after the header.php file. In my scenario it was not possible for me to include the class declaration file before calling the header.

Later on there was a need to access the class functions within the header file. When I included the class within the header.php file, I got this error as the class declaration happened twice.

2. In rare occasions there can be a class name declared more than once.

Solutions

1. Using  multiple includes: Class or functions file can be included multiple times.

2. Using IF statement:

 

Integrating Prestashop shopping cart with existing PHP -MySQL Authentication

Assumptions

Here we are assuming that you are using PHP – MySQL and you have an existing list of clients/users already maintained in a seperate table.

You have your own registration, login and logout mechanisms

Step 1: Prestashop cookies

First let us understand little bit about Prestashop cookies

A customer login prestashop cookie is as below

Cookie name: ps

Cookie content:id_guest%7C1%C2%A4id_customer%7C1%C2%A4customer_lastname%7CLASTNAME%C2%A4customer_firstname%7CFIRSTNAME%C2%A4logged%7C1%C2%A4passwd%7CMD5_PASSWORD%C2%A4email%7CEMAIL%C2%A4

Note: there can be some other parameters as well in the cookie but I have included only the ones which we are interested.

The cookie is ofcourse not readable. It is encrypted and decrypted using Rinjdael or Blowfish.

Apart from that there is also a SALT phrase which is added through the settings.inc.php file

Just for demonstration purposes I have removed the SALT phrase in settings.inc.php (in the config directory) as below

I disabled the cookie encryption and decryption process in Cookie.php file (in the classes directory) as shown below

Comment below lines

 

After doing the above changes, try to login again. If everything is done correctly, you should be able to login successfully. After you login the cookie generated will be readable.

Just to make the cookie bit more clear to us, let us run PHP urldecode

Here’s the output

id_guest|1¤id_customer|1¤customer_lastname|LASTNAME¤customer_firstname|FIRSTNAME¤logged|1¤passwd|MD5_PASSWORD¤email|EMAIL¤

Now this cookie is quite readable and easy to follow.

id_guest=1, id_customer=1, customer_lastname=LASTNAME, etc

Step2: Import your existing users to the prestashop customer table

Export all users from your database and include fields like memberid,firstname, lastname, username, md5(password), salutation and import it into the ps_customer table in prestashop.

Step 3: Single sign on

Once the import-export is done, achieving the single sign on is very easy

All you need to do is when a user logs in on your website apart from the usual cookie which you create, you also need to create one more cookie for the prestashop login (even better is to combine the cookie and name is as ps which might need a corresponding change in your authentication code). The format of this cookie would be same as the one obtained after the urldecode above in Step 1. Cookie name would be ps

Step 4: Enable cookie encryption

Note that we turned off all cookie encryptions in Step 1. We can turn on cookie encryption and decryption provided you are able to replicate the same through a function on your existing site when you create the cookie.

Step 5: Close all loops

A. Disable password reset through Prestashop and send users to your website password reset process and in that process make sure you update password in both the tables

B. When a new user registers make sure you add a new entry in the Prestashop table as well (ps_customer and ps_customer_group)

C. When people login on prestatshop, the form action URL can be changed so that it goes to your custom authentication code which enables you can create the necessary cookies