Month: February 2018

How to check port is open and not open using PHP?

Sometime this is need to know that some port is active or not cause a port is used to recognize a specific service which is being executed in a computer. To recognize a port and to send packets, 16 bit numbers known as port numbers are used. A port number along with IP address serves as a destination for packets to be sent.

Suppose domain name https://techparkbd.com/ so get this ip address so ip is 184.107.37.81. There are many website to know ip. Then check port that open or not.

<?php
$host_connection = '184.107.37.81'; // techparkbd
$ports_number = array(21, 25, 80, 81, 110,143, 443, 3306); //checking port number

foreach ($ports_number as $port)
{
    $connection_check = @fsockopen($host_connection, $port);

    if (is_resource($connection_check))
    {
        echo '<h5>' . $host_connection . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open now.</h5>' . "\n";

        fclose($connection_check);
    }

    else
    {
        echo '<h5 style="color: red">' . $host_connection . ':' . $port . ' is not open now.</h5>' . "\n";
    }
}

?>

when you run this code it will show result like this

184.107.37.81:21 (ftp) is open now.
184.107.37.81:25 (smtp) is open now.
184.107.37.81:80 (http) is open now.
184.107.37.81:81 is not open now.
184.107.37.81:110 (pop3) is open now.
184.107.37.81:143 (imap) is open now.
184.107.37.81:443 (https) is open now.
184.107.37.81:3306 () is open now.

This is very simple way to know port open or not. If you face any problem then please inform me.

Comment

json_encode not working uft8 content , solve by use extra parameter in json_encode

Sometime json_encode not working in uft8 content so this is very common problem , In this article it will solve this problem. This is simple code which help you.

<?php
$data = [
    'গুগলের ‘স্টাইল আইডিয়াস’',
    'ফেসবুকের নয়া উদ্যোগে'
];
echo "<pre>";
print_r($data);
echo "<br>";
echo json_encode($data); // normal json_encode
echo "<br>";
echo json_encode($data, JSON_UNESCAPED_UNICODE); // json_encode with extra parameter which Encode multibyte Unicode characters literally

Then output is :

This is array data:

print_r($data);
Array
(
    [0] => গুগলের ‘স্টাইল আইডিয়াস’
    [1] => ফেসবুকের নয়া উদ্যোগে
)

This is normal json_encode

echo json_encode($data);
output will be is:

["\u0997\u09c1\u0997\u09b2\u09c7\u09b0 \u2018\u09b8\u09cd\u099f\u09be\u0987\u09b2 \u0986\u0987\u09a1\u09bf\u09df\u09be\u09b8\u2019","\u09ab\u09c7\u09b8\u09ac\u09c1\u0995\u09c7\u09b0 \u09a8\u09df\u09be \u0989\u09a6\u09cd\u09af\u09cb\u0997\u09c7"]

json_encode with JSON_UNESCAPED_UNICODE

echo json_encode($data, JSON_UNESCAPED_UNICODE);
output will be: 

["গুগলের ‘স্টাইল আইডিয়াস’","ফেসবুকের নয়া উদ্যোগে"]

If you face this problem then use this code which help you, if you face any problem then please inform me. I will try to help you.

Comment

Dynamic Drag and Drop using PHP & mysql and jQuery

This is very common feature that items list need to drag and drop and save it. Drag’n drop generally looks hard-to-apply but it is definitely not by using JavaScript frameworks. In this article i will show how the drag’n drop & saving the new positions to the database Will be working”. This is very simple process you can apply it. This is simple code which you can check

First create a simple database Suppose “drag” . Here is the code for create a table then run this code in mysql

CREATE TABLE `records` (
 `recordID` int(11) NOT NULL auto_increment,
 `recordText` varchar(255) default NULL,
 `recordListingID` int(11) default NULL,
 PRIMARY KEY (`recordID`)
 );
 
INSERT INTO `records` VALUES ('1', 'Once dropped, an Ajax query is activated', '3');
 INSERT INTO `records` VALUES ('2', 'Dragging changes the opacity of the item', '2');
 INSERT INTO `records` VALUES ('3', 'Returned array can be found at the right', '1');
 INSERT INTO `records` VALUES ('4', 'It is very very easy', '4');

The most important column in the database is recordListingID which shows us the order of the records.

Then create a php page which ready mysql connect .

<?php
/*
* Author : Ali Aboussebaba
* Email : bewebdeveloper@gmail.com
* Website : http://www.bewebdeveloper.com
* Subject : Dynamic Drag and Drop with jQuery and PHP
*/

// PDO connect *********
function connect() {
	$host = 'localhost';
	$db_name = 'drag';
	$db_user = 'root';
	$db_password = '';
    return new PDO('mysql:host='.$host.';dbname='.$db_name, $db_user, $db_password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}
?>

Then create index.php which is the main page to view list

<!DOCTYPE>
<html>
<?php
include('config.php');
$pdo = connect();
$sql = 'SELECT * FROM records ORDER BY recordListingID ASC';
$query = $pdo->prepare($sql);
$query->execute();
$list = $query->fetchAll();

?>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>jQuery Dynamic Drag'n Drop</title>
    <script type="text/javascript" src="http://techparkbd.com/subdomain/source_file/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="http://techparkbd.com/subdomain/source_file/jquery-ui-1.10.4.custom.min.js"></script>

    <style>
        body {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 16px;
            margin-top: 10px;
        }

        ul {
            margin: 0;
        }

        #contentWrap {
            width: 700px;
            margin: 0 auto;
            height: auto;
            overflow: hidden;
        }

        #contentTop {
            width: 600px;
            padding: 10px;
            margin-left: 30px;
        }

        #contentLeft {
            float: left;
            width: 400px;
        }

        #contentLeft li {
            list-style: none;
            margin: 0 0 4px 0;
            padding: 10px;
            background-color:#00CCCC;
            border: #CCCCCC solid 1px;
            color:#fff;
        }

        #contentRight {
            float: right;
            width: 260px;
            padding:10px;
            background-color:#336600;
            color:#FFFFFF;
        }

    </style>
    <script type="text/javascript">
        $(document).ready(function(){

            $(function() {
                $("#contentLeft ul").sortable({ opacity: 0.6, cursor: 'move', update: function() {
                    var order = $(this).sortable("serialize");
                    $.ajax({
                        url: 'http://localhost/updatelist.php?action=updateRecordsListings',
                        type: 'POST',
                        data: order,
                        success: function (result) {
                            $("#contentRight").html(result);
                        }
                    });
                }
                });
            });

        });
    </script>

</head>
<body>

<div id="contentWrap">
    <div id="contentLeft">
        <ul>
            <?php
            foreach ($list as $rs) {
                ?>
                <li id="recordsArray_<?=$rs['recordID']?>"><?=$rs['recordText']?></li>
                <?php
            }
            ?>
        </ul>
    </div>

    <div id="contentRight">
        <p>New List will be displayed here.</p>
        <p>&nbsp; </p>
    </div>

</div>

</body>
</html>


After posting the array of “new order of the items” to updatelist.php, we must run a query to update our database that will reflect the last positions of every item:

<?php
include('config.php');
$pdo = connect();

$action = $_GET['action'];
$updateRecordsArray = $_POST['recordsArray'];

if ($action == "updateRecordsListings"){

    $listingCounter = 1;
    foreach ($updateRecordsArray as $recordIDValue) {

        $query = "UPDATE records SET recordListingID = " . $listingCounter . " WHERE recordID = " . $recordIDValue;
        $query = $pdo->prepare($query);
        $query->execute();
        $listingCounter = $listingCounter + 1;
    }

    echo '<pre>';
    print_r($updateRecordsArray);
    echo '</pre>';
    echo 'If you refresh the page, you will see that records will stay just as you modified.';
}
?>

all done so run index.php and drag list , it will automatically save drag list and after refresh it will show new list

This is example output


this is output

Comment