hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Problem with a script...
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

Problem with a script...

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 08-06-2012, 02:34 AM
Sreeganesh Sreeganesh is offline
WHT Addict
 
Join Date: Jan 2012
Location: Lab
Posts: 107

Problem with a script...


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;

Reply With Quote


Sponsored Links
  #2  
Old 08-06-2012, 03:21 AM
Matt R Matt R is offline
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!

Reply With Quote
  #3  
Old 08-06-2012, 03:48 AM
Matt R Matt R is offline
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!

Reply With Quote
Sponsored Links
  #4  
Old 08-06-2012, 03:49 AM
Matt R Matt R is offline
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!

Reply With Quote
  #5  
Old 08-06-2012, 04:03 AM
Sreeganesh Sreeganesh is offline
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 " <"

Reply With Quote
  #6  
Old 08-06-2012, 04:24 AM
Matt R Matt R is offline
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!

Reply With Quote
  #7  
Old 08-06-2012, 04:33 AM
Matt R Matt R is offline
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!

Reply With Quote
  #8  
Old 08-06-2012, 09:37 AM
tickedon tickedon is offline
Retired Moderator
 
Join Date: Oct 2003
Location: Scotland, UK
Posts: 2,846
Quote:
Originally Posted by Sreeganesh View Post
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!

Reply With Quote
  #9  
Old 08-06-2012, 09:42 AM
Sreeganesh Sreeganesh is offline
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" .

Reply With Quote
Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with script almowaly Hosting Security and Technology 2 06-02-2009 01:17 PM
script problem ... vince2doom Programming Tutorials 2 01-09-2006 02:11 PM
php problem? Apache problem? Mime? Script can't do upload.... galacnet Hosting Security and Technology 7 05-30-2005 11:15 AM
Help!! Script Problem Please help will_stevens Programming Discussion 4 03-06-2004 04:09 PM
script problem!!! skrillz Programming Discussion 4 05-11-2003 11:46 AM

Related posts from TheWhir.com
Title Type Date Posted
Pingdom Talks Top Web Hosting Cities and Countries Web Hosting News 2013-03-27 18:49:54
Whistleblower Site Cryptome Hacked, Infects PCs with Drive-By Exploits Web Hosting News 2012-02-14 14:48:24
Control Panel cPanel Launches New Apache Configuration Script Web Hosting News 2011-12-28 19:41:39
Web Host HostingZoom Adds Softaculous Auto-Installer to Hosting Plans Web Hosting News 2011-08-17 17:52:34
Web Host JaguarPC Adds Auto-Installer Softaculous to Hosting Plans Web Hosting News 2011-07-27 18:55:46


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?