M/s VIKASH TECH - We Write Imaginations to Codes..!!

Website Security – PHP: Implementing Security To Your Website

PHP Security

Security is one of the major concerns today and when it comes to coding, It becomes a point to re-think what are the best possible ways to implement security to the website.

We have jotted down the basic concepts of web security (in php) which can be used to secure your code from being misused and to which can protect you from some basic attacks.

Use of Nonce

Nonce is basically used to identify if the user is sending request from a valid location. Location here means from a webpage that has been served by the genuine server.

How it works

As the name suggests, it is a combination of occasions.

When a user sends the initial request to a website, the server generates an unique session for the user, which is used to identify the user every-time.

But it is hard to justify if the webpage which is submitting the request is on the same website. Eg. If I have a form on my website with the following code

<form method="post" action="login.php">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" name="submit"/>
</form>

It simply suggests that the data with the key “username” and “password” will be sent to the page named “login.php”.

The same form can be developed on an automation tool in some X machine and can be used to send request to the website, which can lead to security breach

To justify, if the form is submitted from a location from the website itself. A nonce is used.

<?php
session_start();
$_SESSION['nonce'] = md5(rand(1111,99999));
$nonce = $_SESSION['nonce'];
?>
<form method="post" action="login.php">
<input type="text" name="nonce" value="<?php echo $nonce; ?>" readonly/>
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" name="submit"/>
</form>

In the above set of code, we have generated a random number which is then hashed using the md5() hashing technique, and stored in the nonce variable.

The same value is stored in the session too. Once someone opens up the website, the nonce will be generated and will be stored in the unique session of the user, when he submits the form, the nonce will be sent back to the server, which will be validated to see if it matches the original nonce. If it matches, then it is coming from a valid source and if not, it is not from a valid location.

<?php
session_start();
if(isset($_POST['username']) && isset($_POST['password']) && isset($_POST['nonce'])){
if($_SESSION['nonce']==$_POST['nonce']){
 echo "Submited from a valid source";
}
else{
 echo "Submitted from an invalid source";
}
}
$_SESSION['nonce'] = md5(rand(1111,99999));
$nonce = $_SESSION['nonce'];
?>
<form method="post" action="login.php">
<input type="text" name="nonce" value="<?php echo $nonce; ?>" readonly/>
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" name="submit"/>
</form>

Securing files from being required/included in a file outside of website.

It is a general practice to create generic code and requiring or including it in the files where needed.

This is a great way to implement the concept of Don’t Repeat Yourself (DRY), but there is a security breach that can come into picture here too.

Look at the following codes:

connection.php

<?php
 $connection = mysqli_connect('hostname', 'user','password','database');
?>

save.php

<?php
 require('connection.php');
 //some mysql transaction code goes here
?>

In the above two files, connection.php and save.php, you can see that the file just needs to be written in require() function and it will get required.

The file connection.php can be required from any other source and can be used to showcase all the connection information from any other machine.
For example, anyone can use the global path for the same file to require it in his/her code as below

hackerFile.php

<?php
 require('https://abc.com/connection.php');
 print_r($connection);
?>

The above code will show all the connection information.

To secure it, we can define a variable which can be used as a token to check if it is being requested from a valid location. For example

connection.php

<?php
if(!defined('uniquenamevariable')){
 die('Nothing Found');
}
 $connection = mysqli_connect('hostname', 'user','password','database');
?>
<?php
define('uniquenamevariable',true);
 require('connection.php');
 //some mysql transaction code goes here
?>

So if anyone will require it using a absolute path, he will be unknown of the unique variable name defined in the php code, which will stop him from digging inside the code.

There are a lot other security ways, stay tuned to our blogs to learn more...

Category :

Comments

No comments found.

Leave a Comment

JOIN TODAY

Affiliate Marketing

Latest Posts

Free Software Tools By M/s VIKASH TECH

Free Tools & Software

You want powerful software & tools—but you don’t have to buy them. Here is a list of free useful tools…

WhatsApp Messaging App Plugin

Simply - Connect: WhatsApp Messaging Plugin For Website

Simply-Connect : WhatsApp Messaging Plugin is easy to set up and use a plugin for all kinds of websites. It…

Website page Speed

How to improve Website Page speed?

What is Page Speed? Page speed is generally confused with "website speed", which is wrong. Page speed is specific to…

Web Hosting

Need web hosting? CLOUDPOKO is the best place for you!

Having a website is crucial, especially if you are serious about building a web presence.  To create a website— and…

Tags