PHP - before page header and anything else

I now also came across PHP requirement. I need some PHP to be executed before anything else. So I tried to put it into the header using the options of my topic.

Unfortunately there is no setting to put the code before

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">

so I need to use an extern script to append the html to the PHP and then save as *.php. The PHP has to be executed so early, because only then I can modify the http headers.

So maybe this little option can be implemented.

(Of course it would be nice to be able to specify php as extension)

To change the extensions to php you can use a batch file:

  @ECHO  OFF
cd %1
del *.php
ren *.html *.php

Use at your own riskā€¦ as you will see the third line deletes all the php files in the folder.

You can see how to set it up here:

With regards to adding php to the top you could use notepad ++ find in file feature and search for

<!DOCTYPE html>

and replace with

<?php your php here ?>
<!DOCTYPE html>

Hope this helps

Thanks for the reply. Yes, as noted I am using a script already. In my case it is:

copy downloadphp.php upload\download.php
type upload\download.html >>upload\download.php

In my form I simply load download.php and all is good. I do not even have to change the navbar.

I first copy my PHP code into the upload directory and then append the original HTML to it using >>.

My suggestion was to add an option for this. The combo box already allows
ā€œBefore Headerā€ - it misses ā€œBefore HTMLā€ for this use.

The same as for the additional footer text ā€œAfter HTMLā€. It would allow a lot of extensions, especially when used globally.

I just checked out Version 6 - it is phantastic.

I checked the page settings. If I add something to ā€œBefore Contentā€ It still is exported here:

<!DOCTYPE html>*
<html lang="en">*

<head>*
    <meta charset="utf-8">*
BEFORE CONTENT

Although I know the work around, it would be really helpful to have an option
AT START OF FILE

to add scripting code such as

<?PHP before anything else is done. Thanks, Julian
1 Like

Overall i would be glad if we could see in the future some php integration inside bss, it would allow to do much more than only front end, but i wouldnā€™t expect it, but i have hopešŸ˜…

I still miss this functionality:

  • Add some text before any HTML code when exporting a page (at the first character!)
  • Add some text after all HTML code when exporting a text

Why I need this? Because when using the file as PHP script I cannot have any output before the PHP code since this automatically changes the HTTP response. Up to now I need to use a preprocessor to update the files which is not optimal to solve such an easy problem.

Ideally allow to load the before/after code from external file at the time when exporting it. That should also be very easy to do.

1 Like

I would propose the following. This would give us total freedom to add code anywhere on the page.

@martin Your thoughts?

2 Likes

This feature would be great to have!!

@martin Giving this a bump. Clearly other users would like the ability to add code before or after the HTML.

3 Likes

I tried with the latest build - I still miss this possibility.

I really need to create code before anything else, only there I can insert PHP code. After the <html> is already too late.

1 Like

I really hope this is added as a feature. I too need to add some PHP before html.

PHP extension too, but at least we have a workaround for that.

What would you all use php for?

Incorporating the CMS

It can also be used to add password protection to pages.

I really donā€™t see why the devs donā€™t allow it. They have no issues with us adding custom code inside the page HTML. Weā€™re simply asking for the ability to add it outside the page HTML.

1 Like

I use a Python Beautiful Soup py script to do this after export (about 4 seconds) Changes the href links to .php and saves as .php. Leaves the html files alone but I have a script to delete those also after completion.
Unique code for each html page would be different, I would not use a loop or possibly use definitions to match page characteristicsā€¦

import glob
import os.path
from bs4 import BeautifulSoup

# Modify main html pages and convert to php

dir_path = r"C:\MySites\mywebsite2"

for fpath in glob.glob(os.path.join(dir_path, "*.html")):
    with open(fpath) as html_file:

        soup = BeautifulSoup(html_file, 'html.parser')

        for a in soup.findAll('a'):
            a['href'] = a['href'].replace("html", "php")
            print (soup.a)

        new_dom_content = soup.prettify(formatter=None)

        html_number = os.path.basename(fpath).rsplit('.',1)[0]        
        with open(html_number + '.php', 'w') as outfile:

             outfile.write("<?php include('admin/runtime.php'); ?>\n")
             outfile.write("ANYTHING ELSE BEFORE <!DOCTYPE html>")
             outfile.write(new_dom_content)
             outfile.close()

@martin, @gabby Going to bump this post.

Even with the solution posted by @fotoarray it means that the publish to SFTP is redundant as the site needs to be exported for the script to run and then manually uploaded.

I know there would be issues with people trying to upload before html onto the bss server, but there could be a conditional that before will only work on publishing to SFTP.

2 Likes