PHP & Web Development Blogs

Search Results For: implement
Showing 1 to 5 of 11 blog articles.
8 views · 4 months ago



To bridge the gap between web-based and cloud-based applications, businesses often rely on skilled DevOps developers. These professionals play a crucial role in ensuring seamless integration, efficient customization, and robust back-end infrastructure for applications. The expertise of DevOps developers is indispensable for optimizing development workflows and enhancing collaboration between development and operations teams in the dynamic landscape of app development services.

In the realm of cloud computing, web based application in cloud computing play a pivotal role. Technically, web apps, as the name suggests, are applications hosted on remote servers & accessible through web browsers. On the other hand, cloud-based apps are web applications that come with advanced functionalities & elaborate compatibility.


In the realm of contemporary software development, the demand for innovative solutions is evident in the competition between web and cloud-based applications. These two platforms share similarities but diverge significantly in crucial aspects. This article will delve into the distinctions between web-based and cloud-based applications, exploring facets such as back-end infrastructure, scalability, and technical perspectives, shedding light on the nuances that developers navigate in this dynamic landscape, including the pivotal role of technologies like chatbot development.
What Is a Web Application?

A web-based app is an application designed and developed for the web browser. Unlike cloud based application development, the web app completely depends on the web server for functionality & processing. This application program is mainly stored on the remote server & delivered through a web browser interface over the internet. According to web application development company, web apps have client-server architecture & their codes are divided into 2 major components – server-side architecture & client-side architecture.

Server-side architecture: The server-side architecture or script usually deals with data processing. The web server can process a client request & send a response back. This web app architecture defines a simultaneous interaction between database instances, components, user interfaces, middleware systems, and servers.

Client-side architecture: The client-side architecture mainly deals with interface functionalities such as drop-down boxes and buttons. When a user clicks on the link, the browser will start loading the client-side script & rendering a text and graphic element for interaction.
Types of Web Apps
Nowadays, many businesses are already adopting various kinds of web-based applications because of their several advantages, features, and functionalities. 8 most popular types of web apps include:
    . Static Web Apps
Static web applications, constructed using HTML, CSS, and JavaScript, lack the flexibility of dynamic counterparts. These web based services provide content directly to users without requiring server-side modifications, resulting in simplicity and straightforward development. Key benefits of these apps include:
Very fast load time
Highly secure
Less complex to build
    . Dynamic Web Apps
This is a complex type that provides real-time data based on the server response and the user’s request. Dynamic web apps can be developed either as a conventional website with several pages and levels of navigation or as a single-page web application. They use several server-side and client-side languages to create web pages such as HTML, CSS, JavaScript, Python, PHP, Ruby, etc. Key benefits of dynamic web apps include:
Wider audience reach
Scalable in comparison to static web apps
Very flexible in terms of a new content update
    . Single Page Apps
A single page web app entirely runs on the browser & never requires browser reloading. This is actually a dynamic web app that manages all data on a single HTML page. This type of web app is faster than traditional websites as its logic is implemented in the browser directly than a server. Gmail, Netflix, Pinterest & Paypal are the best examples of single page applications. Key benefits include:
Enhanced user experience
Minimized server load
Improved app performance
    . Multiple-Page Apps
Multiple page apps are designed multiple pages separately and combined to form a website. They have different pages with static information like texts & images. Web based app development companies recommend using multiple-page apps as they offer excellent control over search engine optimization techniques. Major benefits of Multiple page apps include:
Ideal for SEO
Quick browser back or forward navigation
Simple to develop
    . Animated Web Apps
This is a type of web application that effectively supports synchronization & animation on the web platform. These applications are widely used by freelancers and creative companies to present their creativity better. Technically, JavaScript, HTML5, FLASH, and CSS are used to create animated web applications. Key benefits of AWAs include
Improved User Engagement
Enhanced Navigation
Excellent Branding
    . Web Apps with CMS
In this web application, content is updated constantly. It helps to manage, modify and create digital content with ease. WordPress is one of the best examples of CMS web applications. A variety of languages are used to create content management systems such as C#, PHP, Java, and Python. Key advantages of CMS web apps include:
Quick content creation & management
Efficient & quick updates
A vast range of features
    . E-commerce Web Apps
It’s a complicated and advanced dynamic web application that allows users to buy & sell goods electronically. These web based services encompass transaction and payment integration as key components, facilitating seamless order processing, payment acceptance, and logistical management for businesses involved in online commerce. Key benefits of these web apps include:
Scale business quickly
Offers customer insights through tracking & analytics
Sell goods across the world
    . Progressive Web Apps
Progressive web apps or PWAs are also called cross-platform web apps usually built with HTML, CSS, & JavaScript. PWAs use different features, APIs, and progressive methods to deliver a seamless experience. Progressive web apps boost the adaptability and speed of web applications. These apps are still easy to access if internet connectivity is poor. Key benefits of progressive web apps include:
Fast loading time
No installation required
Quickly respond to user interactions
Enhanced cross-platform conversion

Looking for App Development Solutions?
Take your brand up a notch with our custom mobile app development services.
Talk to an Expert

Type
Widely Used In
Advantages
Dynamic web apps
Social media
Healthcare
IT Industry
Logistics and transportations
Retail and ecommerce sectors
On-demand
Directly manage websites to update & change the information
Quick user management to protect servers & control all website users
Static web apps
Book publishing sectors
Works in offline mode
No 3rd party software installation required to access web apps
Single page apps
Email service
Communication sectors
Allows navigation & optimized routing experience
Keeps visual structure of web apps consistently through presentation logic
Multiple page apps
E-commerce sectors
Enterprise industries
Enables optimizing every page for the search engine
Allows users to access other pages
Animated web apps
Animation
Education
Gaming industries
Hold user attention for a very long time due to its attractive approach & unique design
Aspect ratios, landscape orientations, portrait, and viewing distances & different pixel densities are considered
Web apps with CMS
Blogging platforms
Sales & marketing platform
News portals
Easily organizes the web content Offers group & user functionality
Simple language support & integration
E-commerce web apps
E-commerce sectors
Allows sellers to sell products using a single platform
Helps you expand business globally & reach maximum audience
Progressive Web Apps
On-demand
Healthcare
Retail and e-commerce
Logistics and transportations
Social media
IT sectors
Responsive & Browser Compatibility Works in online & offline mode
Updates with no user interaction

Key Benefits of Web Apps
Web apps enable businesses to interact with their customers more efficiently. These applications can make it easy to track & measure data that are essential to keep business operations streamlined. Key advantages of web apps include:
Easily accessible through any kind of web browser
Runs on multiple platforms that make it cross-platform compatible
Minimizes the risk of compatibility issues
Requires less maintenance & support from the developer’s end
Helps to ease usability for the customers
Effectively eliminates hard drive space limitations
Apps can be maintained & updated without software reinstallation on several devices
Offers high scalability and flexibility
Simple to deploy, maintain, and update
The cost of routine maintenance is minimized as the data is stored on remote servers
What is a Cloud Based App?
These apps are online software programs with elements accessible via a local server and executed on the cloud environment. As internet-based software, cloud applications are stored in the remote data center & handled by cloud-service providers. These apps are used for file sharing & storage, order entry, word processing, inventory management, financial accounting, customer relationship management, data collection, etc.

According to the report, the global market size of cloud apps is projected to reach approx 168.6 billion USD by 2025. Cloud apps usually support several user requirements through customization and provide several services to meet storage, backup & security needs. Some major characteristics of cloud apps include:
Agile application
Microservices-oriented
API-backed
Continuously integrated & delivered
DevOps-enabled
Analytics-infused
User experience-centric
Types of Cloud-based Applications
Cloud apps are divided into three major cloud computing models – SaaS, PaaS, and IaaS. Each model also shows several parts of cloud computing stacks. Take a closer look at these types:
    . SaaS or Software as a Service
SaaS is one of the best cloud apps that enable users to easily access full-functioning software applications over the internet. These cloud applications are primarily designed for freelance services, large enterprises & SMBs. Some of the best examples of SaaS applications are HubSpot CRM, Wrike, MS Office 365, Sisense, Wix, etc.
    . PaaS or Platform as a Service
PaaS provides users with the infrastructure, computing platforms, and solutions to build their own applications. Platform as a Service is ideal for businesses that mainly engage in collaboration, testing, and development of cloud solutions. PaaS applications have a deployment environment including run-time system libraries, operating systems, and graphic UI. Some of the best examples of PaaS apps are Google App Engine, Microsoft Azure, Rackspace Cloud Sites, etc.
    . IaaS or Infrastructure as a Service
IaaS consists of basic building blocks that offer access to networking functionalities, features & data storage space. It enables users to outsource IT infrastructures like servers, processing, virtual machines, storage, networking & other resources. IaaS applications also offer a good level of management control and flexibility over IT resources. Some of the best examples of IaaS apps are Amazon WorkSpaces, IBM Cloud, Google Cloud, etc.
Benefits of Cloud Apps
Web based application in cloud computing boost productivity, accessibility, security, and data safety. They help businesses make the process of collaboration more effective and easier. Key benefits of cloud applications include:
Minimal service provider interaction & management effort
Provides large computing capabilities, online & offline
Provides access to information from any device or place
Offers fast access to important applications through cloud servers
The performance of the availability of cloud apps enhances profitability & streamlines workflows
Serves multiple consumers with virtual and physical needs
Provides high transparency to resource providers & consumers
Offers improved collaboration options
Web Apps Vs Cloud Apps – Key Differences
Web apps and cloud apps both come with a wide range of functionalities & have noticeable distinctions. Web-based applications usually are accessible via web browsers, whereas cloud app’s infrastructure and data aren’t only accessible through the web browser but also downloadable. So, all cloud apps are web apps with additional features. Other differences between web and cloud apps are listed below.

Parameters
Cloud apps
Web apps
Internet
Work partially or entirely without the internet connectivity
Work with the internet only
Security
Ensures high security measures for sensitive & confidential information
It can verify client info on authentic servers
Technology
It needs a back–end framework & a JavaScript-based structure like React Js, Angular, etc
It has inbuilt languages such as PHP, Python & Ruby, and databases like MySQL.
Access
It’s not dependent on the web browser
Accessed via the web browser only
Customization
Customization features improve functionalities.
Never provides customization and similar functionalities
Costs
Expensive as compared to web apps
Development cost is less than cloud apps
Types
SaaS, PaaS, IaaS, RaaS
Static web apps, dynamic web apps portal web apps, etc
Scalability
Inherently scalable
Limited scalability
Availability
High uptime
Limited uptime
Storage
Multiple replicated center
Single data center

Are You In Search of The Best App Development Company?
With top-notch development services, we develop the best software applications that meet your needs.
Book an Appointment
Final Words
Web apps and cloud apps both are continuing to deliver users as the most crucial touch point. Since they are packed with similarities and dissimilarities in terms of software architecture, storage, and other aspects, selecting the right application always depends on customer preferences, business needs, and operations. Are you planning to build a custom web application or looking for web app development services? Get in touch with our experts for complete assistance.

4283 views · 3 years ago
Using AI for Weather Forecasting

Technology is constantly changing the way we interact, research, and react. One such way artificial intelligence is impacting our daily lives, and we may not even realize it is in weather forecasting.


The forecast we usually have been receiving in our phones and in older times primarily in newspapers, was based on data collected via satellites, radar system and weather balloons. In recent times there has been the addition of IoT based sensors as well. However, with the advent of Artificial Intelligence (AI) finding its way in numerous areas, AI has taken a role in improving the accuracy of weather as well.

The Dataset expansion

A significantly enormous set of data is available - from the weather satellites in space, to the private and government owned weather stations which are gaining real-time data. IBM for instance has more the 0.25 million weather stations that help IBM collect real-time data. Additionally, as we are in the age of Internet of Things (IOT), each small device to big device- cellphones, solar panels and vehicles everything has become or is yet to become yet another data source. Companies like GE have installed IOT street lights, which help in monitoring air quality and humidity. These are some of the few sources which help us in collecting the vast amount of data necessary for building on the AI technology, in future these sources and the amount of available data would grow exponentially.

Google and Weather forecast

Using the AI technology Google is able to develop a weather forecast tool, it has been trained to predict rainfalls accurately as much as six hours before. The underlying technology on which this prediction is build upon is U-Net convolutional neural network which is originally used in biomedical research. It works by taking satellite images as input and uses AI technology to transform these images into high resolution images. The only off-set is this is not real-time prediction and the delay due to complex calculations results in using six-hour old data and hence can only predict six-hours before.

IBM and its efforts in weather prediction

The quest for IBM to venture into weather forecasting began with IBM acquiring The Weather Company. IBM plans on using the large amount of weather data available coupled with IBM Watson and the cloud platform to enhance weather forecasting. In 2019 IBM developed Global High-Resolution Atmospheric Forecasting System (GRAF) in order to forecast weather conditions 12 hours prior to a greater degree of accuracy. The radius encompassed by the GRAF is also more narrowed down up to 3 kilometers as opposed to generally being 10-15 kilometers. Another of its marvel is that it gives accurate predictions down to each hour and not just daily.

Artificial Intelligence and Panasonic

Panasonic is the company behind TAMDAR, the weather sensor installed on commercial airplanes. With this advantage of extensive amount of data from in-flight sensors as well as publicly available data Panasonic developed Global 4D Weather. Proving to their claim of being the most advanced global forecasting platform globally they were able to timely predict Hurricane Irma in its early days.

Uses of Weather Forecasting


Sales

Everyday life decisions are affected by weather, it makes us choose in the way we travel, things we eat and things we buy to wear. The rise in temperature may increase sales of chilled drinks, if the company is fully aware of the forecast it would be able to manage productions as per demand. AI can help brands in maximizing sales based on weather forecasts and in minimizing waste.

Natural Disasters

The Panasonic Global 4D weather predicting Hurricane Irma is just another example where timely prediction can save millions of lives in face of situations like floods and Hurricanes. Companies like IBM combine weather forecasting data with utilities distribution network, which enables them to narrow down areas with likely outages. This enables utilities to place their workforce timely so the repair process catering to damage repairs post disasters is shortened. This in turn brings huge benefits to the overall economy.

Agriculture

The weather and agriculture have the most obvious correlation, each process in farming from sowing to reaping all depends on the weather. As farmers cultivate on huge farming lands, accurate information about each part of the land can help farmers in improving their crops and yield by manifolds. Weather conditions can lead to almost 90 percent of crop losses, 25 percent of these losses can be avoided using accurate AI prediction models to forecast weather and in turn improve the yield.

Transportation

Sea travel has always been eventful, timely prediction of storms by using machine learning techniques and hyper-local data allows companies to plan shipments accordingly and avoid severe weather conditions that usually result in delays. Tools like IBM’s Operations Dashboard for Ground Transportation equips in enhancing productivity based on weather predictions.

Another of the implementation of AI in transportation industry corelating to weather is fuel consumption. For instance, using weather prediction models to reduce airplane fuel consumption during its ascent.

To conclude Artificial Intelligence has a key role to play in weather forecasting, weather direct or indirectly impacts each sector in the economy. As the amount of information available to improve predictions increases exponentially it gives a chance to AI to improve accuracy even further. As we continue narrowing down weather conditions precise to time and location the benefits of such advancements across all industries are innumerable.

9690 views · 3 years ago


Welcome back!, if you’re new please be sure to read Part 1 here.


This tutorial will focus primarily on Security and will touch on how to plan functionality.

Planning out an application and seeing progress regularly is a good strategy as you are most likely to complete your tasks in a timely fashion with this approach.

Ready?, ok let’s jump into it!

DISCLAIMER


We highly recommend that you follow these tutorials on a localhost testing server like Uniserver. Read through Part 1 here to look at our recommendations. These tutorials follow a phased approach and it is highly recommended that you do not make snippets of code live prior to completing this tutorial series.


Where we left off – the serious stuff.


In the previous tutorial we saved variables to the database.

It’s important to note that further steps are needed to ensure that data transactions to / from the database are secure.

A great first step is to ensure that all POST data (data transmitted after a user clicks a form’s submit button) is sanitized.

What we’re trying to prevent


One of the most common exploits is SQL Injection, an attack most commonly used to insert SQL into db queries. POST data that’s not sanitized leaves a huge security hole for malicious exploits. In some cases SQL injection can be leveraged to rage an all out assault on a server’s operating system.

A few examples of a basic version of what this might look like can be seen below.



OUTCOME


This might delete your database table



OUTCOME


This might provide access to the entire user table and the password protected area/dashboard.


***Please note that there are various types of SQL injection techniques and I will delve into this during the course of this series.***


So what exactly is sanitization and what does it do?


When sanitizing POST data, we are essentially looking for any special characters that are often used in SQL injection attacks.

In many ways, this tiny piece of code is the unsung superhero of many database driven applications.

Let’s secure that POST data!


Navigate to your backend folder and open index.php

Locate the following line of code:

$sql = "INSERT INTO content(title,content,author)VALUES ('".$_POST["title"]."', '".$_POST["content"]."', '".$_POST["author"]."')";


Ok, let’s get to work.

Based on what I mentioned a few moments ago, it’s clear that our SQL statement is vulnerable so we need to sanitize the POST data pronto!

The method I will focus on first is $mysqli->real_escape_string. This will escape any special characters found in the POST data.

Add the following just above your $sql.

$title = $letsconnect -> real_escape_string($_POST['title']);

$content = $letsconnect -> real_escape_string($_POST['content']);

$author = $letsconnect -> real_escape_string($_POST['author']);


Did you notice the use of $letsconnect? This was used because of our db connection defined in conn.php.

Our new query will look like this:

$sql = "INSERT INTO content (title,content,author) VALUES ('".$title."', '".$content."', '".$author."')";


Go ahead and replace the old $sql.

Phew!, we can breathe easy now.

Next, let’s lighten things up a bit by focusing on functionality and aesthetics.


A phased approach is the best way to tackle projects of any size.

I tend to jot this down on paper before creating a more legible professional spec!.

Typically the phased approach lends itself to logical progression.

For example, over the next several days I will go over the following:

* Account Access
* The login process
* The registration process
* The password recovery process
* Frontend
* The look and feel
* Menus
* Sidebars
*Main Content
*Footer
* Backend
* Content Management
* Add/Edit/Delete
* Security

This will give us a good springboard to delve into more complex functionality.

The aesthetic I have in mind will be barebones at first with clean CSS practices (this will make life a whole lot easier when we have to make changes down the line!).

Challenge :


Plan out your own CMS, think about the user interface and design choices you’d like to implement, and create a phased approach.

Conclusion


I hope this tutorial encouraged you to think about security and understand one of the most common exploits. During the course of this series, you will receive the tools necessary to beef up security while maintaining your sanity!

Next up


CodeWithMe – Let’s go templating.
24598 views · 4 years ago
PHP CHAT WITH SOCKETS

Hey Friends,

I am sharing a very interesting blog on how to create a chat system in php without using ajax. As we all know ajax based chat system in php is not a good solution
because itincreases the server load and redundant xhr calls on our server.

Instead, I am going to use sockets for incoming messages from and send messages to another user. So lets try them out using the following steps:


Step 1: Cross check in php.ini that sockets extension is enabled


;extension=sockets
extension=sockets


Step 2: Create server.php file


This file will handle the incoming and outgoing messages on sockets, Add following variables in top of the file:

$host = 'localhost';
$port = '9000';
$null = NULL; 


Step 3: After it add helper methods


The following code for handshake with new incoming connections and encrypt and decrypt messages incoming and outgoing over sockets:

function send_message($msg)
{
global $clients;
foreach($clients as $changed_socket)
{
@socket_write($changed_socket,$msg,strlen($msg));
}
return true;
}
function unmask($text) {
$length = ord($text[1]) & 127;
if($length == 126) {
$masks = substr($text, 4, 4);
$data = substr($text, 8);
}
elseif($length == 127) {
$masks = substr($text, 10, 4);
$data = substr($text, 14);
}
else {
$masks = substr($text, 2, 4);
$data = substr($text, 6);
}
$text = "";
for ($i = 0; $i < strlen($data); ++$i) {
$text .= $data[$i] ^ $masks[$i%4];
}
return $text;
}
function mask($text)
{
$b1 = 0x80 | (0x1 & 0x0f);
$length = strlen($text);

if($length <= 125)
$header = pack('CC', $b1, $length);
elseif($length > 125 && $length < 65536)
$header = pack('CCn', $b1, 126, $length);
elseif($length >= 65536)
$header = pack('CCNN', $b1, 127, $length);
return $header.$text;
}
function perform_handshaking($receved_header,$client_conn, $host, $port)
{
$headers = array();
$lines = preg_split("/

/", $receved_header);
foreach($lines as $line)
{
$line = chop($line);
if(preg_match('/\A(\S+): (.*)\z/', $line, $matches))
{
$headers[$matches[1]] = $matches[2];
}
}
$secKey = $headers['Sec-WebSocket-Key'];
$secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
$upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake

" .
"Upgrade: websocket

" .
"Connection: Upgrade

" .
"WebSocket-Origin: $host

" .
"WebSocket-Location: ws://$host:$port/php-ws/chat-daemon.php

".
"Sec-WebSocket-Accept:$secAccept



";
socket_write($client_conn,$upgrade,strlen($upgrade));
}


Step 4: Now add following code to create bind and listen tcp/ip sockets:


$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, 0, $port);
socket_listen($socket);
$clients = array($socket);


Ok now a endless loop that will use for handeling incominga nd send messages:

while (true) {
$changed = $clients;
socket_select($changed, $null, $null, 0, 10);

if (in_array($socket, $changed)) {
$socket_new = socket_accept($socket); $clients[] = $socket_new;
$header = socket_read($socket_new, 1024); perform_handshaking($header, $socket_new, $host, $port);
socket_getpeername($socket_new, $ip); $response = mask(json_encode(array('type'=>'system', 'message'=>$ip.' connected'))); send_message($response);
$found_socket = array_search($socket, $changed);
unset($changed[$found_socket]);
}

foreach ($changed as $changed_socket) {

while(socket_recv($changed_socket, $buf, 1024, 0) >= 1)
{
$received_text = unmask($buf); $tst_msg = json_decode($received_text, true); $user_name = $tst_msg['name']; $user_message = $tst_msg['message']; $user_color = $tst_msg['color'];
$response_text = mask(json_encode(array('type'=>'usermsg', 'name'=>$user_name, 'message'=>$user_message, 'color'=>$user_color)));
send_message($response_text); break 2; }

$buf = @socket_read($changed_socket, 1024, PHP_NORMAL_READ);
if ($buf === false) { $found_socket = array_search($changed_socket, $clients);
socket_getpeername($changed_socket, $ip);
unset($clients[$found_socket]);

$response = mask(json_encode(array('type'=>'system', 'message'=>$ip.' disconnected')));
send_message($response);
}
}
}
socket_close($socket);


So you are ready with server side socket program, Now its time to move on front side where we will implement w3c provided client side Web Socket Apis,

Step 5: create a file named index.php for frontend usage with following initial code


$host = 'localhost';
$port = '9000';
$subfolder = "php_ws/";
$colors = array('#007AFF','#FF7000','#FF7000','#15E25F','#CFC700','#CFC700','#CF1100','#CF00BE','#F00');
$color_pick = array_rand($colors);
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="chat-wrapper">
<div id="message-box"></div>
<div class="user-panel">
<input type="text" name="name" id="name" placeholder="Your Name" maxlength="15" />
<input type="text" name="message" id="message" placeholder="Type your message here..." maxlength="100" />
<button id="send-message">Send</button>
</div>
</div>
</body>
</html>


Now add some basic styling in the head section using following code:

<style type="text/css">
.chat-wrapper {
font: bold 11px/normal 'lucida grande', tahoma, verdana, arial, sans-serif;
background: #00a6bb;
padding: 20px;
margin: 20px auto;
box-shadow: 2px 2px 2px 0px #00000017;
max-width:700px;
min-width:500px;
}
#message-box {
width: 97%;
display: inline-block;
height: 300px;
background: #fff;
box-shadow: inset 0px 0px 2px #00000017;
overflow: auto;
padding: 10px;
}
.user-panel{
margin-top: 10px;
}
input[type=text]{
border: none;
padding: 5px 5px;
box-shadow: 2px 2px 2px #0000001c;
}
input[type=text]#name{
width:20%;
}
input[type=text]#message{
width:60%;
}
button#send-message {
border: none;
padding: 5px 15px;
background: #11e0fb;
box-shadow: 2px 2px 2px #0000001c;
}
</style>


Ok Style is all set now need to add a jquery script and create web socket object and handle all events on it as following code need to add before closing of bosy tag:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
var msgBox = $('#message-box');
var wsUri = "ws://".$host.":".$port."/php-ws/server.php";
websocket = new WebSocket(wsUri);

websocket.onopen = function(ev) { msgBox.append('<div class="system_msg" style="color:#bbbbbb">Welcome to my "Chat box"!</div>'); }
websocket.onmessage = function(ev) {
var response = JSON.parse(ev.data);
var res_type = response.type; var user_message = response.message; var user_name = response.name; var user_color = response.color; switch(res_type){
case 'usermsg':
msgBox.append('<div><span class="user_name" style="color:' + user_color + '">' + user_name + '</span> : <span class="user_message">' + user_message + '</span></div>');
break;
case 'system':
msgBox.append('<div style="color:#bbbbbb">' + user_message + '</div>');
break;
}
msgBox[0].scrollTop = msgBox[0].scrollHeight; };

websocket.onerror = function(ev){ msgBox.append('<div class="system_error">Error Occurred - ' + ev.data + '</div>'); };
websocket.onclose = function(ev){ msgBox.append('<div class="system_msg">Connection Closed</div>'); };
$('#send-message').click(function(){
send_message();
});

$( "#message" ).on( "keydown", function( event ) {
if(event.which==13){
send_message();
}
});

function send_message(){
var message_input = $('#message'); var name_input = $('#name');
if(message_input.val() == ""){ alert("Enter your Name please!");
return;
}
if(message_input.val() == ""){ alert("Enter Some message Please!");
return;
}
var msg = {
message: message_input.val(),
name: name_input.val(),
color : '<?php echo $colors[$color_pick]; ?>'
};
websocket.send(JSON.stringify(msg));
message_input.val(''); }
</script>


Ok All set, Now need to run the server.php file using following php-cli utility,make sure you have php cli utility installed in your system:

php -q c:\xampp\htdocs\php-ws\server.php


Now you may access the front index.php file via the browser url like following and see a chatbox and connection status, you may use the same url or different browser to check the chat system is working or not.
26458 views · 4 years ago
Introduction to Gitlab CI for PHP developers
As a developer, you've probably at least heard something about CI - Continuous integration. And if you haven't - you better fix it ASAP, because that's something awesome to have on your skill list and can get extremely helpful in your everyday work. This post will focus on CI for PHP devs, and specifically, on CI implementation from Gitlab. I will suppose you know the basics of Git, PHP, PHPUnit, Docker and unix shell. Intended audience - intermediate PHP devs.
Adding something to your workflow must serve a purpose. In this case the goal is to automate routine tasks and achieve better quality control. Even a basic PHP project IMO needs the following:
* linter) checks (cannot merge changes that are invalid on the syntax level)
* Code style checks
* Unit and integration tests
All of those can be just run eventually, of course. But I prefer an automated CI approach even in my personal projects because it leads to a higher level of discipline, you simply can't avoid following a set of rules that you've developed. Also, it reduces a risk of releasing a bug or regression, thus improving quality.
Gitlab is as generous as giving you their CI for free, even for your private repos. At this point it is starting to look as advertising, therefore a quick comparison table for Gitlab, Github, Bitbucket. AFAIK, Github does not have a built-in solution, instead it is easily integrated with third parties, of which Travis CI seems to be the most popular - I will therefore mention Travis here.

Public repositories (OSS projects). All 3 providers have a free offer for the open-source community!


| Provider | Limits |
|---|---|
| Gitlab | 2,000 CI pipeline minutes per group per month, shared runners |
| Travis | Apparently unlimited |
| Bitbucket| 50 min/month, max 5 users, File storage <= 1Gb/month |

Private repositories


| Provider | Price | Limits |
|---|---|---|
| Gitlab | Free | 2,000 CI pipeline minutes per group per month, shared runners |
| Travis | $69/month | Unlimited builds, 1 job at a time |
| Bitbucket| Free | 50 min/month, max 5 users, File storage <= 1Gb/month |

Getting started

I made a small project based on Laravel framework and called it "ci-showcase". I work in Linux environment, and the commands I use in the examples, are for linux shell. They should be pretty much the same on Mac and nearly the same on Windows though.
composer create-project laravel/laravel ci-showcase

Next, I went to gitlab website and created a new public project: https://gitlab.com/crocodile2u/ci-showcase. Cloned the repo and copied all files and folders from the newly created project - the the new git repo. In the root folder, I placed a .gitignore file:
.idea
vendor
.env

Then the .env file:
APP_ENV=development

Then I generated the application encryption key: php artisan key:generate, and then I wanted to verify that the primary setup works as expected: ./vendor/bin/phpunit, which produced the output OK (2 tests, 2 assertions). Nice, time to commit this: git commit &amp;&amp; git push

At this point, we don't yet have any CI, let's do something about it!

Adding .gitlab-ci.yml

Everyone going to implement CI with Gitlab, is strongly encouraged to bookmark this page: https://docs.gitlab.com/ee/ci/README.html. I will simply provide a short introduction course here plus a bit of boilerplate code to get you started easier.
First QA check that we're going to add is PHP syntax check. PHP has a built-in linter, which you can invoke like this: php -l my-file.php. This is what we're going to use. Because the php -l command doesn't support multiple files as arguments, I've written a small wrapper shell script and saved it to ci/linter.sh:
#!/bin/sh
files=<code>sh ci/get-changed-php-files.sh | xargs</code>last_status=0
status=0
# Loop through changed PHP files and run php -l on each
for f in "$files" ; do message=<code>php -l $f</code> last_status="$?" if [ "$last_status" -ne "0" ]; then # Anything fails -> the whole thing fails echo "PHP Linter is not happy about $f: $message" status="$last_status" fi
done
if [ "$status" -ne "0" ]; then echo "PHP syntax validation failed!"
fi
exit $status

Most of the time, you don't actually want to check each and every PHP file that you have. Instead, it's better to check only those files that have been changed. The Gitlab pipeline runs on every push to the repository, and there is a way to know which PHP files have been changed. Here's a simple script, meet ci/get-changed-php-files.sh:
#!/bin/sh
# What's happening here?
#
# 1. We get names and statuses of files that differ in current branch from their state in origin/master.
# These come in form (multiline)
# 2. The output from git diff is filtered by unix grep utility, we only need files with names ending in .php
# 3. One more filter: filter *out* (grep -v) all lines starting with R or D.
# D means "deleted", R means "renamed"
# 4. The filtered status-name list is passed on to awk command, which is instructed to take only the 2nd part
# of every line, thus just the filename
git diff --name-status origin/master | grep '\.php$' | grep -v "^[RD]" | awk '{ print }'

These scripts can easily be tested in your local environment ( at least if you have a Linux machine, that is ;-) ).
Now, as we have our first check, we'll finally create our .gitlab-ci.yml. This is where your pipeline is declared using YAML notation:
# we're using this beautiful tool for our pipeline: https://github.com/jakzal/phpqa
image: jakzal/phpqa:alpine
# For this sample pipeline, we'll only have 1 stage, in real-world you would like to also add at least "deploy"
stages: - QA
linter:
stage: QA
# this is the main part: what is actually executed
script: - sh ci/get-changed-php-files.sh | xargs sh ci/linter.sh

The first line is image: jakzal/phpqa:alpine and it's telling Gitlab that we want to run our pipeline using a PHP-QA utility by jakzal. It is a docker image containing PHP and a huge variety of QA-tools. We declare one stage - QA, and this stage by now has just a single job named linter. Every job can have it's own docker image, but we don't need that for the purpose of this tutorial. Our project reaches Step 2. Once I had pushed these changes, I immediately went to the project's CI/CD page. Aaaand.... the pipeline was already running! I clicked on the linter job and saw the following happy green output:
Running with gitlab-runner 11.9.0-rc2 (227934c0) on docker-auto-scale ed2dce3a
Using Docker executor with image jakzal/phpqa:alpine ...
Pulling docker image jakzal/phpqa:alpine ...
Using docker image sha256:12bab06185e59387a4bf9f6054e0de9e0d5394ef6400718332c272be8956218f for jakzal/phpqa:alpine ...
Running on runner-ed2dce3a-project-11318734-concurrent-0 via runner-ed2dce3a-srm-1552606379-07370f92...
Initialized empty Git repository in /builds/crocodile2u/ci-showcase/.git/
Fetching changes...
Created fresh repository.
From https://gitlab.com/crocodile2u/ci-showcase * [new branch] master -> origin/master * [new branch] step-1 -> origin/step-1 * [new branch] step-2 -> origin/step-2
Checking out 1651a4e3 as step-2...
Skipping Git submodules setup
$ sh ci/get-changed-php-files.sh | xargs sh ci/linter.sh
Job succeeded

It means that our pipeline was successfully created and run!

PHP Code Sniffer.

PHP Code Sniffer is a tool for keeping app of your PHP files in one uniform code style. It has a hell of customizations and settings, but here we will only perform simple check for compatibilty with PSR-2 standard. A good practice is to create a configuration XML file in your project. I will put it in the root folder. Code sniffer can use a few file names, of which I prefer phpcs.xml:
<?xml version="1.0"?>
/resources

I also will append another section to .gitlab-ci.yml:
code-style:	stage: QA	script:	# Variable $files will contain the list of PHP files that have changes	- files=<code>sh ci/get-changed-php-files.sh</code> # If this list is not empty, we execute the phpcs command on all of them - if [ ! -z "$files" ]; then echo $files | xargs phpcs; fi

Again, we check only those PHP files that differ from master branch, and pass their names to phpcs utility. That's it, Step 3 is finished! If you go to see the pipeline now, you will notice that linter and code-style jobs run in parallel.

Adding PHPUnit

Unit and integration tests are essential for a successful and maintaiable modern software project. In PHP world, PHPUnit is de facto standard for these purposes. The PHPQA docker image already has PHPUnit, but that's not enough. Our project is based on Laravel, which means it depends on a bunch of third-party libraries, Laravel itself being one of them. Those are installed into vendor folder with composer. You might have noticed that our .gitignore file has vendor folder as one of it entries, which means that it is not managed by the Version Control System. Some prefer their dependencies to be part of their Git repository, I prefer to have only the composer.json declarations in Git. Makes the repo much much smaller than the other way round, also makes it easy to avoid bloating your production builds with libraries only needed for development.
Composer is also included into PHPQA docker image, and we can enrich our .gitlab-ci.yml:
test:	stage: QA	cache:	key: dependencies-including-dev	paths: - vendor/	script:	- composer install	- ./vendor/bin/phpunit

PHPUnit requires some configuration, but in the very beginning we used composer create-project to create our project boilerplate.laravel/laravel package has a lot of things included in it, and phpunit.xml is also one of them. All I had to do was to add another line to it:
xml

APP_KEY enironment variable is essential for Laravel to run, so I generated a key with php artisan key:generate.
git commit & git push, and we have all three jobs on theQA stage!

Checking that our checks work

In this branch I intentionally added changes that should fail all three job in our pipeline, take a look at git diff. And we have this out from the pipeline stages:Linter:
$ ci/linter.sh
PHP Linter is not happy about app/User.php:
Parse error: syntax error, unexpected 'syntax' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in app/User.php on line 11
Errors parsing app/User.php
PHP syntax validation failed!
ERROR: Job failed: exit code 255

**Code-style**:
$ if [ ! -z "$files" ]; then echo $files | xargs phpcs; fi
FILE: ...ilds/crocodile2u/ci-showcase/app/Http/Controllers/Controller.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
---------------------------------------------------------------------- 13 | WARNING | Line exceeds 120 characters; contains 129 characters
----------------------------------------------------------------------
Time: 39ms; Memory: 6MB
ERROR: Job failed: exit code 123

**test**:
$ ./vendor/bin/phpunit
PHPUnit 7.5.6 by Sebastian Bergmann and contributors.
F. 2 / 2 (100%)
Time: 102 ms, Memory: 14.00 MB
There was 1 failure:
1) Tests\Unit\ExampleTest::testBasicTest
This test is now failing
Failed asserting that false is true.
/builds/crocodile2u/ci-showcase/tests/Unit/ExampleTest.php:17
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
ERROR: Job failed: exit code 1

Congratulations, our pipeline is running, and we now have much less chance of messing up the result of our work.

Conclusion

Now you know how to set up a basic QA pipeline for your PHP project. There's still a lot to learn. Pipeline is a powerful tool. For instance, it can make deployments to different environments for you. Or it can build docker images, store artifacts and more! Sounds cool? Then spend 5 minutes of your time and leave a comment, you can also tell me if there is a pipeline topic you would like to be covered in next posts.

SPONSORS