PB Interpreter » Use PB instead of PHP or Perl for websites!

Developed or developing a new product in PureBasic? Tell the world about it.
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Post by djes »

DoubleDutch wrote:What happens if multiple people access the site at the same time? I mean while the compiler is busy compiling - will it not mess the system up?

If there are no problems, why should you keep compiling the source - why not just execute the compiled version?
It's what does the AND51's program, compiling only when necessary.

milan1612> Why couldn't we use linux executables, if they have the good permissions?
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

OOps, sorry - just saw the bit about the v1.1 version - any chance of seeing this versions source?
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Thank you for your great feedback! I'll answer you now:
Must admit that I am a little confused about the installation instructions?
It's very simple. Get PureBasic and extract it on your local PC. Then you take the source code of the PB Interpreter and compile it, in order to put it into the PB directory. This PB directory is to be uploaded 'as is' to your server. On my Linux server, I uploaded PureBasic to /pb, because then the path is short and my shebang-line will also be short (but on Linux, you can use every other directory, too as you can create SymLinks to achieve short path names).

After this upload process, you should have a look at the permissions; give the whole PB directory 755-rights (also applay these 755-rights to the interpreter, the PB compiler, etc...).
When Apache calls a CGI-Script, this CGI skript might run under another account name other than you used to upload PureBasic. Example: I logged into my FTP-Server with root-rights. The owner of the PB folder now is 'root'. In further times this was a problem, because Apache runs my CGI Scripts under the 'wwwrun' account (an account with limited rights). So be sure you give all user accounts access to the PB directory, or make the used useraccounts matching. I hope you understood this part? :oops:

Then go into your cgi-bin directory and create a file named "test.cgi". Type in the shebang-line containing the path to the interpreter. The rest is pure PureBasic: open a console, write a HTTP-header followed by any data you like. This could be plain text, HTML, whatever. Use the hello-world-example, if you don't know what to write.

Save this file and don't forget to give this file also 755-rights! Otherwise it cannot be executed by Apache (this is my most frequently mistake, when I wonder why nothing is working). :wink:
I compile your code to "interpreter.exe" and place it in my /pb installation folder - is that correct?
Err... Yes. :lol:

Remember to adjust the code where neccessary, because it was written for Linux. But there is not so much to do: Only change some paths (the / and \ thing).
However, have to be rock solid for security reasons, checking all input variables, and so on.
Actually, this has do be done in every language. In PureBasic there is no eval() function, to wich noobs can pass their input variables. :P

Also keep in mind that the executable can use commands like Get/SetCurrentDirectory(), ProgramFilename(), RunProgram(), etc. This enables you to read your current dir, get to know your actual program filename and to run other, external programs. As long as only you are using this, it might not be a problem. But don't forget that you can also use the OS's API or the commandline within your program.

Be also sure not to upload PB and the PB Interpreter into your httpdocs. This was my first attempt and it worked. But then I found out how to store PB outside of my webspace and now it works too.
I am still unsure whether I can upload the PB installation safely?
Try to access http://www.and51.de/purebasic , this is the directory to which I uploaded PureBasic the first time. Only noobs (like me) upload a complete PB installation to the webspace. That's why I'm going do delete it soon. But the point is that it is safe, because you can protect PureBasic from being downloaded via .htaccess for example:

Code: Select all

Order Allow,Deny
Deny from all
You won't need this .htacces, if you store PB somewhere outside your webspace on your server.
Actually I didn't think my hosting service would allow exe's to run in my cgi-bin folder; quite a surprise when it worked! I thought that was just for Perl scripts!
Well this often depends on what server type you have. Obviously, you do not have a simple webspace, but a (virtual) server. So you can run every services and programs. For example, I run TeamSpeak, TacticalOps and BOINC on my virtual server.

The second factor is Apache's configuration. PHP and Perl are so famous, that Apache is pre-configured by default to support PHP and Perl. Sometimes, you don't even need to type shebang-lines in Perl-Scripts, because Apache is so cifigured that it searches the PATH-variable to find the Perl-Interpreter. If you've got enough knowledge, you can also modify your Apache to find the PB Interpreter automatically.
This opens up some nice possibilities! Though I know others have been doing this for years!
Well, I got this idea when I wanted to write a "real" counter. A counter, not a hitcounter. So I must store the IP addresses and timestamps to check, if a visitor is there for the first time (then increase counter) or not. I got this working in Perl it was too %!$"(/%, although I love Perl. I wanted to use a language I use every day: No not German, but PureBasic! :D
It took me ~1 month to create the PB Interrpeter, but I didn't realized a simple counter yet. :lol: I was too busy with publishing it in the forums and play around with it.
@AND51 : have you checked with Fred that our PB license allows the compiler to be used in this way?
The license allows you to use PureBasic on every computer that you own. But only you may use PureBasic.

If you are still very unsure, you can also try this with the demo version of PureBasic, since it is free for every one. But then you get limited to 800-lines-only and an always-on-debugger.
I can't imagine you can do this with Linux binaries. Does anyone know if it's possible?
Of course, it is. The cgi-bin in Apache is given "ExecCGI" rights which advises Apache first, to execute the file if possible. If it's an executable, it's executed; if it's a script, it's sent to the specified interpeter.
Every directory hat you enable with "Options +ExecCGI" directive, will turn into a cgi-bin.
What happens if multiple people access the site at the same time?
No problem in version 1.0, no problem in version 1.1.

In v1.0 the PB Interpreter generates a unique filename for the executable. So it cannot be run twice. You can also call the PB Interpreter or the PB compiler itslef twice. This will result into 2 different processes which won't influence each other.

Version 1.1, however, compiles again only when neccessary. If you then call the executables twice, this is no problem, too. This also results in 2 independent processes.

Just be careful with the ressources, you use! Don't block an external file with OpenFile(), for example!
In Perl there is a command named flock(), acting like a kind of global mutex. In PureBasic you must find your own solution. But I plan to write those functions. Yesterday I read that flock() is a Linux-API, so obviously you can also use those functions in PB.

(flock is to prevent different processes from accessing the same file. If one process finishes, the next process gets access to the file. It works just like LockMutex() and TryLockMutex() commands in PB.)

However, it might take some time till I can present you a nice include providing those functions.
just saw the bit about the v1.1 version - any chance of seeing this versions source?
Yes, of course.
Since you gave me so nice feedback and expressed your interest, I'll release it, too. Just give me some time, please.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
blueb
Addict
Addict
Posts: 1111
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Post by blueb »

freak wrote:You shouldn't call it "interpreter". This is misleading.
Perhaps he could call it: "Ivory" to parody the American soap company's product that claims to be over 99%
Pure. :)

blueb
Last edited by blueb on Mon May 11, 2009 2:59 pm, edited 1 time in total.
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Thanks for the reply. :)

I understand now. My host server is using IIS 6 and so I need to be absolutely sure that I can protect the PB installation before I consider uploading it. I don't have access to a htaccess file and the usual Apache permissions are not appropriate so I am awaiting a reply from those who administer my account. I do have a protected folder within my virtual server, but want to be absolutely sure first! :)
I may look like a mule, but I'm not a complete ass.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

Wow, really cool project! *thumbs up*
I guess I will play around with it later some time. And maybe we can even write more commands for it, smart stuff which will make it more "PHP like".

Hmm, and about the name...
PBCWS? PureBasic Compiled Web Scripts :D
I like logic, hence I dislike humans but love computers.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

My host server is using IIS 6 nd so I need to be absolutely sure that I can protect the PB installation before I consider uploading it.
Well, that's understandable. I hope you get it working, since you have completely other circumstances (Apache/IIS 6, Linux/Windows). Good luck! :)
maybe we can even write more commands for it, smart stuff which will make it more "PHP like".
Yes, indeed. In Perl, there are many modules, which you can load and which do several tasks for you.
And also the issue with flock() would be nice to have.
Maybe we can create 2 includes: one for Windows and one for Linux.

One thing that can annoy you from the beginning on: double quotes. Imagine you want to print a hyperlink:

Code: Select all

Print("<a href="+Chr(34)+"http://www.and51.de""+Chr(34)+" target=""+Chr(34)+"_blank"+Chr(34)+"">click here</a>")
If somebody wants to avoid this, he must write his own pre-compiler, which he can put between PB Interpreter and compiler. But this wouldn't make sense, because the Interpreter does already open the file (when the shebang-line must be filtered). So you can also add your routine directly into the interpreter.

Perhaps he could call it: "Ivory" to parody the American soap company's product that claims to be over 99%
Pure.
Hmm, and about the name...
PBCWS? PureBasic Compiled Web Scripts
Hm... Well... All the time I'm talking of a so called "interpreter" (although it's literally not an interpreter). Do you really wish this project to be renamed?

I thought about something like "PureCGI", as it is the Common Gateway Interface (CGI) that is made use of here. But there are already several products (user libs, etc) having this name... PBCWS sounds a bit too complex or am I exaggerating? :lol: To be honest, I don't really got the point with "Ivory". I know that's a rare and precious material from elephants, but I don't know the "American sopar company's product"... What's that?
// edit:
Is this the ivory, you mean? :lol: http://www.ivory.com/
PB 4.30

Code: Select all

onErrorGoto(?Fred)
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

lol, I'm talking and talking and talking...
Almost forgot to append the PB Interpreter 1.1 :oops:

http://www.and51.de/pbinterpreter.zip

Contains:
  • PB Interpreter 1.1.pb
    Source code
  • interpreter
    binary for Linux
  • interpreter.exe
    executable for Windows
  • Readme.txt
    the usual, brief readme
I also wrote a changelog and posted it together with the current source code on page 1.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Post by djes »

blueb wrote:
freak wrote:You shouldn't call it "interpreter". This is misleading.
Perhaps he could call it: "Ivory" to parody the American soap company's product that claims to be over 99%
Pure. :)

blueb
<offtopic>
:lol: We had a discussion on the "BASIC" term that seems to be a problem to describe our quality language. "Ivory" for pure, what a foot in the racists asses, I love it! But as always, irony can be misinterpreted.
</offtopic>
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post by idle »

Wow cool, I've often thought about trying this but never got past the initial hmmm part! It opens up a whole host of possibilities.
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

I also wrote a changelog and posted it together with the current source code on page 1
Thanks. :)
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Post by J. Baker »

Hmm, I can't seem to get this to work with godaddy web hosting. Does anyone else use the deluxe godaddy web hosting?
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Well, is this just a kind of webspace or do you have SSH (or Telnet access or whatever)?
You need a dedicated or at least virtual server, which allows running own programs. But I assume, you understood this already. Personally, I don't know "godaddy", so I cannot give you THAT ultimate help. :?
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Post by J. Baker »

AND51 wrote:Well, is this just a kind of webspace or do you have SSH (or Telnet access or whatever)?
You need a dedicated or at least virtual server, which allows running own programs. But I assume, you understood this already. Personally, I don't know "godaddy", so I cannot give you THAT ultimate help. :?
I know it uses SSH but I'll contact godaddy and see if this is possible.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

www.DreamHost.com appear to give you all you need to do this with a regular shared account. They kill processes after a while though. They allow processes to run as long as you like if you also sign up the the PS (Private Server) system that they have.

To get a $50 discount, a free '.com' and also a 'Free IP for life' (very, very useful for things like SSL certs.), use the code 'PUREIP'.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Post Reply