PHP script as single binary - php

I was whatching facebook's development steps and getting codes live , i think they are making their script as a single binary and pushing a single file. We got a portal with alot of files inside which coded with PHP . is there any encoder or compiler can bundle all files in single one ?
While we are pushing our codes to our servers when renaming folder or extracting files and override , it starts to give error spam.
Also any solution or approach to send the webpages alive without getting any error are welcome.
thanks

You probably need PHAR

FaceBook use a tool named HipHop which compiles PHP into a single binary executable.

For Windows, Linux and FreeBSD there's Roadsend PHP Compiler. It truly compiles PHP source code into native binary format. No interpreter is required.
They claim: "Roadsend PHP is a free, open source implementation of the PHP language. It includes a compiler that produces native binaries (no interpreter required). Roadsend Compiler can build online web applications with Fast/CGI, offline web applications with an embedded web server (MicroServer), and console applications."
They say: "Applications compiled with the Roadsend Compiler do not require Zend or any other interpreter to execute. Roadsend is not affiliated with Zend or the PHP Group and does not share (nor is it derived from) the same code base as their PHP implementation. Roadsend PHP is an entirely new implementation of PHP that is syntactically and semantically compatible with PHP Group's PHP 5.2.x and the Zend Engine 2." – Sebastián Grignoli 41 secs ago edit
"How do I know that my application won't be decompiled back to the PHP source file?"
"The compiling of a PHP application with the Roadsend Compiler is much the same as compiling a C file to binary format. The program goes through a process that converts the plain text source into a binary format that is suitable for the processor to execute, and which is extremely hard to decompile back to any sort of high level programming language."

Related

Is it possible to release PHP web application as an desktop application? [duplicate]

I want to automate a fairly simple task. For this I have written a small PHP script which I run from the command line using PHP-CLI. Now I want to hand over this script to someone but I do not want to:
give away the source code
ask him to install PHP on the system
Is there a way to create a .exe version of the PHP script. I am really not much worried about de-compilation; I am more worried about asking people to install and configure PHP.
Peachpie
http://www.peachpie.io
https://github.com/iolevel/peachpie
Peachpie is PHP 7 compiler based on Roslyn by Microsoft and drawing from popular Phalanger. It allows PHP to be executed within the .NET/.NETCore by compiling the PHP code to pure MSIL.
Phalanger
http://v4.php-compiler.net/
http://wiki.php-compiler.net/Phalanger_Wiki
https://github.com/devsense/phalanger
Phalanger is a project which was started at Charles University in Prague and was supported by Microsoft. It compiles source code written in the PHP scripting language into CIL (Common Intermediate Language) byte-code. It handles the beginning of a compiling process which is completed by the JIT compiler component of the .NET Framework. It does not address native code generation nor optimization. Its purpose is to compile PHP scripts into .NET assemblies, logical units containing CIL code and meta-data.
Bambalam
https://github.com/xZero707/Bamcompile/
Bambalam PHP EXE Compiler/Embedder is a free command line tool to convert PHP applications to standalone Windows .exe applications. The exe files produced are totally standalone, no need for php dlls etc. The php code is encoded using the Turck MMCache Encode library so it's a perfect solution if you want to distribute your application while protecting your source code. The converter is also suitable for producing .exe files for windowed PHP applications (created using for example the WinBinder library). It's also good for making stand-alone PHP Socket servers/clients (using the php_sockets.dll extension).
It's NOT really a compiler in the sense that it doesn't produce native machine code from PHP sources, but it works!
ZZEE PHPExe
http://www.zzee.com/phpexe/
ZZEE PHPExe compiles PHP, HTML, Javascript, Flash and other web files into Windows GUI exes. You can rapidly develop Windows GUI applications by employing the familiar PHP web paradigm. You can use the same code for online and Windows applications with little or no modification. It is a Commercial product.
phc-win
http://wiki.swiftlytilting.com/Phc-win
The PHP extension bcompiler is used to compile PHP script code into PHP bytecode. This bytecode can be included just like any php file as long as the bcompiler extension is loaded. Once all the bytecode files have been created, a modified Embeder is used to pack all of the project files into the program exe.
Requires
php5ts.dll
php_win32std.dll
php_bcompiler.dll
php-embed.ini
ExeOutput
http://www.exeoutput.com/
Commercial
WinBinder
http://winbinder.org/ - no longer available
(original: https://github.com/stefan-loewe/WinBinder)
(fork: https://github.com/wagy/WinBinder)
WinBinder is an open source extension to PHP, the script programming language. It allows PHP programmers to easily build native Windows applications, producing quick and rewarding results with minimum effort. Even short scripts with a few dozen lines can generate a useful program, thanks to the power and flexibility of PHP.
PHPDesktop
https://github.com/cztomczak/phpdesktop
PHP Desktop is an open source project founded by Czarek Tomczak in 2012 to provide a way for developing native desktop applications using web technologies such as PHP, HTML5, JavaScript & SQLite. This project is more than just a PHP to EXE compiler, it embeds a web-browser (Internet Explorer or Chrome embedded), a Mongoose web-server and a PHP interpreter. The development workflow you are used to remains the same, the step of turning an existing website into a desktop application is basically a matter of copying it to "www/" directory. Using SQLite database is optional, you could embed mysql/postgresql database in application's installer.
PHP Nightrain
https://github.com/kjellberg/nightrain
Using PHP Nightrain you will be able to deploy and run HTML, CSS, JavaScript and PHP web applications as a native desktop application on Windows, Mac and the Linux operating systems. Popular PHP Frameworks (e.g. CakePHP, Laravel, Drupal, etc…) are well supported!
phc-win "fork"
https://github.com/RDashINC/phc-win
A more-or-less forked version of phc-win, it uses the same techniques as phc-win but supports almost all modern PHP versions. (5.3, 5.4, 5.5, 5.6, etc) It also can use Enigma VB to combine the php5ts.dll with your exe, aswell as UPX compress it. Lastly, it has win32std and winbinder compilied statically into PHP.
EDIT
Another option is to use
http://www.appcelerator.com/products/titanium-cross-platform-application-development/
an online compiler that can build executables for a number of different platforms, from a number of different languages including PHP
TideSDK
http://www.tidesdk.org/
TideSDK is actually the renamed Titanium Desktop project. Titanium remained focused on mobile, and abandoned the desktop version, which was taken over by some people who have open sourced it and dubbed it TideSDK.
Generally, TideSDK uses HTML, CSS and JS to render applications, but it supports scripted languages like PHP, as a plug-in module, as well as other scripting languages like Python and Ruby.
I had problems with most of the tools in other answers as they are all very outdated.
If you need a solution that will "just work", pack a bare-bones version of php with your project in a WinRar SFX archive, set it to extract everything to a temporary directory and execute php your_script.php.
To run a basic script, the only files required are php.exe and php5.dll (or php5ts.dll depending on version).
To add extensions, pack them along with a php.ini file:
[PHP]
extension_dir = "."
extension=php_curl.dll
extension=php_xxxx.dll
...
I tried most of solution given in the 1st answer, the only one that worked for me and is non-commercial is php-desktop.
I simply put my php files in the www/ folder, changed the name of .exe and was able to run my php as an exe !!
Also there is a complete documentation, up to date support, windows and linux (and soon mac) compatibility and options can easily be changed.
RapidEXE is exactly for this job:
SoftPedia page
Author's original page
It converts a php project to a standalone exe. I had enough of all other compilers, tried them one by one and they all disappointed me one way or another. Be my guest, feedbacks are always welcome!
Side note: the mechanism behind it is quite similar to the WinRAR SFX approach; extract engine, extract source, then run. It's just faster and easier to work with. One-command compilation, compressed, smart unpack, auto cleanup, easy config, full control of php engine & version; also extensible with minimal effort.
Happy developing!
ExeOutput is also can Turn PHP Websites into Windows Applications and Software
Turn PHP Websites into Windows Applications and Software
Applications made with ExeOutput for PHP run PHP scripts, PHP applications, and PHP websites natively, and do not require a web server, a web browser, or PHP distribution. They are stand-alone and work on any computer with recent Windows versions.
ExeOutput for PHP is a powerful website compiler that works with all of the elements found on modern sites: PHP scripts, JavaScript, HTML, CSS, XML, PDF files, Flash, Flash videos, Silverlight videos, databases, and images. Combining these elements with PHP Runtime and PHP Extensions, ExeOutput for PHP builds an EXE file that contains your complete application.
http://www.exeoutput.com/
My experience in this matter tells me , most of these software work good with small projects .
But what about big projects? e.g: Zend Framework 2 and some things like that.
Some of them need browser to run and this is difficult to tell customer "please type http://localhost/" in your browser address bar !!
I create a simple project to do this : PHPPy
This is not complete way for create stand alone executable file for running php projects but helps you to do this.
I couldn't compile python file with PyInstaller or Py2exe to .exe file , hope you can.
You don't need uniformserver executable files.
The current PHP Nightrain (4.0.0) is written in Python and it uses the
wxPython libraries. So far wxPython has been working well to get PHP
Nightrain where it is today but in order to push PHP Nightrain to its
next level, we are introducing a sibling of PHP Nightrain, the
PHPWebkit!
It's an update to PHP Nightrain.
https://github.com/entrypass/nightrain-ep

How save php script as bytecode [duplicate]

I know that PHP is compiled to byte code before it is run on the server, and then that byte code can be cached so that the whole script doesn't have to be re-interpreted with every web access.
But can you "compile" PHP code and upload a binary-ish file, which will just be run by the byte code interpreter?
After this question was asked, Facebook launched HipHop for PHP which is probably the best-tested PHP compiler to date (seeing as it ran one of the world’s 10 biggest websites). However, Facebook discontinued it in favour of HHVM, which is a virtual machine, not a compiler.
Beyond that, googling PHP compiler turns up a number of 3rd party solutions.
PeachPie
PeachPie GitHub
compiles PHP to .NET and .NET Core
can be compiled into self-contained binary file
runs on Mac, Linux, Windows, Windows Core, ARM, ...
Phalanger
GitHub (download), Wikipedia
compiles to .NET (CIL) looks discontinued from July 2017 and doesn't seem to support PHP 7.
phc
compiles to native binaries
not very active now (February 2014) – last version in 2011, last change in summer 2013
Roadsend PHP Compiler
GitHub, GitHub of a rewrite
free, open source implementation of PHP with compiler
compiles to native binaries (Windows, Linux)
discontinued since 2010 till contributors found – website down, stays on GitHub where last change is from early 2012
bcompiler
PECL extension of PHP
experimental
compiles to PHP bytecode, but can wrap it in Windows binary that loads PHP interpreter (see bcompiler_write_exe_footer() manual)
looks discontinued now (February 2014) – last change in 2011
Project Zero
Wikipedia, IBM
incubator of changes for WebSphere sMash
supported by IBM
compiles to Java bytecode
looks discontinued now (February 2014) – website down, looks like big hype in 2008 and 2009
Bambalam
compiles to stand-alone Windows binaries
the binaries contain bytecode and a launcher
looks discontinued now (February 2014) – last change in 2006
BinaryPHP
compiles to C++
looks discontinued now (February 2014) – last change in 2003
The short answer is "no".
The current implementation of PHP is that of an interpreted language. You can argue the theoretical aspects of the fact that any language can technically be interpreted or compiled, but as it stands, the current implementations are such that PHP code requires an interpreter to run, and the interpreter manages the executing environment.
To answer your question about uploading pre-compiled PHP bytecode, it's probably possible, but you'd have to implement a way for the PHP interpreter to read in such a file and work with it. With existing opcode caches out there already, it doesn't seem like a task that would reap much reward.
Since the question was first asked, there has been a change to that answer from a flat out "no" to a "kind of"
http://github.com/facebook/hiphop-php/wiki
Hip Hop for PHP was a compiler that took PHP code and turned it into highly optimized C++
Apparently, some functions are not supported (for example 'explode')
I found this question while looking for more information on how to implement HipHop and thought I'd speak up :)
Since 2013 Facebook no longer use it, however, and it has been discontinued in favour of HHVM, which is not a compiler: https://en.wikipedia.org/wiki/HipHop_for_PHP
There is also
PHP's (experimental) bcompiler extension,
which aims
To encode entire script in a proprietary PHP application
To encode some classes and/or functions in a proprietary PHP application
To enable the production of php-gtk applications that could be used on client desktops, without the need for a php.exe.
To do the feasibility study for a PHP to C converter
The extension is available from PECL.
phc allows you to compile PHP programs into shared libraries, which can be uploaded to the server. The PHP program is compiled into binaries. It's done in such a way as to support evals, includes, and the entire PHP standard library.
Um, anybody heard of Zend Guard, which does exactly what this person is asking. It encodes/obfuscates PHP code into "machine code".
If you are simply looking for producing a binary executable from a PHP script, then please avoid trying to make your question extremely precise because it will make it appear that you know exactly what you need. Besides, most PHP developer have absolutely zero clue about what a bytecode is.
With that said, the answers is YES. I have just finished compiling a PHP script into a binary. And not just any binary. I have used the CDE application (link to Wayback Machine, the original link is now broken) to turn it into an portable binary that can be distributed with all the dependencies and executed without any issue… and it works beautifully.
All you need is to use phc.
There are several "compilers" of PHP code. Most of them do not support all of PHP features, since these simply must be interpreted during run time.
We are using Phalanger - http://www.php-compiler.net/ - that is supporting even those dirty PHP dynamic features, and still is able to compile them as .NET assembly, that can be distributed as a standalone DLL.
Actually, the Just-In-Time compiler introduced with PHP 8 does in fact compile PHP. Strangely enough, it doesn't really speed up CMS based websites (e.g. WordPress), however, it does open the doors for PHP to compete with the likes of C++. For more information, see the RFC behind the JIT implementation here: https://wiki.php.net/rfc/jit. Also, Matthew Weir O'Phinney has posted a number of insightful blogs that shed light on its capabilities. Start reading here: https://www.zend.com/blog/exploring-new-php-jit-compiler.
see 5.5.x with the integrated OPcache module, volatile in a shared memory, much more performance and the dynamism principle of php remain untouched.
http://www.php.net/manual/en/opcache.installation.php
In php 7 there is the php ini option opcache.file_cache that saves the bytecode in a specific folder. In could be useful to in php cli script that are "compiled" and saved in a specific folder for a optimized reuse.
Opcache it is not compiling but is something similar.
If you are allowed to run real native binaries, then this is your compiler:
https://github.com/ircmaxell/php-compiler
It's a PHP compiler written in PHP!
It compiles PHP code to its own VM code. This VM code can then either be interpreted by its own interpreter (also written in PHP, isn't that crazy?) or it can be translated to Bitcode. And using the LLVM compiler framework (clang and co), this Bitcode can be compiled into a native binary for any platform that LLVM supports (pretty much any platform that matters today). You can choose to either do that statically or each time just before the code is executed (JIT style). So the only two requirements for this compiler to work on your system is an installed PHP interpreter and an installed clang compiler.
If you are not allowed to run native binaries, you could use the compiler above as an interpreter and let it interpret its own VM code, yet this will be slow as you are running a PHP interpreter that itself is running on a PHP engine, so you have a "double interpretation".
PHP doesn't really get compiled as with many programs. You can use Zend's encoder to make it unreadable though.
There is also bcgen (a PHP7 port of bcompiler):
https://github.com/vjardin/bcgen/
(PHP7.2 only)

Creating a simple PHP interpreter in Visual C++

When I look through the source code files I have downloaded from the PHP website, I get lost so easy. What I would like to do is obtain all the source code needed to create a Console application that takes your PHP code and executes it. I would also like to maybe be able to add C++ functions and calll them from PHP. I have done this quite simply with Lua on my Mac via Xcode, but I don't think it will be that easy using PHP on a different system using a different program.
You can write PHP extensions using C (or C++, I suppose) to expose libraries to PHP, but PHP isn't designed to be embedded in applications other than web servers.
Little 2019 update about #duskwuff comment.
PHP8 will have JIT compiler, so, no, PHP will no longer be used only for web.
As you can see on image, PHP8 compiler is faster than Java, C compiler etc.

Convert a PHP script into a stand-alone windows executable

I want to automate a fairly simple task. For this I have written a small PHP script which I run from the command line using PHP-CLI. Now I want to hand over this script to someone but I do not want to:
give away the source code
ask him to install PHP on the system
Is there a way to create a .exe version of the PHP script. I am really not much worried about de-compilation; I am more worried about asking people to install and configure PHP.
Peachpie
http://www.peachpie.io
https://github.com/iolevel/peachpie
Peachpie is PHP 7 compiler based on Roslyn by Microsoft and drawing from popular Phalanger. It allows PHP to be executed within the .NET/.NETCore by compiling the PHP code to pure MSIL.
Phalanger
http://v4.php-compiler.net/
http://wiki.php-compiler.net/Phalanger_Wiki
https://github.com/devsense/phalanger
Phalanger is a project which was started at Charles University in Prague and was supported by Microsoft. It compiles source code written in the PHP scripting language into CIL (Common Intermediate Language) byte-code. It handles the beginning of a compiling process which is completed by the JIT compiler component of the .NET Framework. It does not address native code generation nor optimization. Its purpose is to compile PHP scripts into .NET assemblies, logical units containing CIL code and meta-data.
Bambalam
https://github.com/xZero707/Bamcompile/
Bambalam PHP EXE Compiler/Embedder is a free command line tool to convert PHP applications to standalone Windows .exe applications. The exe files produced are totally standalone, no need for php dlls etc. The php code is encoded using the Turck MMCache Encode library so it's a perfect solution if you want to distribute your application while protecting your source code. The converter is also suitable for producing .exe files for windowed PHP applications (created using for example the WinBinder library). It's also good for making stand-alone PHP Socket servers/clients (using the php_sockets.dll extension).
It's NOT really a compiler in the sense that it doesn't produce native machine code from PHP sources, but it works!
ZZEE PHPExe
http://www.zzee.com/phpexe/
ZZEE PHPExe compiles PHP, HTML, Javascript, Flash and other web files into Windows GUI exes. You can rapidly develop Windows GUI applications by employing the familiar PHP web paradigm. You can use the same code for online and Windows applications with little or no modification. It is a Commercial product.
phc-win
http://wiki.swiftlytilting.com/Phc-win
The PHP extension bcompiler is used to compile PHP script code into PHP bytecode. This bytecode can be included just like any php file as long as the bcompiler extension is loaded. Once all the bytecode files have been created, a modified Embeder is used to pack all of the project files into the program exe.
Requires
php5ts.dll
php_win32std.dll
php_bcompiler.dll
php-embed.ini
ExeOutput
http://www.exeoutput.com/
Commercial
WinBinder
http://winbinder.org/ - no longer available
(original: https://github.com/stefan-loewe/WinBinder)
(fork: https://github.com/wagy/WinBinder)
WinBinder is an open source extension to PHP, the script programming language. It allows PHP programmers to easily build native Windows applications, producing quick and rewarding results with minimum effort. Even short scripts with a few dozen lines can generate a useful program, thanks to the power and flexibility of PHP.
PHPDesktop
https://github.com/cztomczak/phpdesktop
PHP Desktop is an open source project founded by Czarek Tomczak in 2012 to provide a way for developing native desktop applications using web technologies such as PHP, HTML5, JavaScript & SQLite. This project is more than just a PHP to EXE compiler, it embeds a web-browser (Internet Explorer or Chrome embedded), a Mongoose web-server and a PHP interpreter. The development workflow you are used to remains the same, the step of turning an existing website into a desktop application is basically a matter of copying it to "www/" directory. Using SQLite database is optional, you could embed mysql/postgresql database in application's installer.
PHP Nightrain
https://github.com/kjellberg/nightrain
Using PHP Nightrain you will be able to deploy and run HTML, CSS, JavaScript and PHP web applications as a native desktop application on Windows, Mac and the Linux operating systems. Popular PHP Frameworks (e.g. CakePHP, Laravel, Drupal, etc…) are well supported!
phc-win "fork"
https://github.com/RDashINC/phc-win
A more-or-less forked version of phc-win, it uses the same techniques as phc-win but supports almost all modern PHP versions. (5.3, 5.4, 5.5, 5.6, etc) It also can use Enigma VB to combine the php5ts.dll with your exe, aswell as UPX compress it. Lastly, it has win32std and winbinder compilied statically into PHP.
EDIT
Another option is to use
http://www.appcelerator.com/products/titanium-cross-platform-application-development/
an online compiler that can build executables for a number of different platforms, from a number of different languages including PHP
TideSDK
http://www.tidesdk.org/
TideSDK is actually the renamed Titanium Desktop project. Titanium remained focused on mobile, and abandoned the desktop version, which was taken over by some people who have open sourced it and dubbed it TideSDK.
Generally, TideSDK uses HTML, CSS and JS to render applications, but it supports scripted languages like PHP, as a plug-in module, as well as other scripting languages like Python and Ruby.
I had problems with most of the tools in other answers as they are all very outdated.
If you need a solution that will "just work", pack a bare-bones version of php with your project in a WinRar SFX archive, set it to extract everything to a temporary directory and execute php your_script.php.
To run a basic script, the only files required are php.exe and php5.dll (or php5ts.dll depending on version).
To add extensions, pack them along with a php.ini file:
[PHP]
extension_dir = "."
extension=php_curl.dll
extension=php_xxxx.dll
...
I tried most of solution given in the 1st answer, the only one that worked for me and is non-commercial is php-desktop.
I simply put my php files in the www/ folder, changed the name of .exe and was able to run my php as an exe !!
Also there is a complete documentation, up to date support, windows and linux (and soon mac) compatibility and options can easily be changed.
RapidEXE is exactly for this job:
SoftPedia page
Author's original page
It converts a php project to a standalone exe. I had enough of all other compilers, tried them one by one and they all disappointed me one way or another. Be my guest, feedbacks are always welcome!
Side note: the mechanism behind it is quite similar to the WinRAR SFX approach; extract engine, extract source, then run. It's just faster and easier to work with. One-command compilation, compressed, smart unpack, auto cleanup, easy config, full control of php engine & version; also extensible with minimal effort.
Happy developing!
ExeOutput is also can Turn PHP Websites into Windows Applications and Software
Turn PHP Websites into Windows Applications and Software
Applications made with ExeOutput for PHP run PHP scripts, PHP applications, and PHP websites natively, and do not require a web server, a web browser, or PHP distribution. They are stand-alone and work on any computer with recent Windows versions.
ExeOutput for PHP is a powerful website compiler that works with all of the elements found on modern sites: PHP scripts, JavaScript, HTML, CSS, XML, PDF files, Flash, Flash videos, Silverlight videos, databases, and images. Combining these elements with PHP Runtime and PHP Extensions, ExeOutput for PHP builds an EXE file that contains your complete application.
http://www.exeoutput.com/
My experience in this matter tells me , most of these software work good with small projects .
But what about big projects? e.g: Zend Framework 2 and some things like that.
Some of them need browser to run and this is difficult to tell customer "please type http://localhost/" in your browser address bar !!
I create a simple project to do this : PHPPy
This is not complete way for create stand alone executable file for running php projects but helps you to do this.
I couldn't compile python file with PyInstaller or Py2exe to .exe file , hope you can.
You don't need uniformserver executable files.
The current PHP Nightrain (4.0.0) is written in Python and it uses the
wxPython libraries. So far wxPython has been working well to get PHP
Nightrain where it is today but in order to push PHP Nightrain to its
next level, we are introducing a sibling of PHP Nightrain, the
PHPWebkit!
It's an update to PHP Nightrain.
https://github.com/entrypass/nightrain-ep

PHP in binary mode?

Is it possible to convert PHP to binary mode (compile) and run it in all standard web servers?
There are various PHP accelerators that cache the compiled byte code the interpreter produces. Not sure if they will run without the source still on the server though - I've never tried!
One Facebook developer is working on a compiled form called "Hyper-PHP", and it's not even clear if that will be an open system once complete, but no solution exists yet.
PHP Will never be a compiled Language. It will stay as an Interpreted Language.
#Matchu Facebook is just Promoting their site, so this is just an Promotion action of Facebook
There is a number of tools that will convert your php source to bytecode and allow you to protect your source code. Some of the more popular ones are Zend Guard by Zend and Ion Cube encoder. You will still need their respective loaders installed on the target server for them to function.

Categories