Saturday, May 18, 2019

PHP : How to add reCAPTCHA to your forms

STEP 1:
Paste this in header,

<script src='https://www.google.com/recaptcha/api.js'></script>


STEP 2:
Paste this in the form where you want the widget to appear,

<div class="g-recaptcha" data-sitekey="_______________PUBLIC_KEY_______________"></div>


STEP 3:

EDIT: The code below is updated from the video so that you only need one page. With an if/else statement, you can check if the form has been submitted. If it has, the reCAPTCHA success or error message will appear. If not, the form will appear. 

<?php
// Checks if form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    function post_captcha($user_response) {
        $fields_string = '';
        $fields = array(
            'secret' => '_______________PRIVATE_KEY_______________',
            'response' => $user_response
        );
        foreach($fields as $key=>$value)
        $fields_string .= $key . '=' . $value . '&';
        $fields_string = rtrim($fields_string, '&');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

        $result = curl_exec($ch);
        curl_close($ch);

        return json_decode($result, true);
    }

    // Call the function post_captcha
    $res = post_captcha($_POST['g-recaptcha-response']);

    if (!$res['success']) {
        // What happens when the CAPTCHA wasn't checked
        echo '<p>Please go back and make sure you check the security CAPTCHA box.</p><br>';
    } else {
        // If CAPTCHA is successfully completed...

        // Paste mail function or whatever else you want to happen here!
        echo '<br><p>CAPTCHA was completed successfully!</p><br>';
    }
} else { ?>
    
<!-- FORM GOES HERE -->
<form></form>

<?php } ?>




<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Recaptcha Demo</title>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body> 

<?php
// Checks if form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    function post_captcha($user_response) {
        $fields_string = '';
        $fields = array(
            'secret' => '_______________PRIVATE_KEY_______________',
            'response' => $user_response
        );
        foreach($fields as $key=>$value)
        $fields_string .= $key . '=' . $value . '&';
        $fields_string = rtrim($fields_string, '&');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

        $result = curl_exec($ch);
        curl_close($ch);

        return json_decode($result, true);
    }

    // Call the function post_captcha
    $res = post_captcha($_POST['g-recaptcha-response']);

    if (!$res['success']) {
        // What happens when the CAPTCHA wasn't checked
        echo '<p>Please go back and make sure you check the security CAPTCHA box.</p><br>';
    } else {
        // If CAPTCHA is successfully completed...

        // Paste mail function or whatever else you want to happen here!
        echo '<br><p>CAPTCHA was completed successfully!</p><br>';
    }
} else { ?>
    
    <!-- FORM GOES HERE -->
    <form action="recaptcha-v2.php" method="post">
        <label for="fname">First Name*</label><br>
        <input type="text" name="fname" id="fname" required autofocus><br><br>

        <label for="lname">Last Name*</label><br>
        <input type="text" name="lname" id="lname" required><br><br>

        <label for="email">Email Address*</label><br>
        <input type="email" name="email" id="email" required><br><br>

        <div class="g-recaptcha" data-sitekey="_______________PUBLIC_KEY_______________"></div>
        <br>
        <input type="submit" id="submit" value="Submit">
    </form>

<?php } ?>

</body>
</html>

https://www.makingspidersense.com/tutorials/tut-recaptcha.txt

https://www.makingspidersense.com/tutorials/recaptcha-v2.txt

Javascript Basics : Difference between require and imports in javascript.

It relates to JavaScript modules. JavaScript code can be broken down into modules (which you require), but each module can “export” different things. You use import to specify what you want to, well, import from a module.

So say you had an imaginary shapes module, that exports a Circle and a Rectangle class. If you did require shapes it would import everything from that module. Instead, you can use import to only, well, import specific items from a module. So if you only wanted the Circle, you could do. 

Thursday, May 16, 2019

PHP : Seven most important, most practical and most useful PHP Magic Constants

  • __FILE__ – The full path and filename of the file.
  • __DIR__ – The directory of the file.
  • __FUNCTION__ – The function name.
  • __CLASS__ – The class name.
  • __METHOD__ – The class method name.
  • __LINE__ – The current line number of the file.
  • __NAMESPACE__ – The name of the current namespace

<?php

 // Set namespace (works only with PHP 5.3)
 namespace TestProject;

 // This prints file full path and name
 echo "This file full path and file name is '" . __FILE__ . "'.\n";

 // This prints file full path, without file name
 echo "This file full path is '" . __DIR__ . "'.\n";

 // This prints current line number on file
 echo "This is line number " . __LINE__ . ".\n";

 // Really simple basic test function
 function test_function_magic_constant() {
  echo "This is from '" . __FUNCTION__ . "' function.\n";
 }

 // Prints function and used namespace
 test_function_magic_constant();

 // Really simple class for testing magic constants
 class TestMagicConstants {
  // Prints class name
  public function printClassName() {
   echo "This is " . __CLASS__ . " class.\n";
  }

  // Prints class and method name
  public function printMethodName() {
   echo "This is " . __METHOD__ . " method.\n";
  }

  // Prints function name
  public function printFunction() {
   echo "This is function '" . __FUNCTION__ . "' inside class.\n";
  }

  // Prints namespace name (works only with PHP 5.3)
  public function printNamespace() {
   echo "Namespace name is '" . __NAMESPACE__ . "'.\n";
  }
 }

 // Create new TestMagicConstants class
 $test_magic_constants = new TestMagicConstants;

 // This prints class name and used namespace
 $test_magic_constants->printClassName();

 // This prints method name and used namespace
 $test_magic_constants->printMethodName();

 // This prints function name inside class and used namespace
 // same as method name, but without class
 $test_magic_constants->printFunction();

 // This prints namespace name (works only with PHP 5.3)
 $test_magic_constants->printNamespace();

?>




This file full path and file name is '/tmp/magic_constants/magic.php'.
This file full path is '/tmp/magic_constants'.
This is line number 13.
This is from 'TestProject\test_function_magic_constant' function.
This is TestProject\TestMagicConstants class.
This is TestProject\TestMagicConstants::printMethodName method.
This is function 'printFunction' inside class.
Namespace name is 'TestProject'.

Wednesday, May 15, 2019

Database : What is a database transaction?

A transaction is a unit of work that you want to treat as "a whole." It has to either happen in full or not at all.
A classical example is transferring money from one bank account to another. To do that you have first to withdraw the amount from the source account, and then deposit it to the destination account. The operation has to succeed in full. If you stop halfway, the money will be lost, and that is Very Bad.
In modern databases transactions also do some other things - like ensure that you can't access data that another person has written halfway. But the basic idea is the same - transactions are there to ensure, that no matter what happens, the data you work with will be in a sensible state. They guarantee that there will NOT be a situation where money is withdrawn from one account, but not deposited to another.

A transaction is a way of representing a state change. Transactions ideally have four properties, commonly known as ACID:
  • Atomic (if the change is committed, it happens in one fell swoop; you can never see "half a change")
  • Consistent (the change can only happen if the new state of the system will be valid; any attempt to commit an invalid change will fail, leaving the system in its previous valid state)
  • Isolated (no-one else sees any part of the transaction until it's committed)
  • Durable (once the change has happened - if the system says the transaction has been committed, the client doesn't need to worry about "flushing" the system to make the change "stick")

Monday, May 13, 2019

Cakephp : Different between belongs to and has one?



image

Its just the way you read, using yor image as example:
User (id - primary key) => hasOne => Profile (user_id - foreing key)
this way the developer and the application know that when you are at a USER context and need to look for this user PROFILE information you should search in the profiles table by the user foreing key (user_id)
Profile (user_id - foreing key) => BelongsTo => User (id - primary key)
in this other way when you are at a profile context and need know whats the associated user of this profile you should search the user’s primary key (id) in USERS table
all this information are used to compose the sql joins


https://discourse.cakephp.org/t/different-between-belongs-to-and-has-one/3234