
08-06-2012, 02:34 AM
|
|
WHT Addict
|
|
Join Date: Jan 2012
Location: Lab
Posts: 107
|
|
Hi,
I try to create a small php License System with the help of a php tutorial. but i'm getting some error.
Could you please help to troubleshot it ?
Client Side File
Quote:
<?php
$pass_array['key'] = "123-456";
$pass_array['domain'] = $_SERVER['SERVER_NAME'];
$pass_array['website_ip'] = $_SERVER['SERVER_ADDR'];
function confirm_license($url, $data)
{
$options = array(CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => false, CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 50, CURLOPT_TIMEOUT => 50, CURLOPT_MAXREDIRS => 0,
CURLOPT_POST => 1, CURLOPT_POSTFIELDS => $data, CURLOPT_SSL_VERIFYHOST => 0, );
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
$license = confirm_license("http://www.yousite.com/path/to/folder/server/", $pass_array);
if ($license['status'] != "1")
{
die($license['message']);
}
?>
|
Server Side File
Quote:
<?php
if(!mysql_connect("DATABASE_HOSTNAME", "DATABASE_USERNAME", "DATABASE_PASSWORD")){
echo "Error: Connection To The MySQL Server Failed.";
}
if(!mysql_select_db("DATABASE_NAME")){
echo "Error: Unable To Select Database.";
}
$required_keys = array("key", "domain", "website_ip");
foreach ($required_keys as $req_key)
{
if (array_key_exists($req_key, $_POST))
{
$sanitised[$req_key] = stripslashes(strip_tags($_POST[$req_key]));
}
else
{
echo "Error: " . $req_key . " missing from passed variables.";
break 1;
}
}
$ret_db = mysql_query("SELECT * FROM `license` WHERE `key` = '" .
mysql_real_escape_string($sanitised['key']) . "' && `domain` = '" .
mysql_real_escape_string($sanitised['domain']) . "' && `website_ip` = '" .
mysql_real_escape_string($sanitised['website_ip']) . "' ORDER BY `id` DESC LIMIT 0,1");
if (mysql_num_rows($ret_db) == "0")
{
echo "Invalid Details.";
}
else
{
$retdb = mysql_fetch_array($ret_db);
if ($retdb['status'] == "active")
{
echo "1";
}
else
{
if ($retdb['status'] == "inactive")
{
echo "Details Valid, License Status Inactive.";
}
else
{
echo "Details Valid, License Status Unknown.";
}
}
}
?>
|
SQL File
Quote:
CREATE TABLE `license` (
`id` int(50) NOT NULL auto_increment,
`key` varchar(50) NOT NULL,
`domain` varchar(300) NOT NULL,
`website_ip` varchar(70) NOT NULL,
`status` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
|
|

08-06-2012, 03:21 AM
|
|
Web Hosting Master
|
|
Join Date: Mar 2005
Location: Florida
Posts: 2,521
|
|
Well, knowing what the error you're receiving is would certainly be helpful 
__________________
█ Matthew Rosenblatt, and I'm the lead developer for CloudPanel.
█ Check out the DreamVelop Blog for more information!
█ I'm back in the country. I work as a lighting technician for Celebrity Cruises.
█ Want to read about my job and my travels? Click here!
|

08-06-2012, 03:48 AM
|
|
Web Hosting Master
|
|
Join Date: Mar 2005
Location: Florida
Posts: 2,521
|
|
Here's a decent (and untested) example of what I would do on the server-side script.
PHP Code:
<?php
/* This assumes you have ADODB installed and readily available. For more information on ADODB, visit http://adodb.sourceforge.net/
CREATE TABLE `licenses` ( `id` int(9) NOT NULL auto_increment, `license` varchar(50) NOT NULL, `domain` varchar(32) NOT NULL, `web_ip` bigint(32) NOT NULL, `status` ENUM('active', 'inactive'), PRIMARY KEY (`id`) ) ENGINE=MyISAM;
*/
define('DB_TYPE','mysql'); define('DB_HOST','localhost'); define('DB_USER','username'); define('DB_NAME','database_name'); define('DB_PASS','password');
class license { private $license; private $domain; private $ip; function __construct(){ $this->ip = ip2long($this->getIP()); $this->license = $this->sanitize($_POST['license']);
$this->domain = $this->sanitize($_POST['domain']); } function getIP(){ if (!empty($_SERVER['HTTP_CLIENT_IP'])) { //check ip from share internet $ip=$_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //to check ip is pass from proxy $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip=$_SERVER['REMOTE_ADDR']; } return $ip; } function sanitize($input) { //Connect to the Database $db = NewADOConnection(DB_TYPE); $db->PConnect(DB_HOST,DB_USER,DB_PASS,DB_NAME); if (is_array($input)) { foreach($input as $var=>$val) { $output[$var] = sanitize($val); } } else { if (get_magic_quotes_gpc()) { $input = stripslashes($input); } //$input = cleanInput($input); $output = mysql_real_escape_string($input); } $db->Close(); return $output; } function validateLicense(){ //Connect to the Database $DB = NewADOConnection(DB_TYPE); $DB->PConnect(DB_HOST,DB_USER,DB_PASS,DB_NAME); if(!empty($domain) && !empty($license)){ $check = $DB->Execute("SELECT id FROM licenses WHERE license='{$this->license}' AND domain='{$this->domain}' AND web_ip='{$this->ip}'"); if($check === false){ // There was an error executing the query $result['status'] = false; $result['message'] = 'An SQL related error occured while checking your domain name against the licensing server.'; } else { // SQL query went through properly. if($check->rowCount() > 0){ // The license is valid. $result['status'] = true; } else { // There were no results. License is invalid. $result['status'] = false; $result['message'] = 'The license key is not valid for the domain and server IP address provided.'; } } } else { // Information is missing. Throw an error. $result['status'] = false; $result['message'] = 'Please provide both the domain name and license key.'; } return $result; } }
?>
__________________
█ Matthew Rosenblatt, and I'm the lead developer for CloudPanel.
█ Check out the DreamVelop Blog for more information!
█ I'm back in the country. I work as a lighting technician for Celebrity Cruises.
█ Want to read about my job and my travels? Click here!
|

08-06-2012, 03:49 AM
|
|
Web Hosting Master
|
|
Join Date: Mar 2005
Location: Florida
Posts: 2,521
|
|
But, there's still a lot of things wrong with the way that works fundamentally. There's no client-side caching, so the server ALWAYS needs to be online. There's no server-side caching in the event of a high frequency of license requests. There's no failover. Etc...
__________________
█ Matthew Rosenblatt, and I'm the lead developer for CloudPanel.
█ Check out the DreamVelop Blog for more information!
█ I'm back in the country. I work as a lighting technician for Celebrity Cruises.
█ Want to read about my job and my travels? Click here!
|

08-06-2012, 04:03 AM
|
|
WHT Addict
|
|
Join Date: Jan 2012
Location: Lab
Posts: 107
|
|
Thanks Matt...
i'm really interested in php
and now i'm learning php via some tutorials... have any tutorials or sample script to add caching to client side
on my script im only getting " <"
|

08-06-2012, 04:24 AM
|
|
Web Hosting Master
|
|
Join Date: Mar 2005
Location: Florida
Posts: 2,521
|
|
Well, for one, the output of your license checking function is going to be a string and not an array.
There's a lot fundamentally wrong. IF someone else doesn't get to it, I'll clarify when I wake up. But basically, you're assuming the output of the CURL script is going to be an array when it won't be.
__________________
█ Matthew Rosenblatt, and I'm the lead developer for CloudPanel.
█ Check out the DreamVelop Blog for more information!
█ I'm back in the country. I work as a lighting technician for Celebrity Cruises.
█ Want to read about my job and my travels? Click here!
|

08-06-2012, 04:33 AM
|
|
Web Hosting Master
|
|
Join Date: Mar 2005
Location: Florida
Posts: 2,521
|
|
I just realized that I missed a fundamental part of that script for the licensing.
PHP Code:
<?php
/* This assumes you have ADODB installed and readily available. For more information on ADODB, visit http://adodb.sourceforge.net/
CREATE TABLE `licenses` ( `id` int(9) NOT NULL auto_increment, `license` varchar(50) NOT NULL, `domain` varchar(32) NOT NULL, `web_ip` bigint(32) NOT NULL, `status` ENUM('active', 'inactive'), PRIMARY KEY (`id`) ) ENGINE=MyISAM;
*/
define('DB_TYPE','mysql'); define('DB_HOST','localhost'); define('DB_USER','username'); define('DB_NAME','database_name'); define('DB_PASS','password');
class license { private $license; private $domain; private $ip; function __construct(){ $this->ip = ip2long($this->getIP()); $this->license = $this->sanitize($_POST['license']);
$this->domain = $this->sanitize($_POST['domain']); } function getIP(){ if (!empty($_SERVER['HTTP_CLIENT_IP'])) { //check ip from share internet $ip=$_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //to check ip is pass from proxy $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip=$_SERVER['REMOTE_ADDR']; } return $ip; } function sanitize($input) { //Connect to the Database $db = NewADOConnection(DB_TYPE); $db->PConnect(DB_HOST,DB_USER,DB_PASS,DB_NAME); if (is_array($input)) { foreach($input as $var=>$val) { $output[$var] = sanitize($val); } } else { if (get_magic_quotes_gpc()) { $input = stripslashes($input); } //$input = cleanInput($input); $output = mysql_real_escape_string($input); } $db->Close(); return $output; } function validateLicense(){ //Connect to the Database $DB = NewADOConnection(DB_TYPE); $DB->PConnect(DB_HOST,DB_USER,DB_PASS,DB_NAME); if(!empty($domain) && !empty($license)){ $check = $DB->Execute("SELECT id,status FROM licenses WHERE license='{$this->license}' AND domain='{$this->domain}' AND web_ip='{$this->ip}'"); if($check === false){ // There was an error executing the query $result['status'] = false; $result['message'] = 'An SQL related error occured while checking your domain name against the licensing server.'; } else { // SQL query went through properly. if($check->rowCount() > 0){ if($check->fields['status'] == "active"){ // The license is valid. $result['status'] = true; } else { // The license is inactive. $result['status'] = false; $result['message'] = 'The license key you have installed is currently inactive. Please contact our support department for further assistance.'; } } else { // There were no results. License is invalid. $result['status'] = false; $result['message'] = 'The license key is not valid for the domain and server IP address provided.'; } } } else { // Information is missing. Throw an error. $result['status'] = false; $result['message'] = 'Please provide both the domain name and license key.'; } return $result; } }
?>
The usage of this whole thing would be something like this --
filename: checklicense.php
PHP Code:
<?php if($_POST['license'] && $_POST['domain']){ include('licensing.php'); $license = new License; $res = $license->validateLicense(); echo json_encode($res); }
?>
__________________
█ Matthew Rosenblatt, and I'm the lead developer for CloudPanel.
█ Check out the DreamVelop Blog for more information!
█ I'm back in the country. I work as a lighting technician for Celebrity Cruises.
█ Want to read about my job and my travels? Click here!
|

08-06-2012, 09:37 AM
|
|
Retired Moderator
|
|
Join Date: Oct 2003
Location: Scotland, UK
Posts: 2,846
|
|
Quote:
Originally Posted by Sreeganesh
Thanks Matt...
i'm really interested in php
and now i'm learning php via some tutorials... have any tutorials or sample script to add caching to client side
on my script im only getting " <"
|
Are you using PHP short tags anywhere ("<?" instead of "<?php")? If so, that could be your problem - either change them to a full <?php or enable short tags in the php.ini.
Don't forget once you've got your licensing sorted, you'll need to encode the code to stop someone tampering with it - there are a few different options, but I'd recommend ionCube ( www.ioncube.com) - they have both an online and software encoder option.
__________________
Alasdair - SolidPHP, Inc.
SPBAS - Business Automation Software for web hosts, web-applications, PHP licensing and digital goods delivery.
Customer management, billing & invoicing, email marketing, integrated helpdesk, multiple brands support & more.
Now with Web Hosting and Domain Reg/Transfer/Renew Support!
|

08-06-2012, 09:42 AM
|
|
WHT Addict
|
|
Join Date: Jan 2012
Location: Lab
Posts: 107
|
|
Thanks for the suggestion "tickedon" I think majority of professionals are using ionCube .
i used "<?php" .
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
| Postbit Selector |
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
| Login: |
|
|
| Advertisement: |
|
|
| Web Hosting News: |
|
|
|