Recent

Author Topic: Using pascal programs as backend for PHP based webpages?  (Read 12897 times)

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Using pascal programs as backend for PHP based webpages?
« Reply #15 on: December 16, 2019, 06:03:43 am »
Along with the tutorial, I also made a WebCRT unit to demonstrate how easy it's to write web apps using Pascal and the unit. It's almost as easy as using the standard CRT unit.

For more control but with more "manual" way, you may use my webUtils unit, like to handle sessions and cookies. The repo contains the codes of my web app tutorial. I think you can learn something from them although you don't read the tutorial.

Hope it will help.
Impressive. Thanks for sharing.

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Using pascal programs as backend for PHP based webpages?
« Reply #16 on: December 16, 2019, 08:59:23 am »
You can simply add JsonRPC to your application and expose some of it's functionality to 3rd parties. If you browse fpc sources you will find some examples. There is also CeosMW JSONRPC which can be downloaded via OPM or through https://github.com/jbsolucoes/ceosmw. CeosMW components make this task very easy.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Using pascal programs as backend for PHP based webpages?
« Reply #17 on: December 16, 2019, 09:05:29 am »
But I need a GUI configuration form where I can enter or select values and then submit them to a web server handler.
For me this has always been a php file receiving the data and then doing something with them.
PHP essentially boils down to reacting to a HTTP request (e.g. GET index.php) by sending a HTTP response to the client which (in most cases) will simply be HTML text. That is nothing special of PHP and can just as easily be down with FPC (or any other language in fact).

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Using pascal programs as backend for PHP based webpages?
« Reply #18 on: December 16, 2019, 09:19:25 am »
Some people think that the application part (the program that you wrote to handle web server request) can only be written in PHP. It's very wrong. You can even write it in bash script or batch file. There's nothing that PHP can do and other language can't. My first web app was written in Turbo Pasal 5.5.

The crucially important thing is that the web server should have a mechanism to pass a request for certain content to custom code. That custom code could be PHP, SSI (in the original page), CGI (a script in a separate directory), or a server-specific mechanism to improve the efficiency of any of those (e.g. Apache's ModPerl, which eliminates the overhead of a load/compile every time a script is run).

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: Using pascal programs as backend for PHP based webpages?
« Reply #19 on: December 16, 2019, 11:36:03 am »
It seems Bosse doesn't have proper knowledge about web apps nor web pages.
You are quite right about that concerning "Web apps"! It is the main reason I am asking here...
Web pages in general I do know, I have written sites since the 1990:es using just a text editor...
Quote

I wrote a tutorial about that, but unfortunately it's written in Bahasa Indonesia not English, because it's for my fellow Pascal programmers in Indonesia. The tutorial explains from the very basic of HTTP, HTML, CSS, JavaScript, scripting, web server features (based on Apache), communication between web server and our program (in Pascal), etc.
Maybe I can try sending it through Google Translate?

Quote
PHP is just ONE of MANY ways to make a web apps. There are many other better ways than using PHP for web apps. Web apps are like desktop app but using web interface (instead of GUI or CLI). So, instead using OS GUI APIs to display things to user, it's using HTML and CSS and using Javascript to make to do the logic (on the browser side). Oh, Javascript is NOT Java.
I looked at your example and realized that:
1) One can use a straight pascal cli program to handle the POST or GET call.
2) One just has to place the program into the cgi-bin dir of the webserver and set its name as the form handler
3) One builds the response page as html in pascal code and put return variable values into that then writes it out as a writeln

So I should be able to do this in my command line programs written with Lazarus!

Quote
The easiest way is using CGI since all you need to do is read input from standard input and send output to standard output. Your program response to your web server request by sending output in HTML document along with the required CSS and Javascript code (either internal or external to the HTML document). Your web server reads your response and forward it to the browser that requested at the beginning. That's what happened in every single request and response of HTTP communication.

Some people think that the application part (the program that you wrote to handle web server request) can only be written in PHP. It's very wrong. You can even write it in bash script or batch file. There's nothing that PHP can do and other language can't. My first web app was written in Turbo Pasal 5.5.

Very helpful response! Thank you very much!

I have a few clarification questions:

1) Displaying the main form using a pascal program?
Since I am using web forms for my configuration and status pages it would be better if I can synchronize the web form and processing inside the pascal program by having the actual page display also controlled by the pascal code. One less thing to get out of sync....
So can I display my config using an url such as this:
Code: [Select]
http://rpimonitor/cgi-bin/mainpageWhere mainpage is my pascal program for showing the main page...
Or maybe cgi-bin is not directly available on the browser (I think it is typically located above the document root) so I may need an index.php file on the server that calls the cgi-bin script? If so how would that be done?

2) Form handler type GET and POST?
The two types I know of are POST and GET where the submit button will in the GET case put the input data into the URL itself whereas in the POST case the data is sent in another way, which I don't know how...
When dealing with form handlers there are different ways to deal with that in PHP, how does it work in cgi pascal programs?
Say I use the POST method in the form, how do the data arrive into the pascal cmd line program?

Grateful for your input!"
--
Bo Berglund
Sweden

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Using pascal programs as backend for PHP based webpages?
« Reply #20 on: December 16, 2019, 11:54:04 am »
I looked at your example and realized that:
1) One can use a straight pascal cli program to handle the POST or GET call.
2) One just has to place the program into the cgi-bin dir of the webserver and set its name as the form handler
3) One builds the response page as html in pascal code and put return variable values into that then writes it out as a writeln

So I should be able to do this in my command line programs written with Lazarus!

That's right. But you'll need to look at the CGI environment variables that are made available to your script, and different servers make different one available.

> Or maybe cgi-bin is not directly available on the browser (I think it is
> typically located above the document root) so I may need an index.php
> file on the server that calls the cgi-bin script? If so how would that be done?

Note that cgi-bin is entirely a server-side thing. On the browser you've got Javascript (disabled to varying extents by security addons), and very rarely you might come across Java, ActiveX and so on.

> 2) Form handler type GET and POST?

One pipes the submission, the other encodes it in the URI. There have also been various server-specific methods in the past, e.g. PATCH, but those are rarely implemented these days.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: Using pascal programs as backend for PHP based webpages?
« Reply #21 on: December 16, 2019, 12:32:03 pm »
I found this Apache tutorial concerning CGI and its various aspects...

So I think I am on the right way now.  :D
I believe that my compiled pascal program will work if it is placed in a dir, which has been marked in the apache conf file using the directive ScriptAlias. I will check that as soon as I have an example program to test with.


Seems like the input data are available like this:
POST:
on STDIN as a string like name=value&name2=value&name3=value

GET:
in an environment variable QUERY_STRINGS in the same format as above
--
Bo Berglund
Sweden

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Using pascal programs as backend for PHP based webpages?
« Reply #22 on: December 16, 2019, 12:46:27 pm »
On a standard Debian system I found that Apache was configured to expect CGIs in /usr/lib/cgi-bin and that provided I set up a symlink it worked fine; I'd be surprised if Raspbian were different. I ended up with /var/www/html/cgi-bin which isn't quite the classic layout but if it works...

BE TOLD: treat QUERY_STRINGS and the rest with caution. I think that one's fairly universally implemented, but there are others relating to referring document etc. which you'll need and are somewhat variable.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

bee

  • Sr. Member
  • ****
  • Posts: 393
Re: Using pascal programs as backend for PHP based webpages?
« Reply #23 on: December 16, 2019, 05:16:19 pm »
Maybe I can try sending it through Google Translate?
If you're so interested, you may get my free mini ebook about web programming using (Free)Pascal here (it's a 2.2 MB PDF file). And the code of the examples of the book are here.

I looked at your example and realized that:
1) One can use a straight pascal cli program to handle the POST or GET call.
2) One just has to place the program into the cgi-bin dir of the webserver and set its name as the form handler
3) One builds the response page as html in pascal code and put return variable values into that then writes it out as a writeln

So I should be able to do this in my command line programs written with Lazarus!
Indeed! :)

Very helpful response! Thank you very much!
You're welcome. I'm glad to be able to help you.

I have a few clarification questions:

1) Displaying the main form using a pascal program?
Since I am using web forms for my configuration and status pages it would be better if I can synchronize the web form and processing inside the pascal program by having the actual page display also controlled by the pascal code. One less thing to get out of sync....
So can I display my config using an url such as this:
http://rpimonitor/cgi-bin/mainpage
Where mainpage is my pascal program for showing the main page…
The main page of your program is the output of the program itself. If your mainpage[.cgi] produce correct HTML document along with the required CSS and JS files, then the client browser will render it to the user like a normal web page.

Or maybe cgi-bin is not directly available on the browser (I think it is typically located above the document root) so I may need an index.php file on the server that calls the cgi-bin script? If so how would that be done?
The cgi-bin folder is configurable. It's the folder that specially set for F/CGI binary files, by default. Of course you may change it to any folder. Just study the Apache setting for executable binary files.

2) Form handler type GET and POST?
The two types I know of are POST and GET where the submit button will in the GET case put the input data into the URL itself whereas in the POST case the data is sent in another way, which I don't know how…

When dealing with form handlers there are different ways to deal with that in PHP, how does it work in cgi pascal programs?
Say I use the POST method in the form, how do the data arrive into the pascal cmd line program?
Very simple actually. Every GET variables and its value will be stored in an environment variable named QUERY_STRING, you can use the GetEnvironmentVariable() function to read the content. And POST variables and its values streamed into standard input, you simply use the standard readln procedure to get the content.

Or, you better study my tutorial and read the examples. Good luck! :)
« Last Edit: December 16, 2019, 05:37:48 pm by bee »
-Bee-

A long time pascal lover.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Using pascal programs as backend for PHP based webpages?
« Reply #24 on: December 16, 2019, 06:30:55 pm »
The main page of your program is the output of the program itself. If your mainpage[.cgi] produce correct HTML document along with the required CSS and JS files, then the client browser will render it to the user like a normal web page.

Or the main page can be a frameset, with at least some of the constituent frames being generated by CGI. That has the advantage that the scripts can look at the referrer and have some degree of confidence that they're not being fed malicious parameters, rather than letting a potentially-untrusted user drive the scripts directly.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

zamronypj

  • Full Member
  • ***
  • Posts: 133
    • Fano Framework, Free Pascal web application framework
Re: Using pascal programs as backend for PHP based webpages?
« Reply #25 on: December 17, 2019, 06:57:32 am »

Question:
Is there some simple way to call processing functions written in Pascal from within PHP such that it can retrieve data to display to the user or execute commands and feed back results?

All I really want is read and write to the SQLite database and also scan a few directories for certain files, list and down/upload these.

Has someone here been there and done that and is willing to share the experience?

mixing Pascal with PHP is actually hard.

  • You need to export functionality written in Pascal as library (as shared object *.so).
  • create PHP extension so that library function can be invoked from PHP code.
  • Enable that extension

Writing PHP extension requires you to use C language or Zephir language. I do not know if Free Pascal can be used to write PHP extension. (maybe Free Pascal can but, AFAIK, no PHP headers are converted yet)

So the simplest way is just to use Pascal or PHP. PHP can access SQLite database as well as Free Pascal.
« Last Edit: December 17, 2019, 07:06:18 am by zamronypj »
Fano Framework, Free Pascal web application framework https://fanoframework.github.io
Apache module executes Pascal program like scripting language https://zamronypj.github.io/mod_pascal/
Github https://github.com/zamronypj

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Using pascal programs as backend for PHP based webpages?
« Reply #26 on: December 17, 2019, 07:47:48 am »
Once you have web-server program, then you may use Lazarus program (I mean Windows visual program if the terminals are running on Windows --- I do not know about Linux) using Synapse... You don't have to bother write HTML (and CSS, JS, etc.).

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Using pascal programs as backend for PHP based webpages?
« Reply #27 on: December 17, 2019, 08:31:41 am »
mixing Pascal with PHP is actually hard.
Not if you use JsonRPC on Pascal side (like CeosMW), and JsonRPC on PHP side (like https://github.com/datto/php-json-rpc).
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Using pascal programs as backend for PHP based webpages?
« Reply #28 on: December 17, 2019, 09:23:18 am »
I looked at your example and realized that:
1) One can use a straight pascal cli program to handle the POST or GET call.
2) One just has to place the program into the cgi-bin dir of the webserver and set its name as the form handler
3) One builds the response page as html in pascal code and put return variable values into that then writes it out as a writeln

So I should be able to do this in my command line programs written with Lazarus!
You can also take a look at fpWeb which is part of FPC and Lazarus and would deal with all the background stuff of request variables, headers, etc. You can find an example for CGI here. Please note that you do not need to use Lazarus for this. You can just as well use fpWeb inside a "command line" application.

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: Using pascal programs as backend for PHP based webpages?
« Reply #29 on: December 17, 2019, 11:34:20 am »
I looked at your example and realized that:
1) One can use a straight pascal cli program to handle the POST or GET call.
2) One just has to place the program into the cgi-bin dir of the webserver and set its name as the form handler
3) One builds the response page as html in pascal code and put return variable values into that then writes it out as a writeln

So I should be able to do this in my command line programs written with Lazarus!
You can also take a look at fpWeb which is part of FPC and Lazarus and would deal with all the background stuff of request variables, headers, etc. You can find an example for CGI here. Please note that you do not need to use Lazarus for this. You can just as well use fpWeb inside a "command line" application.
When I am referring to Lazarus I am just indicating my use of it as a development IDE.
The actual programs are just non-visual command line programs communicationg via start arguments, environment variables and STDIO interchanges. Meant to run on an RPi without anyone logged on and no display or keyboard attached.
Also the RPi will be interfaced this way only for configuration by a single user and once that is done it will be on its own...
And I have scrapped the idea of using PHP in favour of directly calling the pascal programs from Apache.
So the subject of this therad is now a bit misleading....
--
Bo Berglund
Sweden

 

TinyPortal © 2005-2018