Skip to content

Instantly share code, notes, and snippets.

@wakhub
Created April 28, 2012 16:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wakhub/2520108 to your computer and use it in GitHub Desktop.
Save wakhub/2520108 to your computer and use it in GitHub Desktop.
PHP and Perl and Python/Ruby polyglot
#<?php eval('echo "PHP Code\n";'); __halt_compiler(); ?>
print ((("b" + "0" == 0) and eval('"Perl Code\n"')) or (0 and "Ruby Code\n" or "Python Code"));
__DATA__ = 1
"""""
__END__
===== This is comment of all. =====
outputs:
$ perl polyglot.pl.php.py.rb
Perl Code
$ php polyglot.pl.php.py.rb
#PHP Code
$ python polyglot.pl.php.py.rb
Python Code
$ ruby polyglot.pl.php.py.rb
Ruby Code
$
"""
@SaswatPadhi
Copy link

Would be good if you could suppress unwanted/un-intentional output..

Currently, the outputs are :

Ruby

falsePython/Ruby Code

The false is unwanted.


#### Python

Python/Ruby Code

The new line before output is unwanted.


#### Perl
Perl Code1

The 1 is unwanted.


#### PHP
#PHP Code

The # is unwanted.

@wakhub
Copy link
Author

wakhub commented Jun 3, 2012

Fixed some issues. But it isn't perfect.

$ ruby polyglot.py 
Python/Ruby Code
$ python polyglot.py 

Python/Ruby Code

$ perl polyglot.py 
Perl Code
$ php polyglot.py 
#PHP Code
$ 

@SaswatPadhi
Copy link

Yes, not perfect .. But good effort anyways! :-)

@joshyrobot
Copy link

If the eval(...) in the PHP section doesn't do anything, it can be compressed and the hash can be removed.
#<?php echo "\x08PHP Code\n";__halt_compiler();?>

@alexd2580
Copy link

you can run php with the output_buffering parameter enabled php --define output_buffering=On $file.
then in the first line fo your polyglot you can use ob_end_clean() to drop everything that was printed before and __halt_compiler() to not even read the rest, thus yielding a "real" PHP on stdout, without magic/invis characters. e.g.:

#define X[<?php ob_end_clean();echo"PHP\n";__halt_compiler();#print(("ALGOL68",newline))COMMENT]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment