How to create envato purchase code verification in php

How to create envato purchase code verification in php

Things required first

  • Live server
  • domain or subdomain

connect.php

$servername = "localhost";
$username = "u957918675_temp";
$password = "~EnAb1Qm1";
$db = "u957918675_temp";

// Create connection
$conn = new mysqli($servername, $username, $password, $db);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";



index.php

include "connect.php";

if(isset($_GET['purchase_code'])){
     $purchase_code = $_GET['purchase_code'];
     
     $query="SELECT * FROM verify WHERE pcode ='$purchase_code'";
     $result=$conn->query($query);
      if($result->num_rows>0){
          while($row=$result->fetch_assoc())
          {
            
           if($result->num_rows>0){
                $responce = array("msg" => "Valid purchase code", "status" => "true");
           }else
           {
                $responce = array("msg" => "Invalid", "status" => "false");
           }
           
          }
      }else{
           $responce = array("msg" => "cannot get the Pcode", "status" => "false");
      }
   
}else{
     $responce = array("msg" => "cannot get the Pcode", "status" => "false");
}

  echo json_encode($responce);

Verify.php

function verify_purchase_code($code) {
    $code = urlencode($code);
    $url = "http://envato.codelone.com/index.php?purchase_code=" . $code;

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Content-type: application/json'));

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

   if (!$ret) {
        $ret = file_get_contents($url);
    }

    $data = json_decode($ret??"{}",true);
    
   // print_r($data);

   if($data["status"]){
       echo "valid go ahead!";
  
  }else{
    return false;   
    exit();
  }
}

verify_purchase_code("41545");

TABLE STRUCTURE

-- --------------------------------------------------------

--
-- Table structure for table `verify`
--

CREATE TABLE `verify` (
  `vid` int(11) NOT NULL,
  `element` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `pcode` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Dumping data for table `verify`
--

INSERT INTO `verify` (`vid`, `element`, `pcode`, `date`) VALUES
(1, '124568', '41545', '2022-05-01 10:03:19');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `verify`
--
ALTER TABLE `verify`
  ADD PRIMARY KEY (`vid`);

--
-- AUTO_INCREMENT for dumped tables
--