Recent

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

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Using pascal programs as backend for PHP based webpages?
« on: December 15, 2019, 06:12:16 pm »
I have built a Raspberry Pi based data collection system and I have used FreePascal and Lazarus to program its control software running on the RPi.

This consists of a few console programs intended to be run by cron.
The data are managed inside an SQLite database (for which I have received good help on this forum!)

Now I want to build a configuration utility that will be web based (Apache2 on the RPi) and originally I thought that I would use PHP7 and straight HTML coding.
But that would mean to somehow re-create the database and other functions I have already developed in Pascal so it would work with PHP...
Not so fun.

Why web based?
Well, it makes it possible to use any computer with a web browser to configure the system when connected to the same network rather than logging on to the RPi itself, which is headless (no monitor or keyboard attached).


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?
--
Bo Berglund
Sweden

PaulRowntree

  • Full Member
  • ***
  • Posts: 132
    • Paul Rowntree
Re: Using pascal programs as backend for PHP based webpages?
« Reply #1 on: December 15, 2019, 06:22:43 pm »
Ignorant question coming ...Why do you need the PHP intermediate layer? 
Paul Rowntree
- coding for instrument control, data acquisition & analysis, CNC systems

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Using pascal programs as backend for PHP based webpages?
« Reply #2 on: December 15, 2019, 06:23:29 pm »
I'd suggest https://wiki.freepascal.org/CGI_Web_Programming and related pages.

I've just set up a very simple webpage on a server run by our ISP which will eventually be a portal to various backend stuff- probably in VMs. They don't support PHP etc but do support CGI, and I wrote a simple script in Perl which expands SSIs in a page passed as the parameter and emits the result.

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 #3 on: December 15, 2019, 06:30:42 pm »
You don't need to use PHP to make web apps. Just use Pascal directly, either as CGI, FastCGI, or Apache module. Free Pascal provides fpWeb framework for everything you need to make web apps. I've made lots of web apps using FPC since like 15 years ago. I've never need PHP or nodeJS or any non-Pascal language for that. Now it's even better, Free Pascal has a sister project named pas2js that convert pascal program into javascript. So, you can even write the front-end side using pascal as well. Isn't that awesome? 😊
-Bee-

A long time pascal lover.

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: Using pascal programs as backend for PHP based webpages?
« Reply #4 on: December 15, 2019, 06:58:52 pm »
Ignorant question coming ...Why do you need the PHP intermediate layer?
Because that is the only way I (kind of) know how to use for programming stuff on webpages...
There is a way to call an external executable on the webserver from PHP and somehow retrieve the result, I guess. Since I have written a number of related utilities in pascal and compiled for Raspbian they are available to be called. Only I don't really know how to extract the result from them...
--
Bo Berglund
Sweden

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: Using pascal programs as backend for PHP based webpages?
« Reply #5 on: December 15, 2019, 07:00:19 pm »
I'd suggest https://wiki.freepascal.org/CGI_Web_Programming and related pages.

I've just set up a very simple webpage on a server run by our ISP which will eventually be a portal to various backend stuff- probably in VMs. They don't support PHP etc but do support CGI, and I wrote a simple script in Perl which expands SSIs in a page passed as the parameter and emits the result.

MarkMLl
Hi Mark,
I will have a look at that documentation.
Thanks.
--
Bo Berglund
Sweden

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: Using pascal programs as backend for PHP based webpages?
« Reply #6 on: December 15, 2019, 07:03:49 pm »
You don't need to use PHP to make web apps. Just use Pascal directly, either as CGI, FastCGI, or Apache module. Free Pascal provides fpWeb framework for everything you need to make web apps. I've made lots of web apps using FPC since like 15 years ago. I've never need PHP or nodeJS or any non-Pascal language for that. Now it's even better, Free Pascal has a sister project named pas2js that convert pascal program into javascript. So, you can even write the front-end side using pascal as well. Isn't that awesome? 😊
First off, I am an old hand so I do not know much about "webapps", what are these?

Do you mean one can use the Lazarus desktop programming and somehow show the programs through a web server?
Regarding Javascript I am totally unfamiliar with Java so I'd rather stay away from that. Started using Pascal back in the 1980-es, though.
--
Bo Berglund
Sweden

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Using pascal programs as backend for PHP based webpages?
« Reply #7 on: December 15, 2019, 07:06:13 pm »
Bo, first step: find out what scripting etc. facilities are supported by your webserver. Second step: do not under any circumstances be tempted to write your own custom webserver.

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

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Using pascal programs as backend for PHP based webpages?
« Reply #8 on: December 15, 2019, 08:14:32 pm »
Regarding Javascript I am totally unfamiliar with Java so I'd rather stay away from that. Started using Pascal back in the 1980-es, though.

Javascript is not Java.

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: Using pascal programs as backend for PHP based webpages?
« Reply #9 on: December 15, 2019, 09:20:20 pm »
I now looked (briefly) at the links provided and it seems like a webapp is more or less an invisible processing system. The examples I looked at looked mostly like "Hello world" type programs operating in a console.

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.

In the current case I want to check the validity of the data and store in the database or maybe trigger some server side action.
Also on request from the user retrieve result data as files or display. This would be PHP code calling pascal programs for retrieval and then rendering the result into a GUI form sent back to the user.

Rather than programming these database and file system operations in PHP my thought was to use PHP to extract the input data from the user (form submission) and then repackage these into a call to my pascal program that does the real work towards the database and hardware....
--
Bo Berglund
Sweden

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Using pascal programs as backend for PHP based webpages?
« Reply #10 on: December 15, 2019, 09:31:09 pm »
As I said, you start off by finding out what facilities your web server (Apache?) is configured to provide. It might even have PHP.

Any HTML page- without Javascript and potentially without CSS- has a basic set of interactive data entry components. Even without PHP etc., CSS allows you to make components visible/invisible depending on what else is going on. Javascript is vastly overused in my opinion, mainly by addons which benefit their writer more than the user (e.g. targetted advertising under the control of Google et al.). But detailed discussion is way off topic.

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

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Using pascal programs as backend for PHP based webpages?
« Reply #11 on: December 15, 2019, 09:36:18 pm »
Rather than programming these database and file system operations in PHP my thought was to use PHP to extract the input data from the user (form submission) and then repackage these into a call to my pascal program that does the real work towards the database and hardware....

It depends heavily on what you're currently doing (if anything) and what your final goal is but if I were you I would forget about PHP and let my Pascal program manage everything. That way you can avoid most (if not all) problems of interoperabilrity between the two programs.

Whether the backend is CGI/FastCGI, an Apache module or whatever I'm fairly sure it can be done in Pascal without more than the normal problems.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: Using pascal programs as backend for PHP based webpages?
« Reply #12 on: December 15, 2019, 10:30:34 pm »
As I said, you start off by finding out what facilities your web server (Apache?) is configured to provide. It might even have PHP.
As I think I have mentioned but possibly in another thread the target for my system is a Raspberry Pi running Raspbian Buster and with Apache2 + PHP etc installed. Not MySql though, I decided against its complications and am using an SQLite database instead.
The web interface is not a user interface as such, it is just a front end to set up (configure) the system and define the automatic tasks to execute. In order to use it one has to be physically connected to the RPi network.
Quote
Any HTML page- without Javascript and potentially without CSS- has a basic set of interactive data entry components. Even without PHP etc., CSS allows you to make components visible/invisible depending on what else is going on.
Yes, I have designed a prototype webpage in php lingo that shows the boxes, lists and selectors I need. It works ok right now but it does not do anything of course.

I think I will just use the PHP exec() function with my own pascal programs and get it done....
Of course I will have to create the php webpages also, but it should not be a big deal.
And I already know the way to extract the posted data from the form so I can stuff it onto a call to my pascal program.
Will do some experimentation shortly.
--
Bo Berglund
Sweden

bee

  • Sr. Member
  • ****
  • Posts: 393
Re: Using pascal programs as backend for PHP based webpages?
« Reply #13 on: December 16, 2019, 02:50:28 am »
It seems Bosse doesn't have proper knowledge about web apps nor web pages. 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.

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.

Basically, a web app starts from a browser request to your web server. Your web server should know the URL requested is handled by other application. Your web server forward the request to the application handler which is your pascal program. Your program then reads the input given by your web server (using either CGI, FastCGI, or Apache module). 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.
« Last Edit: December 16, 2019, 03:02:38 am by bee »
-Bee-

A long time pascal lover.

bee

  • Sr. Member
  • ****
  • Posts: 393
Re: Using pascal programs as backend for PHP based webpages?
« Reply #14 on: December 16, 2019, 02:55:00 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.
« Last Edit: December 16, 2019, 02:59:59 am by bee »
-Bee-

A long time pascal lover.

 

TinyPortal © 2005-2018