Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use separate php version for different php files

Tags:

php

.htaccess

I have a site (based on ZEND framework) and hosted on 1and1 server.

1and1 server uses PHP version 5.2.17 and 5.4.5. I need to use PHP version 5.4.5 for a few files only. This is because some of my other files show a errors if I use PHP 5.4.5 but they execute fine using PHP 5.2.17.

On my .htaccess file the line below was written earlier to use PHP 5.2.17

AddType x-mapp-php5 .php

To use PHP 5.4.5 I have to use the the line below (instructions to use this code will be found on the 1and1 faq page)

AddHandler x-mapp-php6 .php

FAQ url(s):

  • http://faq.1and1.com/scripting_languages_supported/php/7.html
  • http://faq.1and1.com/scripting_languages_supported/php/6.html

Is there any way to use PHP 5.4.5 for those specific files only?

I tried to use the line below in .htaccess, but it's not working:

AddType x-mapp-php5 .php
AddHandler x-mapp-php6 FilenameController.php

EDIT

I tried to edit the 5.2 code. I am using PEAR packages to create Excel sheet which is working fine with PHP 5.2.17 but displaying the error below on PHP 5.4.5

Fatal error: Cannot redeclare _PEAR_call_destructors()

Addendum

This change to the .htaccess file

AddType x-mapp-php5 .php 
AddHandler x-mapp-php6 .php6 
SetEnv APPLICATION_ENV development 
Options -MultiViews 
<IfModule mod_rewrite.c> 
  RewriteEngine On 
  RewriteBase / 

  RewriteRule ^FilenameController\.php$ FilenameController.php6  [L] 

  RewriteCond %{REQUEST_FILENAME} -s [OR]
  RewriteCond %{REQUEST_FILENAME} -l [OR] 
  RewriteCond %{REQUEST_FILENAME} -d 
  RewriteRule ^.*$ - [NC,L] 

  RewriteRule ^.*$ index.php [NC,L] 
</IfModule>

generates 404 "Page not found" error if I change the filename of FilenameController.php to FilenameController.php6 and when I am trying to visit the page its generating.

like image 760
Debashis Avatar asked Aug 17 '12 13:08

Debashis


2 Answers

Why not just

AddHandler x-mapp-php6 .php6

And rename your 5.4 scripts to php6? You can even hide this from the user by doing the following in your .htaccess file:

RewriteEngine On
RewriteBase   /
RewriteRule   ^FilenameController\.php$   FilenameController.php6 [L]

This does an internal direction to a .php6 extension for that one script file. :-)

However, let me emphasise: you can only choose which scripting engine to use on a per request basis not on a per file basis. For example: http:/yourdomain.com/FilenameController.php will be handled by the PHP 5.4 RTS and all included files will be compiled and executed using PHP 5.4; http:/yourdomain.com/index.php will be handled by the PHP 5.2 RTS and all included files will be compiled and executed using PHP 5.2.

You can't use 5.4 to compile on file within an application and 5.2 to compile the rest.

like image 107
TerryE Avatar answered Sep 19 '22 15:09

TerryE


1and1 server uses PHP version 5.2.17 and PHP version 5.4.5.

I'd be curious as to the details of this.

Assuming you run php as an Apache module (mod_php), you can only have one version running on the same installation of Apache at a time. Are you sure that your server even allows to use both at the same time? More likely, you can restart Apache and have it run with a different version. If that is the case, you can't really configure your way out of this, lest you want to set up php over cgi.

like image 43
troelskn Avatar answered Sep 20 '22 15:09

troelskn