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...
I'd suggest https://wiki.freepascal.org/CGI_Web_Programming (https://wiki.freepascal.org/CGI_Web_Programming) and related pages.Hi Mark,
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
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 (https://wiki.freepascal.org/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?
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.
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....
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.
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.
Along with the tutorial, I also made a WebCRT (https://github.com/pakLebah/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.Impressive. Thanks for sharing.
For more control but with more "manual" way, you may use my webUtils (https://github.com/pakLebah/dasarweb) 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.
But I need a GUI configuration form where I can enter or select values and then submit them to a web server handler.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).
For me this has always been a php file receiving the data and then doing something with them.
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.
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...
Maybe I can try sending it through Google Translate?
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.I looked at your example and realized that:
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.
http://rpimonitor/cgi-bin/mainpage
Where mainpage is my pascal program for showing the main page...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!
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 (https://pak.lebah.web.id/ebook/pascal.id_kulgram2.pdf) (it's a 2.2 MB PDF file). And the code of the examples of the book are here (https://github.com/pakLebah/dasarweb).
I looked at your example and realized that:Indeed! :)
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!
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: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.
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…
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?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.
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?
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.
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.Not if you use JsonRPC on Pascal side (like CeosMW), and JsonRPC on PHP side (like https://github.com/datto/php-json-rpc).
I looked at your example and realized that: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 (https://wiki.freepascal.org/fcl-web). Please note that you do not need to use Lazarus for this. You can just as well use fpWeb inside a "command line" application.
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!
When I am referring to Lazarus I am just indicating my use of it as a development IDE.I looked at your example and realized that: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 (https://wiki.freepascal.org/fcl-web). Please note that you do not need to use Lazarus for this. You can just as well use fpWeb inside a "command line" application.
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!
[Tue Dec 17 19:18:04.837217 2019] [cgi:error] [pid 24911] [client 192.168.119.166:58361] malformed header from script 'getwebpage': Bad header: <!DOCTYPE html public >
Content-type: text/html
<!DOCTYPE html public >
<html>
<head>
<title>Monitoring Config</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
... long style section, then:
table, td, th {
border: 1px solid black;
}
</style>
</head>
<body>
<p>Got Data:<br>namn=Olle<br>trafik=hemsk<br>Test=7</p>
</body></html>
Content-type: text/html; charset=ISO-8859-1
<html>
<head>
</head>
<body>
</body>
</html>
You should be sending out quite a lot more headers than just Content-Type (for example, Content-Lenght, IIRC).
But when I call this from a web browser via Apache CGI handling I am always getting error 500 and the Apache error log says this:Code: [Select][Tue Dec 17 19:18:04.837217 2019] [cgi:error] [pid 24911] [client 192.168.119.166:58361] malformed header from script 'getwebpage': Bad header: <!DOCTYPE html public >
The data sent out from the program looks like this:Code: [Select]Content-type: text/html
<!DOCTYPE html public >
<html>
<head>
<title>Monitoring Config</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
... long style section, then:
table, td, th {
border: 1px solid black;
}
</style>
</head>
<body>
<p>Got Data:<br>namn=Olle<br>trafik=hemsk<br>Test=7</p>
</body></html>
I have got it working now but I sure do not know why it does (or why it didn't before)....Glad to read you made it worked.
Then I had a fresh Lazarus and could rebuild the IDE with my own selection of extra packages plus fpWeb.This is my personal opinion, take it with extra grain of salt. I think fpWeb is great and all. But for simple web apps, it's overkill. I've made lots of simple web apps with 1-5 forms to serve less than 100 users. For that purpose, a simple and straight-forward unit like webCRT or webUtils is enough. The executable file size is a lot smaller too which is pretty important for CGI app (faster loading).
This is my personal opinion, take it with extra grain of salt. I think fpWeb is great and all. But for simple web apps, it's overkill. I've made lots of simple web apps with 1-5 forms to serve less than 100 users. For that purpose, a simple and straight-forward unit like webCRT or webUtils is enough. The executable file size is a lot smaller too which is pretty important for CGI app (faster loading).My current usage is for exactly 1 user... ;D
Unless you need to make a huge and complex web apps, fpWeb is more appropriate. It provides you with everything you need. It also scales very well. It's even better if you made your fpWeb app as FastCGI that responses instantly to Apache requests so it could handle lots more loads. It's like the desktop-class framework for web app, while unit like webCRT or webUtils is like a simple library for console or command line program in the DOS era.I installed the fpWeb package but then my original design started working so I have not looked further into it.
Well, your mile may vary of course. Just share a thought of mind. :)I have a very simple use case but I want to avoid all hardware like displays, keyboards and the like. That is why I zoomed in on using a web based system.
My current usage is for exactly 1 user... ;DWell… that's a perfect case for my webCRT or webUtils unit. I also have an SQLite tutorial. But again, it's in Bahasa Indonesia.
To configure the RaspberryPi system before deployment and for navigating recorded data. All inside an SQLite database.
Speed is not a real issue.
Now I have downloaded the two units from GitHub and also translated webUtils comments to English from MalayanIt's actually Bahasa Indonesia, not Malayan. But it doesn't matter, for english speaker, both are pretty much the same anyway.
Using Google Translate, of course. It is pretty good at this…
From the wiki I found I can create a CGI application (which is what this is all about) but when I tested it all I see is a very sparse main application with an initialization section for a web oriented object…Yes, fpWeb has lots of boilerplate. It's too complex for a simple web app. That's why I made webCRT unit. If you want more nifty control over the protocol, you could use the webUtils unit.
I have a very simple use case but I want to avoid all hardware like displays, keyboards and the like. That is why I zoomed in on using a web based system.Yes, that's the advantage of web application.
Along with the tutorial, I also made a WebCRT (https://github.com/pakLebah/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.Thanks Bee!
For more control but with more "manual" way, you may use my webUtils (https://github.com/pakLebah/dasarweb) 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.
This is my personal opinion, take it with extra grain of salt. I think fpWeb is great and all. But for simple web apps, it's overkill. I've made lots of simple web apps with 1-5 forms to serve less than 100 users. For that purpose, a simple and straight-forward unit like webCRT or webUtils is enough. The executable file size is a lot smaller too which is pretty important for CGI app (faster loading).My current usage is for exactly 1 user... ;D
To configure the RaspberryPi system before deployment and for navigating recorded data. All inside an SQLite database.
Speed is not a real issue.
Where do I find the webCRT and webUtils units?
EDIT:
Sorry, I found them both as links to GitHub in reply #14 on this thread by you....
EDIT2:
Now I have downloaded the two units from GitHub and also translated webUtils comments to English from Malayan
Using Google Translate, of course. It is pretty good at this...QuoteUnless you need to make a huge and complex web apps, fpWeb is more appropriate. It provides you with everything you need. It also scales very well. It's even better if you made your fpWeb app as FastCGI that responses instantly to Apache requests so it could handle lots more loads. It's like the desktop-class framework for web app, while unit like webCRT or webUtils is like a simple library for console or command line program in the DOS era.I installed the fpWeb package but then my original design started working so I have not looked further into it.
What does it give me when the simple CL program now works OK?
From the wiki I found I can create a CGI application (which is what this is all about) but when I tested it all I see is a very sparse main application with an initialization section for a web oriented object...QuoteWell, your mile may vary of course. Just share a thought of mind. :)I have a very simple use case but I want to avoid all hardware like displays, keyboards and the like. That is why I zoomed in on using a web based system.
The RPi will be set up as a WiFi Access Point to which you can connect your phone or tablet and then use the web browser to contact the system for config and results browsing. The RPi will have no Internet connection so you are the only user on the system.
I will make sure there is an index file that redirects to the web app so the proper pages will be shown.