Advertisement
aaaaaa123456789

PHP quine

Sep 27th, 2013
178
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. $code = <<<'code_end'
  4.   /*
  5.      This function outputs all of the code used to write this program.
  6.      It effectively makes this program a quine.
  7.   */
  8.  
  9.   // declare $code (remember it contains the code of this function itself)
  10.   global $code;
  11.  
  12.   // take care of the initial opening statements
  13.   $initial = "<?php\n\n\$code = <<<'code_end'\n";
  14.  
  15.   // add the final few lines into an array, which we'll concatenate later
  16.   $lines = array(
  17.     'code_end;',
  18.     '$printing_function = create_function("", $code);',
  19.     '$printing_function();'
  20.   );
  21.  
  22.   // create a variable containing the closing tag
  23.   // the tag is created via concatenation to prevent it from ending the code
  24.   $closing_tag = '?' . '>';
  25.  
  26.   // concatenate the $lines array
  27.   $final_lines = "";
  28.   foreach ($lines as $line)
  29.     $final_lines .= $line . "\n\n";
  30.  
  31.   // send a header to the browser, to show this as text instead of HTML
  32.   header("Content-Type: text/plain");
  33.  
  34.   // concatenate all the individual parts of the code and output it
  35.   $sourcecode = $initial .
  36.                 $code . "\n" . // nowdoc syntax doesn't add the final newline
  37.                 $final_lines .
  38.                 $closing_tag;
  39.   echo($sourcecode);
  40.    
  41. code_end;
  42.  
  43. $printing_function = create_function("", $code);
  44.  
  45. $printing_function();
  46.  
  47. ?>
Advertisement
RAW Paste Data Copied
Advertisement