Convert PHP file to binary
Asked Answered
E

5

27

Is it possible to convert a PHP file to binary and deploy it on a webserver?

Employee answered 4/12, 2009 at 6:16 Comment(4)
What do you mean convert to binary? PHP is an interpreted language, meaning it is read line by line and executed (roughly.) It is not compiled to a binary file like C, you simply place the file in a web directory and allow the user to access it; of course, the web server must be running PHP.Herbivore
You want to encrypt the source code and distribute it as an executable so that no one can steal and alter the code for re-use, right?Illhumored
c'mon really .. you cant do a better job asking a question ? .. no idea what you are trying to doFinlay
@Hulk: if your aim is what @Salman A says you should use Zend Guard(zend.com/en/products/guard)Habakkuk
I
24

Since PHP is a (relatively)portable language, compiling PHP source to bytecode instead of binary code is more preferable. For this purpose there is an experimental project called bcompiler.

Ilonailonka answered 4/12, 2009 at 6:23 Comment(3)
There is also roadsend, which (I think) is using parts of phpc after they abandoned scheme.Mella
phc is kind of a pain to install, and roadsend seems to be slower than running files through the php interpreter.Ultraism
Once you use pecl command line tool to install bcompiler into your PHP, you can use bencoder as a script to encode one file or an entire directory (and subdirectories) of all PHP files, as well as copying over into a new dir all the unencoded files. You may find you don't need IonCube in that case. But this is experimental still, so please test that all functionality still works.Lease
A
14

Some months ago I searched for it (php code protection or script protection) and my results were:

I provide some description but try to google them and pardon me.

Be careful with your search because you may encounter to php compressors instead and compressors are just about GZIP and other http transfer compression mechanisms.

The most important issues with php script protecting tools are:

  1. How much speed of php runtime performance will get low(down)(how much speed down)

  2. Will we need an extra environment or installed tools on the php server?

  3. Easy handling

You will find about whole description and comparison on: http://www.seocompany.ca/software/free-encryption-software.html

Obfuscators

A lot of obfuscators you will find on the web but there's no guarantee for your codes to work.

Even I found a mixed way with Visual C++ and MSVS for obfuscating php and because also I'm experienced with asp.net I tested it however didn't work.

Converters and Lockers

  • ion protector (http://www.ioncube.com/): Oh not free but I heard many about his famous name

  • phc (php compiler): free and was a good case but "Brendan Long" said the truth because phc has a painful install way. However result is magic and has famous name such as ion

  • php locker: I got it and test it. It had error with compiled code and it's released for lower than php5. Absolutely not free and for who thinking about ccr-ac*ks finding it for php locker is impossible.

  • Zend Guard: A perfect way to guard but it's harder than phc or ion because you need Zend runtime environment (ZRE) on your server and Zend Guard is absolutely non-free however if you are a Zend guy (zend framework+zend IDE+zend Guard) you should know Zend Guard is compatible with zend IDE and code blocking process will be so easy. I'm not a Zend guy because either of zend framework and zend guard get down runtime speed obviously. I love php speeded runtime.

  • php -> C/C++ compiling (YES IT'S POSSIBLE) (BEST PERFORMANCE THROUGH THE WORLD): It's not so famous so it's normal if no one talked about.

  • facebook hiphop: This tool is using by facebook to compile php files on c++/c for getting a better performance however finally we have unreadable compiled codes. I don't remeber but I think it needs a good familiarity to linux or about php recompile mechanism on Microsoft Visual C++. (However DON'T TRUST to my weak memory and google it) (AND FOR LEARNING ABOUT PHP RECOMPILE WAY PLZ REFER TO latest release of php_manual.chm)

Finally I made my self simple php obfuscator using regular expressions which worked on php 5.3 and my complex scripts so I moved on OpenSSL and different encryption mechanisms and got a review on php secure development.

Wishing you be successful.

Arsenal answered 6/5, 2012 at 9:26 Comment(1)
Sounds like an XY AnswerTwostep
A
5

This is way overdue but should do what you want.

Make your own custom extension with the zend engine as they compile to a c++ dll. Add that dll to your ext location in the php directory. After that you can call your own php scripts as php functions. Keep in mind that php is server side only so no one can get your scripts unless they are copied from the server and handed out.

Download the php source library for the version that you have. [windows.php.net] The zend stuff you need will be included.

Step 1. Create a new project and select "General" under Visual C++ and the empty project option. Give it a name and Click ok. Mine is Project1 so I will be using that.

Step 2. Right click the solution name and select properties. Select Dynamic Library (.dll) Under Project Defaults/Configuration Type in the Configuration Properties/General item. Be sure that you are changing the Debug config as Release is not used.

Step 3. Under VC++ Directiories add the following under the Include libraries: C:\php-7.1.8-src\Zend C:\php-7.1.8-src\win32 C:\php-7.1.8-src\TSRM C:\php-7.1.8-src\main C:\php-7.1.8-src Libraries: C:\php-7.1.8\dev

Step 4. Under C/C++ Preprocessor add ZEND_DEBUG=0;ZTS=1;ZEND_WIN32;PHP_WIN32 to Preprocessor Definitions

Step 5. Under Linker Input add php7ts.lib. This needs to match the lib for your version of php

Click ok to save the Property settings for your solution.

Now lets add a code file for your function (Project1)

Under the source files folder right click and add Code. I called mine Project1.cpp and here is the source. You'll need to rename config.w32.h.in to config.w32.h and copy it to the directories where it is needed. Intellisense should tell you where it's looking and config.w32.h.in is in the C:\php-7.1.8-src\win32 directory.

// this needs to match the compiler version that php was compiled with
#define _CRT_SECURE_NO_WARNINGS
#define PHP_COMPILER_ID "VC14"

#pragma once
/* You must include config.w32.h first */
#include "win32/config.w32.h"

#include "php.h"

ZEND_FUNCTION(ReturnString);

zend_function_entry Project1_functions[] =
{
    ZEND_FE(ReturnString, NULL)
    {
        NULL, NULL, NULL
    }
};

zend_module_entry Project1_module_entry =
{
    STANDARD_MODULE_HEADER,
    "Project1 Module",
    Project1_functions,
    NULL, NULL, NULL, NULL, NULL,
    NO_VERSION_YET, STANDARD_MODULE_PROPERTIES
};

ZEND_GET_MODULE(Project1)

ZEND_FUNCTION(ReturnString)
{
    zval* value;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE)
    {
        RETURN_FALSE;
    }

    convert_to_string(value);

    RETURN_STRING(Z_STRVAL_P(value), true);
}

Press F7 to compile and it should create Project1\Debug\Project1.dll. Copy Project1 to your ext directory C:\php-7.1.8\ext and add an extension=Project1.dll to your php.ini and you should be good to go. If it doesn't work double check your properties to make sure you didn't miss something.

The script is quite simple:

<?php
echo ReturnString("The Returning String");
?>
Anglophobe answered 25/8, 2017 at 18:34 Comment(1)
There is a setting for caching php byte code so check near the bottom of your php.ini for the [opcache] settings.Anglophobe
M
4

If the web server is Linux based, you can create a package like .deb or .rpm (depending on the Linux distribution) and easily distribute/deploy it.

Meave answered 4/12, 2009 at 6:27 Comment(3)
It's not a bad answer. The question seems to be asking "How do I convert php files into a distributable program".Ultraism
Convert a PHP file into a binary is not the same converting a PHP file into a distributable program.Ilonailonka
Well obviously the question is not that clear. But it seems that he needs a servlet like approach.Habakkuk
B
1

It depends on what you mean by "binary".

If you want to compile (or just obfuscate) your PHP code to keep someone else from modifying then use a bytecode compiler. One example are the tools from Zend, among others. (I prefer Zend's tools because they the primary company behind PHP and fully QA all their tools against all the versions of PHP).

If you want to compile your PHP code and link the PHP runtime to it and then deploy it (like C\C++), then no. Maybe in theory that would be possible but it would be a mess. (Not practical or feasible and don't think anyone has put anything together to try and the output would also be tied to a particular architecture).

Boonie answered 4/12, 2009 at 6:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.