View Full Version : Very simple PHP license system
UrlGuy 05-08-2005, 07:18 PM Okay I got this script..
What it basicly do is..
It do fopen() and fread() on a file, if the content matches whatever string you have set.. then start code..
Like this:
<?php
$fp = fopen("http://domain.com/lic.txt", "r");
stream_set_timeout($fp, 10);
$license = fread($fp, filesize($filename));
fclose($fp);
if ($license == "1234567") {
echo "Your license key is valid";
} else {
die("Invalid license key");
}
?>
I also have a lic.txt at domain.com which contains the string "1234567".
When I run this script it output this error:
Warning: fread(): Length parameter must be greater than 0. in /home/user/public_html/lic.php on line 5
Invalid license key
But if I put lic.txt next to this script and use this instead:
$fp = fopen("lic.txt", "r");
then it works perfectly..
As you probably understand Im going to use this as a very very simple lilcense system and I cant have the correct 'serial' next to it, but rather have it somewhere else or on other server as the user who uses this must not be able to view the correct key.. (yes I will encode) so the lic.txt needs to be on another serv.. but that doesnt seem to work :S
Any ideas how I can do this?
orbitz 05-08-2005, 07:23 PM are you planning to encode your script by any mean?
UrlGuy 05-08-2005, 07:24 PM Yea IONCube, and note: this is not meant as a serious licensing system I KNOW it is easy to crack and so.. lol =P
orbitz 05-08-2005, 07:30 PM no, i don't think it is easy. in fact, i was about to use similar method for some scripts of mine. To bypass your license check, they have to crack Ioncube which is impossible up until now. :)
however, the important thing is that if your sever ever goes down, your clients can not use your script -they would come after you :) so, to be safe...check if the file exists or can be read, if not....let the script be executed as usual without your customer knowing it :)
UrlGuy 05-08-2005, 07:31 PM Cool, lol
But still.. I cant have the .txt containing the license next to the script :\
Like I add it to some product which I send to some guy, I want to be able to shut he's script down at any time by changing the content of lic.txt, so that definetly needs to reside at my server, not next to his script :\
I thought fopen(); could be used for opening remote files =|
If this is not working.. could I use any similar methods? fsockopen or something like that to make it work?
orbitz 05-08-2005, 07:35 PM just update my post, there's an important part added.
for your code, i haven't taken a look yet. lolz
UrlGuy 05-08-2005, 07:37 PM Yea.. that is also an issue.. =\
But ill take that after hopefully solving this first problem hehe.. step by step.. (im very newb with PHP :\ )
So needs this thing solved first.. most importand thing in a license system would be that the user couldnt manipulate it as they'd want :\
I could possibly do that part by umm.. if server doesnt respond to ping.. then allow code as normal and act as it had valid license.. only when the server goes down tho.. as it will most likely not be down that much for ppl to take advantage of the script that much =| just an idea..
intransit 05-08-2005, 07:38 PM It appears that you're calling the variable $filename when it hasn't been declared. Try:
$url="http://domain.com/"
$filename="lic.txt";
$fp = fopen($url.$filename, "r");
stream_set_timeout($fp, 10);
$license = fread($fp, filesize($filename));
fclose($fp);
Also, don't forget to initialize the $license variable.
Edit: Sorry, combined the variables. This works for me when loading an outside file. Make sure allow_url_fopen is enabled in php.ini if this doesn't work.
UrlGuy 05-08-2005, 07:41 PM yyea that was the code I started with.. just shortened it down a little to make it look easier... just the regular fopen() thing with url inside instead of variable
UrlGuy 05-08-2005, 07:43 PM umm.. yea the variable was undecleared now hm..
As I started with (tho didnt post before i shortened it down ) I get these errors when I run that:
Warning: filesize(): Stat failed for http://user.com/lic.txt (errno=2 - No such file or directory) in /home/user/public_html/lic.php on line 6
Warning: fread(): Length parameter must be greater than 0. in /home/user/public_html/lic.php on line 6
Invalid license key
I know it says "errno=2 - No such file or directory" But there is such a file or directory.. i double checked everything was correct.. filename.. extension.. directory..
orbitz 05-08-2005, 07:58 PM it is because you use url link...change it to path for filesize, but then you can't do for the purpose of your script....
you can use either function
$lines= file($yourlinktofile);
foreach ($lines as $line) {
echo $line;
}
or change yours to: fread($yourlinktofile,1024);
..etc
UrlGuy 05-08-2005, 08:15 PM thx.. didnt quite understand the top code but heh..
I tried what I thought looked simplest...
I replaced
fread($fp, filesize($filename));
with
fread(http://domain.com/lic.txt,1024);
and now it just output the full content of lic.txt :S
orbitz 05-08-2005, 08:19 PM i thought you just a 1 line with a key..anyway,
use this:
fgets($fh, 4096);
it reads line by line
or the file($url) ...read all the lines of the file and save in the array lines, use foreach loop to get each line.
UrlGuy 05-08-2005, 08:26 PM Okay thanks alot m8.. again.. You've been very helpful to me :)
But.. ummm..
Should I leave $fh undecleared or is that where I should put my url or variable which lead to my url?
Which line of code should I replace with this?
Im just very newb.. pls bare with me :\
Thanks again for being helpful :)
orbitz 05-08-2005, 08:28 PM it's typo: use $fp, that's what you had in your script..
and how exactly does your text file looks like?
I normally go here:
php.net
enter the function name on the search box, it will give you results on the function with users' inputs
UrlGuy 05-08-2005, 08:41 PM WoW! Thanks man that worked!! yeuy!!
thanks agains for all your help!!
cool!!
I owe you man!!
=D
|