PHP CKEditor Installation and Uses

Write a designable content not possible through HTML form field. That’s why JS create editor which helps user to write designable content in website. Like Gmail use Closure Library JS editor, WordPress support TinyMCE WYSIWYG editor same as PHP support CKEditor, WYMeditor, FCKeditor.

Using this editor user can write blog easily with picture colorful content, admin can add product description with table design, product specification. Admin can write their website content easily by this editor like About Us page content, Privacy Policy, Contact Us, Terms & Condition, etc.

How to Install CKEditor

CKEditor is one of the best useful content editors. It is open source free editor. First need to download this editor.  this is the download link.

After downloading move rar file in the project folder and extract.

Using CKEditor in the project with example

CKEditor for php projects

Use CKEditor in any php script with include external js file ckeditor.js and add class attribute with value ckeditor  in a textarea form element.

After that editor is return in the browser.



CKEditor Example

In this example use content_editor table in the test database.


This is the table structure. Content column always set longtext so user can store large data in this field.

When save editor content data will save in the database with the design of the content not only value that’s why when fetch the database data its return same design as it is.

Mysql Database table

That’s why this editor is very user friendly and useful for website. 

PHP Creating Image

In PHP there have many predefined function which helps to create background image and write text in the image.

Like create CAPTCHA image for website. 





Firstly if user write any image code in PHP script need to set content-type:image/jpeg.

Because by default PHP script content type is text/html. Browser only read text value so if user display image using PHP then header(“Content-Type: image/jpeg”) must be set at the top of the code.

In this way whole PHP file content set for image not text.

If any user forget to set header(content-type:) then browser return image as a unreadable text

  1. imagecreatetrucolor(width,height) is a PHP function which create a background image with black color.400, 70 is the width and height value of this background image. All parameters are required.
  2. imagecolorallocate(backgroundimage,red,green,blue) is a PHP function which set the color value through RGB(Red, Green, Blue) format.    First parameter is the background field and second parameter is red value (0,255) third parameter is green value (0,255) and fourth parameter is blue value (0,255).In the above example red value is 52, green value is 237 and blue value is 214. All parameters are required.
  3. imagefilledrectangle(backgroundimage,x-coordinator,y-coordinator,width,height,color) this function is return the rectangle field on the black background image.The first parameter is background image value, the second parameter is X-coordinator value means according to X-axis where the rectangle field is create on the background image, third parameter is Y-coordinator value means according to Y-axis where the rectangle field is create on the background image, forth parameter is width value of the rectangle field, fifth parameter is height value of the rectangle field and sixth parameter is color value of the rectangle field. All parameters are required.
  4. imagettftext(backgroundimage,textfontwidth,textfontangle, x-coordinator, y-coordinator,textfontcolor,textfontstyle,textvalue) this function write any text value on the image. All parameters are required.
    1. backgroundimagewhere text value is print.
    2. textfontwidth – text font width. In the above example font width is 30px.
    3. textfontangletext font angle means which angle text font display on the image. User can give positive value and also negative value. In the above example no angle is use so put 0 in this parameter.
    4. x-coordinator – x-axis’s position of the font. In the above example x-axis is 80
    5. y-coordinator – y-axis’s position of the font. In the above example y-axis is 50
    6. textfontcolortext color means text will be display on the image with which color. Color value set by imagecolorallocate() function. In the above example use 254,7,82 RGB value.
    7. textfontstyleuse font file for text font style. Download external font or copy windows font in the project location.
    8. In the above example use windows font and rename it with 4.ttf
    9. textvaluethe original value which user want to write on the image. In the above example “PHP is Amazing”.
  5. imagejpeg($image) this is the function which return the image in the browser. If user save the image in the directory then use imagejpeg($image, “image.jpg”). After execute this code one image file save in the project location with the name image.jpg

Using above five functions user can create any image file in web.

One application example

PHP image creator


Image resize by PHP

A website mainly creates by images and contents. Website is a virtual things where real things display by images. So if any website has lots of high quality images then the site load very slowly even net speed is high. When anyone open any site first and quickly display content then images and then videos if there. So it’s very important create a website light weight but images are same high resolution.

Using some PHP function easily resizes images without changing the original resolution of the image by changing the image original width height.

And also can resize image without change height width by changing the resolution of the image.



Return new.jpg image with new width 250px and new height 250px and the file size is 9.60kb which is very small than predefinedoriginal image size.


imagecreatefromjpeg(‘image file location’) this function is used to receive original image.

In the above example “Desert.jpg” is the original image with 1024width 768height. The image size is 826kb. $img variable is store “Desert.jpg” image.

If the image file extension is change from jpeg or jpg to png or gif then function name also change according to file. Like use imagecreatefrompng() if image is png image. use imagecreatefromgif() if image is gif image.

imagecreatetruecolor(width, height) create a background with allocate width and height value and black color.

imagecopyresized(new image value, original image value, new image x-axis value, new image y-axis value, original image x-axis value, original image y-axis value, new image width, new image height, original image width, original image height). All parameters are required.

This function resizes the image according to new width and height value.

imagejpg(new image value, name of new image). This function returns new jpeg or jpg image in the project location.

If user wants to return png image then the function is change with predefined()   same as gif image use the ().

PHP Image Compression

Resize image without changing width and height.

Note:70 is the resolution quality of the image. 0 to 100 is the value of this parameter.  0 is low quality image and 100 is the high quality image.

By changing the resolution quality can be change the image original size.

PHP Date and Time

Current date and time can get using PHP function.
User registration time or want to know the user last login time, booking date or time for order product etc all are possible to get by date() function in PHP.

Date function syntax



date() function has many different parameters. All different parameters have different meaning. Like in above example

D is return today’s day value in first three letters
d is return today’s date value with leading zero.
m is return current month value in numeric format with leading zero.
y is return current year value only last two digit.
h is return current hours value with leading zero in 12 hour format.
i is return current minute value with leading zero.
s is return current second value with leading zero.
a is return am or pm value of time in 12 hours format.

Note: date() function always return machine time. If machine time is set wrong then its return wrong time and date. And also it always returns to Europe/Berlin the time. Because in server setting file php.ini file have set date.timezone=Europe/Berlin.

Set current timezone then in PHP have a function to set current location time zone for particular one application. If anyone want permanently changed timezone the set date.timezone = Asia/Kolkata(or any preferred location) in php.ini file and restart the xampp or wampp.


date_default_timezone_set() this function is used to set particular timezone for particular one program where it is run.


Date function has any other parameter to return date and time value in different format.

Sr No

jReturn date value without leading zero

lReturn day value with full textual representation
Sunday to Saturday
Return day value with numeric representation
1 to 7 (Monday to Sunday)
Return English ordinal suffix for the day of month in two characters st, nd, rd or th
Return day value with numeric representation0 to 6(Sunday to Saturday)
Return day value of the year0 through 365
Return week value in numeric format of year42nd week in the year
Return month value with textual representationJanuary to December
Return month value with text format only three lettersJan to Dec
Return month value in numeric format without leading zero1 to 12
Return number of days in the current month
28 or 30 or 31
Return current year is leap year or notIf leap year the return 1 otherwise 0
Return current year value with four digit 2000 or 2018
Return am or pm in uppercase AM or PM
AM or PM
Return 12 hour format of an hour without leading zeros1 to 12

Return 24 hour format of an hour without leading zeros1 to 24
HReturn 24 hour format of an hour with leading zeros01 to 24

Time function in PHP

time() function is return total time value in the number of second. The Time calculate from 1st January 1970,


Return date and time using time() function

Return current time as a UNIX timestamp.


strtotime() in PHP

strtotime() function convert English textual date-time value to a UNIX timestamp. Using this function can return any past or future time or date value.


date_diff() function in PH

The date_diff() is return the difference between to date.


date_create() function return new DateTime object.
%R return + or – sign to denote the difference.
%a returns actual result.

Sending Email using PHP Mail

Send email by PHP is very easy and without cost. PHP gives mail() function to send email.
mail() function Syntax:


Benefits of mail() in web application:
1. Customer enquiry details send admin email-id directly.
2. Send customer notification message to their email.
3. Send OTP to user email for forgot password request and send recovery link to set new password.
4. Email verification link to user email account.
And so many others benefits for mail() function.

Mail function with HTML header:

//About MIME
MIME (Multi-Purpose Internet Mail Extensions) is an extension of the original Internet e-mail protocol that lets people use the protocol to exchange different kinds of data files on the Internet: audio, video, images, application programs, and other kinds, as well as the ASCII text handled in the original protocol, the Simple Mail Transport Protocol

Send mail with attached file using PHPMAILER:
PHP mail attachment script to send email is below

class.phpmailer.php is a open source php class file which use to send attached file via mail function. User can download this file from Google it’s free. All functions are defined in the class.phpmailer.php file.

Send Mail via PHP then program file must be uploading in the online server. User can use free host by registering in 000webhost site. Mail function only perform online not localhost.
If user want test mail function in loclhost the few configurations are want to set in php.ini file.
Find mail function in php.ini file. There is some code like this,

Replace the code with user details.

Then change PHP SMTP mail configuration like this in the sendmail.ini file.

Then restart XAMPP control panel then use mail function.

PHP Session

What is Session?
Session means a period of time. Day start from 12.00AM and end 11.59PM, during this period 12.00AM to 3.59AM is a mid-night session, 4.00AM to 7.59AM morning session, 8.00AM to 11.59AM day session and similarly goes on.

In PHP session is much similar to this. When a user opens an application do some changes and closed it, it’s also a session, the user session in this application.

Purpose of the session
The computer knows the user, who open it or which activities are done and then close it by cookies. But HTTP has no way to identify the user because HTTP is stateless.

When at a time many users come in any website HTTP does not recognize who is come and what they do?
That’s why PHP gives SESSION. When user1 open any website through browser one unique session id create in the server for the user1 browser and then user2 open same website through another browser create another new unique session id in the server for user2. In this way, the above problem can be solved by session.
In PHP any value (using variable) can store in a server for certain time and use it in multiple web pages in one web application. Session creates the unique id for every user in the server and using those unique id servers knows the user and their activities.

Syntax of Session
Use session in the program always starts the session at first. session_start() is a function which used to start a session. Store value in the session using $_SESSION global variable.








All session delete.

In this above example when set.php run on the server a session file create with sess_5bcttfn143tev4uaj835t3e041 name. 5bcttfn143tev4uaj835t3e041 this is the unique id where session value saves on the server.
Session file creates in the server tmp folder. In every browser, this unique id is changed. If user open browser and run set.php then another session id create in the server.
session_id() function return the current session id value on the browser.
Using session any variable easily access multiple files by $_SESSION global variable so above example file1 and file2 is used to the show this. User can use $_SESSION[‘fstnm’] and $_SESSION[‘lstnm’] variable any other pages also in this application.
Session expiry time in the server until user closed the browser.
If the user closed the browser and the run file1.php or file2.php again then the value will be blank only return

Set | File1 | File2 | Unset | Destroy

File 1 User name

If again user wants to set session then set.php runs in the server.

Session variable Unset and Destroy
session_destroy() function is used to manually destroy the session.

In above example, if the user runs destroy.php in the server the session file delete from tmp folder and all session variables are empty.

If the user does not want to delete all session the unset() function is used to empty particularly any session variable.
In the above example, unset.php is shown this.

You have already seen PHP Cookies. Lets see the what is the difference
Difference between Session and Cookie:
Session and Cookie is much similar in using purpose in PHP because both can store value one time and access multiple pages by the global variable in any one application.
But session and cookie both are worked in different field. Cookie worked in browser and session in server.

1Session is server side. So session is secured than cookie. Session value stored in the server, so difficult to directly access session file.Cookie is client side. If anyone wants to know the cookie value then easily access from browser and also can modify it directly in the cookie file.
2In session can store any type of value. In cookie can’t store array.
3Session file size is large then cookie. There are no size limit to store data in sessionCookie file size maximum 4kb. User can’t store more than that value.
4Session expire when browser is closedCookie default time also same, if expiry parameter is not set in cookie. Otherwise cookie is valid till the expiry time.
5Set and retrieve session always need session_start() at the beginning of the program. In cookie there have no need to cookie start.
6Destroy a session need to use session_destroy(). Destroy cookie there have no cookie destroy function. Cookie automatically delete after complete the expiry time.
Otherwise set cookie for past time.

PHP Cookies

The cookie is a mechanism to store data in the browser for a long time. It is a small data that server embeds on the browser. When a user opens any sites or does some activities in it and next time when he/she open the same website server can identify easily the user. Because browser stores the previous data in a small file which name is the cookie.
The first-time browser sends a request to the server
Second server returns response + cookies to the browser.
Third when the same request sent from browser to server then browser send cookie file also with the request.

Practical Application of Cookie in Real life activity:
Now, this day’s maximum user use online shopping through the e-commerce site. If any user opens any shopping site in his/her computer browser and adds some product in the cart list, then next time when he/she open those site again from his/her computer browser he/she can see the product in the cart list as it is. This is one of the applications of Cookies.

Using cookie any website can track the visitor. Like visitor IP, website open time, spending time on this website or the particular on by one page of this website, next return time of old visitors etc.

Remember user email id and password is another application of cookies.

Creating and accessing cookie in PHP

PHP allows creating the cookie and retrieving cookie value. setcookie() function is used to create the cookie in PHP.
Syntax of setcookie() function:

i. Name : Cookie file name where store information or data. It is any variable name or any string value. Which used later to retrieve cookie value. This is the required parameter.
ii. Value: The actual data which is stored in the cookie. Like username, password etc. Here can be set null value also.
iii. Expiry: Duration means how many minutes, hours, days or months cookie will exist. It is the future time in second value after that cookie will automatically delete. If this field is not set then the value will automatically delete when the browser is closed.
iv. Path: This field stands for the directories for which cookie is valid. A single forward slash (/) denote cookie to be valid for all directories.
v. Domain: This is the option for specifying the domain name, so all cookies are valid for this host and domain which created them.
vi. Security: Here set 1 value to specify that the cookie should be sent by secure site using HTTPS or set 0 to specify cookie can be sent from any regular HTTP.





After execute the set.php in the browser a small cookie file generate in the browser settings and Black value set in the favcolor variable for next 1 minute. User can check the cookie file in their browser, go to the browser settings -> Advanced-> Privacy and security-> Content settings-> Cookies-> See all cookie and site data->

There all browser cookie store with the cookie name.

In this example favcolor variable set ‘Black’ value for 1minute. So retrieve.php refresh in the browser after one minute it will return ‘Favorite Color is’. Value will be automatically deleted. Again run the set.php the cookie will be set again for next 1 minute.

Note: time() is a PHP function which returns the current time in second value. Time is calculated from 1970 1st January.

Retrieve cookies

$_COOKIE is a super global variable in PHP. This variable is used to retrieve cookie value. It is the global variable so always declear it in capital letter.

Its return all cookie value used in the current web application as an array. Using cookie a value can be access in multiple pages in a particular web application. In above example, $_COOKIE[‘favcolor’] return Black value any pages in this application. Like print, $_COOKIE[‘favcolor’] in another page along with retrieve.php then also return the same value for 1 minute.

Manually Delete or Destroy Cookie

The cookie is automatically deleted from the browser after complete the expiry time. If expiry time is not set then the cookie will automatically delete when the browser is closed. But need to delete cookie value without the closing browser or before expiry time then set this cookie value for past time.




Set again cookie and the run this file before 1 minute cookie will deleted from the browser automatically then call retrieve.php, its return blank value.

Application of Cookie
In user first login, user enter their username and password and checked remember me option that’s why next time username and password automatically show in the field and do not need to re type username and password again. This mechanism is possible in cookie.


PHP File Upload

File upload is a very important topic in PHP. Build any kind of websites or web application there has must be files. So want know that how can upload files or retrieve those files and download it (if required).



Always use form method post for uploading the file, and enctype is mandatory, otherwise, the file doesn’t upload.
Before uploading the file need to set some options. By default uploaded file size is set 2M. So sometimes file doesn’t upload if the file size is large than 2M.
That’s why set upload_max_filesize= 500M or any large value in the php.ini file (xampp/php/php.ini).
And also modify the post_max_size value in the php.ini file. By default post_max_size is 8M. Sometimes for this reason also file doesn’t upload if the size is large then 8M instead of upload_max_size is greater than 8M.
So also set post_max_size=500M or any large value in the php.ini file (xampp/php/php.ini).
Any changes in the php.ini file need to restart xampp control panel. Stop apache and start again. Then the updated value will be reflected.

PHP Working with Directories

Handle a directory there are several functions in PHP language.
i. mkdir()
ii. opendir()
iii. readdir()
iv. scandir()
v. rmdir()
vi. closedir()

i. Create Directory
Want to create a new folder using PHP then use mkdir(). Only one argument is required the folder name or folder location where want to create the folder.


ii. Open Directory
Using opendir() function can open a folder.Only one argument is required the folder path that is to be opened.


iii. Read Directory
readdir() is used to read any folder content. Return the all files and sub directory list of the specified folder (what want to read). One argument is required the folder path.


iv.Scan Directory
sacndir() is same as readdir() but there are small difference. sacndir() return the all files and sub folder name as an array. One argument is required the folder path. No need to open folder in this case.




v. Remove Directory
rmdir() function is used to remove or delete any empty folder from the specified location. If the folder is not empty then rmdir() is not work.


vi. Close Directory
closedir() function is used to closed any directory.


PHP File Handing

File handling is need for any web application so FILE HANDLING is an important part of PHP language. Some task to be done file needs to be processed. PHP have many function handling any kind of normal files. (Liketxt, docx, csv, jpg etc)
Handle a file need to know below topics

i. File create
ii. File open
iii. File read
iv. File write
v. File modify
vi. File close
vii. File remove

i. File Create
Create any kind file through PHP use fopen() function.fopen() function mainly use to open a file in different mode.
File modes are r for read, w for write and a for append. In this function require two arguments. First argument is file name and second argument is file mode value. It is always open a file with specific mode but if the file is not present then create a blank file with the file name.
touch() function also create new file. Only one argument is required the file name or file location where want to create.

File is creating in the same directory where the program is executed!

ii. File Open
fopen() function is used to open a file with a specific mode.


File open in read mode.
Note: if file is not present in this location then PHP return a warning. Like, in above example if sample.txt file doesn’t exist in this location then output is “Warning: fopen(sample.txt): failed to open stream: No such file or directory”

iii. File Read
Read any kind of file use fopen() function with r mode(Read mode). There is a another function fread() which is used to store or directly display the opened file contents as a string. fread() function have two argument first the file value (which is open in r mode using fopen()) and second is file size (how much bytes want to read from the file). Want to read all data of the file use filesize() function which return the original file size in bytes. filesize() have only one arguments, valid file name is required.



Another way to read a file:

In this example return the same output as the previous one.
feof() function is file end of the file. There is one argument, the file value. Means if the file content is not ended the while loop is continue. Here fread() is only read 1kb data at a time so whenever the file size is to over while loops continue, after complete the reading of the whole file content while loop automatically stop.

Only some data can be read in a large file using fread() function

How do I love thee? Let me count the ways. I love thee to the depth and breadth and height My

Note: Here only 1kb data read from the whole file.

Read online file directly
file_get_contents() is a PHP function which directly read a file from Internet. Only one argument is required in this function the URL value. But it’s also read local server file so without using fopen(), fread() can be read any file data using file_get_contents().

Its return the whole data of this URL file in local server.

iv. File Write:
Write any file using PHP the open the file in w mode in fopen(). Write mode. After that use fwrite() function to write content in this file. fwrite() function have two arguments filename where want to write and another the content value which want to write in this file.


After executing the program ‘This is a sample file…’ content is written in the ‘sample.txt’ file. But add another content with previous content is not possible in w mode. If execute the program with new content the only new content value is present in the text file previous data will be lost.

After execute this one ‘Add something with old content’ this value only store in the ‘sample.txt’ file.
‘This is a sample file…’ will be replaced by the current content.

v. File Modify
Update file content then open the file in a mode. Append mode.


After executing the program ‘This is another sample file…’ content is written in the ‘newsample.txt’ file. In append mode can be modified previous content of the file and add new content in this file.

After execute this one output is ‘This is an another sample file…Add something with old content!’

vi.File Close
After Open a file it must be need to close the file after operation. fclose() is used to close file. Only one argument is there the file value.

Output remain same.

vii File Remove
Remove or Delete any existing file use unlink() function. There have only one argument. The file name or file location/path is required which want to delete.

After execute the program ‘sample.txt’ will be permanently deleted from the folder.

viii Rename File
In PHP language rename any file name use rename(). There have two arguments. First one is the original file name or original file location and second one is new name value of this file with location.

After execute the program ‘sample.txt’ file name will be changed into ‘newfile.txt’.

ix Copying File
Copy one file content to another file use copy() in PHP. There have two arguments. First one is the copied file name or location and the second one is the file name or file location where to want to copy. Simply copy from source to destination first argument is source file name or location and second is destination file name or location.


Usage of file handling
In real life web application there are many usage of file handling. Here is a one example of usage of file handling is downloading. Download online file into PC using PHP.



Images Used from Pexels Free stock photos

Text file used from