PHP server-side scripting language

On this page:


About PHP

PHP is an open-source server-side scripting language with syntax similar to that of C and Perl; for more, see php.net and zend.com. You can use the PHP scripting language to create dynamic web pages; a dynamic page interacts with the user, so that each user visiting the page sees customized information. You can also use PHP to create dynamic web pages that are generated from information in a MySQL schema. PHP commands can be embedded within a standard HTML page.

PHP offers connectivity to most common databases. PHP also offers integration with various external libraries, so you can do anything from generating PDF documents to parsing XML.

PHP versions 8.1, 8.2, and 8.3 are available on Sitehost.

Use PHP on Sitehost

Keep the following in mind when using PHP on Sitehost:

  • Bang lines are not required and will be ignored if present. (The previous web server environment required bang lines.)
  • Use .php as the file extension.
    Notes:
    • This refers only to PHP files executed directly via a URL. Include files may use other extensions, such as .inc.
    • If you have PHP files that currently use .php4 extensions, it is strongly recommended that you use .php instead.
  • The script must have owner execute permission. For best security, setting permissions to 700 (that is, read, write, and execute privileges for owner only) is strongly recommended.

    For example, where script.php is the name of the file containing the script, change the file permission as follows:

    chmod 700 script.php
  • Use /tmp or a subdirectory of /tmp as the location for temporary files such as session or cache files. This is the default location for PHP session files, but some frameworks may have different defaults. Do not use a directory within your account home directory (/groups/username) as the location for temporary files.

Specify PHP versions on Sitehost

PHP versions 8.1, 8.2, and 8.3 are available on Sitehost.

AddHandler directives are not necessary for PHP applications in .htaccess files.

Select a PHP version for your Sitehost website

The UITS Enterprise Web Technical Services website provides owners and proxies a quick way to switch their website's PHP version. The Sitehost section for the account contains a PHP Version drop-down; for additional instructions, see Manage Sitehost accounts with the Enterprise Web Technical Services site.

Select a PHP version for the SSH command-line

If you have server-side scripts or cron jobs that use PHP, you will need to ensure that you are using the correct PHP version. PHP 8.1 is the default if you do not specify another version. To set a specific version of PHP on the SSH server, run one of the following commands:

  • php_version 8.3
  • php_version 8.2
  • php_version 8.1

These commands create a symlink in your ~/bin directory. After running one of the commands, log out of the account and log back in for the change to load into your environment variable. PHP versions will be removed from the SSH server when they reach end of life (EOL). You may receive a message that "no such file or directory can be found" if PHP is set to a removed version. Using the command php_version without specifying a version will set your account to use the default PHP version. Experienced users may also remove the symlink using standard Linux commands.

Notes:
  • These commands only work if bash is your default shell. UITS is working to add support for other shells.
  • Crontabs should include the following at the top to run the PHP version you set above:
    PATH=/groups/username/bin:$PATH

Troubleshoot

If you receive a "500 - Internal Server Error" message when running your scripts, check the following:

  1. Make sure the file permission is set to 700 (read, write, and execute for owner only).
  2. Make sure the directory (folder) permission is set to 711. Setting it to 777 will cause a "500 - Internal Server Error".
  3. If the file/directory permissions are set correctly and you still get a 500 error, it is likely due to line compatibility between the program used to create/upload the file and the Linux server. To fix this, log into the account on Sitehost via an SSH client with a command line interface, and then do the following:
    1. Use cd to move to the directory where your PHP file is located (for example, cd www).
    2. Open the file using an editor (for example, nano), and then save it:
      Ctrl + o

      This converts Windows CR/LF (carriage return/line feed) to Unix LF (line feed) or Mac CR to Unix LF.

    To prevent this problem in the future, change the settings/preferences in the program you are using to develop PHP files. To set preferences in Dreamweaver, go to Edit > Preferences. Select Code Format or HTML Format, depending on the versions. Change Line Breaks to LF (Unix).

  4. Test your site on sitehost-test.iu.edu. PHP errors will display in the browser on the server.

Sample PHP code

Place a file containing the code below inside the web directory of your Sitehost account and save it as info.php:

<?php
phpinfo();
?>

To view the file, provide the web site address to the file; for example, http://account.sitehost.iu.edu/info.php (or http://account.sitehost-test.iu.edu for the test site).

This will output information about the current state of PHP, which includes information about PHP compilation options and extensions, PHP version, server information and environment (if compiled as a module), PHP environment, OS version information, paths, master and local values of configuration options, HTTP headers, and the PHP license. Because every system is set up differently, you can use phpinfo() to check configuration settings and for available predefined variables on a given system. For more, see phpinfo from php.net.

Increase the memory limit

You can increase the memory limit with a .user.ini file in the same directory as your PHP script. If you have multiple subdirectories, you'll need to place the .user.ini file in each subdirectory that has PHP scripts.

In the .user.ini file, to specify the memory_limit you wish to set, use:

memory_limit = whatever integer you wish

To verify the memory_limit has changed, create a php info file.

Resources

For more about PHP, see:

This is document bfsu in the Knowledge Base.
Last modified on 2023-08-03 14:26:32.