Tuesday, April 5, 2016

Sunday, March 27, 2016

Websocket with PHP


There are many ways to create real time web application with PHP, but in this
case I will show you a best websocket library will be useful for making a real
time web application.

What is Websockt?

It is full-duplex (data can transmitted in both directions) clinet/Server commnuication over TCP.
so you will find out that websocket is the new feature of HTML5 that allow Client script to open
the bi-directional socket connections to a server. Now it work on Chrome, Firefox, Safari, Opera
and other browser even on mobile.

What is PHPWebsockets?

It is websocket libray server which written in PHP. You can
download from there : https://github.com/ghedipunk/PHP-WebSockets

testwebsock.php
#!/usr/bin/env php
send($user,$message);
  }
  
  protected function connected ($user) {
    // Do nothing: This is just an echo server, there's no need to track the user.
    // However, if we did care about the users, we would probably have a cookie to
    // parse at this step, would be looking them up in permanent storage, etc.
  }
  
  protected function closed ($user) {
    // Do nothing: This is where cleanup would go, in case the user had any sort of
    // open files or other objects associated with them.  This runs after the socket 
    // has been closed, so there is no need to clean up the socket itself here.
  }
}
$echo = new echoServer("0.0.0.0","9000");
try {
  $echo->run();
}
catch (Exception $e) {
  $echo->stdout($e->getMessage());
}


client.html

Please run the client.html and don't forget to lunch the testwebsocket.php via common line.

Friday, March 25, 2016

How To Install Node.js on Windows

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.



To install Node.js on your Windows, please follow all steps below.

Node.js on Machinetos

1.1. Download Node.js

Go to https://nodejs.org/en/download/ and download Windows Installer (.msi/.exe) (32bits/64bits)

1.2. Install Node.js

Open the file you have downloaded and click Next:


Agree the terms of the software license agreement and click Next:


Continue to click Next and Next and then click Install:


Click Finish:


Test Node.js

To make sure that Node.js is installed on you computer we have to test it by the following steps.

1. Open your CMD and type node --version and npm --version


If you see the version of your current Node.js and npm (Node Package Manager) it means that everything is okay.

If you have any questions related to this article, please leave your question on the comment form below. We will try to answer to your question as much as we can.

Thank your for reading our blog.

Thursday, March 24, 2016

How to Install Node.js on Mac

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.



To install Node.js on your Machinetos, please follow all steps below.

Node.js on Machinetos

1.1. Download Node.js

Go to https://nodejs.org/en/download/ and download Mac OS X Installer (.pkg)


1.2. Install Node.js

Open the file you have downloaded and click Continue:

Agree the terms of the software license agreement:

And then click on Install:

Congratulation! You just installed Node.js on your computer. Please close the window:

Test Node.js

To make sure that Node.js is installed on you computer we have to test it by the following steps.

1. Open your terminal and type node --version and npm --version

If you see the version of your current Node.js and npm (Node Package Manager) it means that everything is okay.

If you have any questions related to this article, please leave your question on the comment form below. We will try to answer to your question as much as we can.

Thank your for reading our blog.

Wednesday, March 23, 2016

Create nested menu with PHP, Mysql, CSS

How to create a nested menu ,

Before starting to implement , I would sure that you will understand the PHP, Mysql, HTML and CSS.

1- You have to download this project and try to create the database which you will be able to find in the zip file.
2- Test it on your computer, then if everything is fine please look through with the explanation.

- connection.php is the file to connect to database
- example.php is the file to display the menu
- style.css  the decoration of menu.

*connection.php
 define('DSN','mysql:host=localhost;dbname=db_menu');
 define('USERNAME' , 'root');
 define('PASSWORD' , '');
 try { 
      $DB = new PDO(DSN, USERNAME, PASSWORD); 
  } 
  catch(PDOException $e) { 
       echo $e->getMessage(); 
  } 
 
  try { 
       $sql = "SELECT * FROM drop_down";
 
       $result = $DB->query($sql);
       $row_count = $result->rowCount();
       if($row_count){
       $rows = $result->fetchAll(PDO::FETCH_ASSOC);
       }
  }catch(PDOException $e) { 
       echo $e->getMessage(); 
  } 

*example.php

*menu.css
body {
   font: 15px arial, helvetica, sans-serif;
 }
 ul.multidrop a {
   color: #fff;
   text-decoration: none;

 }
 ul.multidrop {
   padding: 0;
   margin: 0;
   background: #DF5B50;
   float: left;
 }
 ul.multidrop ul {
   padding: 0;
   margin: 0;
   background: #DF5B50;
   float: left;
 }
 ul.multidrop li {
   padding: 5px;
   float: left;
   display: inline;
   position: relative;
   width: 150px;
   list-style: none;
   cursor: pointer;
 }
   ul.multidrop li:hover {
   background: #3EC4F6;
 }
 ul.multidrop ul {
   position: absolute;
   left: 0;
   top: 100%;
   background: #3EC4F6;
   display: none;
 }
 ul.multidrop ul ul {
   left: 100%;
   top: 0;
   background: #DF5B50;
 }
 ul.multidrop li:hover > ul {
   display: block;
   background: #3EC4F6;
 }
 ul.multidrop li ul li:hover {
   background: #DF5B50;
 }

Next trip that related to this post will be published soon.

cheer!

Some Useful PHP Tricks You May Not Know

After I read many articles, I found some cool tricks in PHP that are very useful for our projects. Please have a look at 5 tricks below you may satisfy with them.



1. Array in Form Fields

Not only the form input fields that create array like
<input name="name['fullname']">
but you can create arrays with checkboxes also.

<label><input type="checkbox" name="hobbies[]" value="Sports" /> Sports</label><br />
<label><input type="checkbox" name="hobbies[]" value="Hiking" /> Hiking</label><br />
<label><input type="checkbox" name="hobbies[]" value="Swimming" /> Swimming</label><br />
<label><input type="checkbox" name="hobbies[]" value="Swimming" /> Watching Movies</label><br />

2. Count Character in a String

When you want to count the string you will think about strlen() function. However, we have a fastest way to count it. Take a look at the following code:

$string = 'testing';

if(isset($string[6]))
 echo "The string '$string' is at least 7 characters long.";
else
 echo "The string '$string' is less than 7 characters long.";

You treated $string like an array.

3. Use PHP Echo like a Boss

Most of the time we always thought that if we want to concatenate strings with echo, we need to use period. But we can use echo like a function that can replace period with commas instead. Please look at the fastest way to do so:
$string1 = 'test-string1';
$string2 = 'test-string2';
$string3 = 'test-string3';

echo 'String #1: ', $string1, '
'; echo 'String #2: ', $string2, '
'; echo 'String #3: ', $string3, '
';
You will find some more useful trick anywhere else, but these cool tricks is for you. Good luck!

Tuesday, March 22, 2016

Create component to validate IPN (Instant Payment Notification) with Cakephp


It's seem to be for senior programmer, but I just want to let somebody know the benefit or how to implement the IPN of PayPal using PHP or Cakephp, before you can understand what to do with IPN you have take a look this website.

If you don't know at all about Paypal please read the previous article : Paypal
class IpnComponent extends Component
{
   public function validate_ipn_data($data_ipn)
   {
     //Change these with your information
     //Sandbox for testing or empty ''
     $site = @$_SERVER['SERVER_NAME'];
     $paypalmode = $site === 'your_site' ? '' : 'sandbox';

    if ($data_ipn) {
       if ($paypalmode=='sandbox') {
          $paypalmode     =   '.sandbox';
       }
       $req = 'cmd=' . urlencode('_notify-validate');
       foreach ($data_ipn as $key => $value) {
           $value = urlencode(stripslashes($value));
           $req .= "&$key=$value";
        }
    $ch = curl_init();
    $url_ = 'https://www'.$paypalmode.'.paypal.com/cgi-bin/webscr';
    $host_ = 'Host: www'.$paypalmode.'.sandbox.paypal.com';
    curl_setopt($ch, CURLOPT_URL, $url_);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array($host_));
    $res = curl_exec($ch);
    curl_close($ch);

    if (strcmp ($res, "VERIFIED") == 0) {
         $item_number = $data_ipn['item_number'];
         $model      = ClassRegistry::init('Reservation');
                $model->id = $item_number;
                $model->save(array('is_paid' => 1));
    }
  }
 }  
}
1- Please create file in Component and pass the code above, then save.
2- You have to load the component in your controller
3- Create a function and call the  validate_ipn_data from IPN component
Note : Make sure that your URL pointed to Paypal.