Note: This try redis io is community version on web so few commands might not work. To use redis’s full power you can download and install on your local system
Redis is an in-memory data structure store, used as a distributed, in-memory key–value database, cache and message broker, with optional durability. Redis supports different kinds of abstract data structures, such as strings, lists, maps, sets, sorted sets, HyperLogLogs, bitmaps, streams, and spatial indices.
chattr: This is a command in Linux used to change file attributes on a file system.
+a: This option sets the “append-only” attribute on the specified file. When this attribute is set, the file can only be opened in append mode for writing. Existing data in the file cannot be modified or removed. This attribute is often used to prevent accidental deletion or modification of important files.
Namespaces are a way of encapsulating items. e.g. In any operating system directories serve to group related files, and act as a namespace for the files within them.
foo file in abc lmn pqr and xyz directory
As a concrete example, the file foo.txt can exist in all 4 directories with different contents in each.
Similarly to avoid ambiguity in PHP we have namespaces. Namespaces solve the confusion of having same class name in program.
Let’s understand this with example.
AdminAccount.php
<?php
class AdminAccount {
public function __construct() {
echo "Admin Account Controller..." . PHP_EOL;
}
}
UserAccount.php
<?php
class UserAccount {
public function __construct() {
echo "User Account Controller..." . PHP_EOL;
}
}
index.php
<?php
require("UserAccount.php");
require("AdminAccount.php");
new UserAccount();
new AdminAccount();
Now, what if we have the same class name in both files. This scenario always comes when you use third-party libraries or any framework.
AdminAccount.php
<?php
class Account {
public function __construct() {
echo "Admin Account Controller..." . PHP_EOL;
}
}
UserAccount.php
<?php
class Account {
public function __construct() {
echo "User Account Controller..." . PHP_EOL;
}
}
index.php
<?php
require("UserAccount.php");
require("AdminAccount.php");
new Account();
new Account();
This will create confusion about which instance to be created. Now let’s resolve this problem using namespaces.
AdminAccount.php
<?php
namespace Admin;
class Account {
public function __construct() {
echo "Admin Account Controller..." . PHP_EOL;
}
}
UserAccount.php
<?php
namespace User;
class Account {
public function __construct() {
echo "User Account Controller..." . PHP_EOL;
}
}
index.php
<?php
require("UserAccount.php");
require("AdminAccount.php");
new User\Account();
new Admin\Account();
An INI file is a configuration file for computer software that consists of a text-based content with a structure and syntax comprising key–value pairs for properties, and sections that organize the properties.
parse_ini_file function
Parse ini file is a function to parse any configuration file which has key-value pair This is required when you want to keep all application-level configuration parameters in one place Maintaining configuration level parameter/variables is easy when you use ini file You need to make changes in one file and it will reflect changes throughout the application
Composer is an application-level package manager for the PHP programming language that provides a standard format for managing dependencies of PHP software and required libraries. It was developed by Nils Adermann and Jordi Boggiano in 2012, who continue to manage the project.
cd ~
curl -sS https://getcomposer.org/installer -o composer-setup.php
sudo php composer-setup.php --install-dir=/usr/bin --filename=composer
OR
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
composer.json Keep this file in the project’s root directory NOTE: This file is mandatory to in root directory to use composer as composer look for this file when you run dump-autoload
<?php
namespace App\Admin;
class Account {
//this is magic method as it will invoked automatically
//when you create instance of account
public function __construct() {
echo "I am from Admin Account" . PHP_EOL;
}
}
app/user/Account.php
<?php
namespace App\User;
class Account {
//this is magic method as it will invoked automatically
//when you create instance of account
public function __construct() {
echo "I am from User Account" . PHP_EOL;
}
}
A user or admin facing problem managing data on excel sheet. He/She need a system to perform at least following operations
Add Record
Modify Record
Delete Record
Show Listing and Search Data to get specific Information.
User need a system which should be accessible from internet so that he can work from any machine (laptop/desktop/mobile).
You need to develop a web application with best of your knowledge
Roles: Admin
With correct credentials admin should be able to login and see the dashboard.
if credentials are wrong he will stay on login page and show a message – wrong credentials.
On successful login admin can see users list perform all CRUDL operations.
NOTE: you need to use vim editor to edit files
Following are the wireframes for reference.
login.php
dashboard.php
add_user.php
edit_user.php
delete confirm box
Technologies to be used
composer for package management and autoload
ini for configuration
git and gitlab for version control
HTML5 CSS3 Bootstrap 5 for UI/UX
jquery 3.6 or javascript for validation and AJAX
php 7.4 or 8 as backend programming language
mysql 8 database
PDO for database operations
PHPUnit for unit testing
python and php for automation script (Use cron jobs to automatically run script)
nginx web server
use infinityfree / webserver / cloudserver for website hosting
Jenkins and git-ftp for CI/CD
MVP / Deliverable
P0
Users Listing
Delete User Record
Add User Record with Profile Picture (User status should be enum in database table: enable, disable, blocked, active, inactive)
Update User Record
Session Management Login / Logout
P1
View User Details in Modal Window
Pagination
Sorting
Searching
Filtering
P2
Frontend – Backend Validation
Export CSV Users
Bulk Upload CSV
Activity Log
Export Activity Log
P3
Login with OTP i.e. 2FA (Use Redis to store OTP)
Login Logout for user account
Inactive User status if not logged in for 3 consecutive days
Change Admin and User Password from their respective account
Secret Questions and Forgot Password / Recover Password using secret questions or through the email link
P4
REST API (Web Services) for User CRUDL Operations
Protect REST API using Basic Authentication or JWT token
Login with google API or Facebook API
PHPUnit test for all functionalities
Licensing or limit user registration
P5
Dashboard showing following summary (Use highcharts)
Total User
Active Users
License Count / Usage Count
Online Users
Weekly Registration Stats
Schema
Ref SQL Queries
SELECT * FROM users ORDER BY id DESC;
SELECT * FROM users WHERE id = 3;
DELETE FROM users WHERE id = 3;
INSERT INTO users (id, name, age, city, added_at, updated_at) VALUES (NULL, 'sonam gupta', 18, 'gorakhpur', NOW(), NOW());
UPDATE users SET name = 'Sonam Gupta', age = 20, city = 'Gorakhpur', updated_at = NOW() WHERE id = 5;
Keyword arguments (or named arguments) are values that, when passed into a function, are identifiable by specific parameter names. A keyword argument is preceded by a parameter and the assignment operator, = . Keyword arguments can be likened to dictionaries in that they map a value to a keyword.
A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure. Decorators are usually called before the definition of a function you want to decorate.
Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.
install flask module
python -m pip install Flask
hello world
from flask import Flask, jsonify, request, render_template
app = Flask(__name__)
#YOUR FUNCTIONS HERE
if __name__ == "__main__":
app.run(debug=True);
#app.run(host="0.0.0.0", port=int("1234"), debug=True)
render html template [NOTE: MAKE SURE TO KEEP ALL TEMPLATE FILES IN templates DIRECTORY]
@app.route('/')
def index():
#return "Hello World";
data = {'company_name': "TCET"}
return render_template('hello_world.html', data = data)
templates/hello_world.html
<h1>Hello World</h1>
<h3>Welcome to {{data['company_name']}}</h3>
from flask import Flask, jsonify, request
from flask_cors import CORS
import pymysql
app = Flask(__name__)
cors = CORS(app)
@app.route('/users', methods=['GET'])
def get_users():
# To connect MySQL database
conn = pymysql.connect(host='localhost', user='root', password = "", db='databasename')
cur = conn.cursor()
cur.execute("select * from users LIMIT 10")
output = cur.fetchall()
print(type(output)); #this will print tuple
for rec in output:
print(rec);
# To close the connection
conn.close()
return jsonify(output);