PHP Licensing system

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sktanmoy
Forum Newbie
Posts: 4
Joined: Thu Jul 14, 2011 10:00 pm

PHP Licensing system

Post by sktanmoy »

I tried to create domain based licensing. The script should be check license each time it'll run. I'll use two domain which are hosted in two different servers and networks so that if a server is down, another one will help scripts for licensing purpose.
Look at the codes bellow. It is working if "domain.net" is up. But I want the code working is any of the servers is up or contain license file. Please help me..... :cry:

Code: Select all

<?php

function checkagain(){
if (false === file_get_contents("http://domain.net/lic/ok.xml",0,null,0,1)) {
   echo "Error, sorry!";
   die;
}
else
{
echo "All is well";
}
}

if (false === file_get_contents("http://domain.com/lic/ok.xml",0,null,0,1)) {
    echo checkagain();
}
else
{
echo "All is well";
}
?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP Licensing system

Post by Jonah Bron »

Uhhh, like this?

Code: Select all

if (file_get_contents("http://domain.com/lic/ok.xml",0,null,0,1) !== false || file_get_contents("http://domain.net/lic/ok.xml",0,null,0,1) !== false) {
    echo 'All is well';
} else {
    echo 'Error, sorry!';
}
sktanmoy
Forum Newbie
Posts: 4
Joined: Thu Jul 14, 2011 10:00 pm

Re: PHP Licensing system

Post by sktanmoy »

Jonah Bron wrote:Uhhh, like this?

Code: Select all

if (file_get_contents("http://domain.com/lic/ok.xml",0,null,0,1) !== false || file_get_contents("http://domain.net/lic/ok.xml",0,null,0,1) !== false) {
    echo 'All is well';
} else {
    echo 'Error, sorry!';
}
Yes, but check the output bellow:

Code: Select all

Warning: file_get_contents(http://domain.com/lic/ok.xml) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:\wamp\www\ok.php on line 3
All is well
I just create a license file in domain.net and in domain.com, there is no "ok.xml". That means if any of the server goes down, the error message like above will be shown. Actually I want to display "All is well" is any of the server contain the license file.
sktanmoy
Forum Newbie
Posts: 4
Joined: Thu Jul 14, 2011 10:00 pm

Re: PHP Licensing system

Post by sktanmoy »

I got the solution, just @file_get_contents to suppress errors.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP Licensing system

Post by Jonah Bron »

That will work, but be advised that you should be very conservative about suppressing errors.
sktanmoy
Forum Newbie
Posts: 4
Joined: Thu Jul 14, 2011 10:00 pm

Re: PHP Licensing system

Post by sktanmoy »

Jonah Bron wrote:That will work, but be advised that you should be very conservative about suppressing errors.
Yes, you are right, but I couldn't find any other way to fix the error. Any idea to fix the issue?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP Licensing system

Post by Jonah Bron »

No, no, file_get_contents() is a fine time to use it. I just mean be careful about suppressing errors because they can make some bugs hard to track down :)
Post Reply