Indiana University

IU Webmaster

PHP Server-Side Scripting Language

Table of Contents

I. What is PHP?
  • PHP is an open-source server-side scripting language (freely downloadable from php.net and zend.com). PHP version 5.2.4 is available on Webserve (www.indiana.edu, www.iupui.edu, www.iun.edu, and www.iuk.edu).

  • You can create dynamic web pages with the PHP scripting language. A dynamic Web page interacts with the user, so that each user visiting the page sees customized information. PHP can also be used to create dynamic web pages that are generated from information accessed from a MySQL database.

  • You can embed PHP commands within a standard HTML page. PHP's syntax is similar to that of C and Perl, making it easy to learn for anyone with basic programming skills.

  • Another feature that PHP offers is connectivity to most of the common databases. PHP also offers integration with various external libraries, which allow the developer to do anything from generating PDF documents to parsing XML.

Return to Top


II. Using PHP on Webserve

  1. Bang lines are not required and will be ignored if present. (This information is provided for historical purposes as the previous web server environment required bang lines.)

  2. You should use .php as the file extension. This refers to only 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 move toward using .php for these instead.

  3. The script must have owner execute permission. For best security, we strongly recommend using permissions set to 700 (only owner has read, write, and execute privileges).

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

    chmod 700 script.php
    

Return to Top


III. Sessions
By default, when PHP creates a session file it stores it in /tmp. The /tmp directory is local and independent to each of the backend web servers. Hence, if a request is made to server_1, it stores the session file in its own /tmp. If a subsequent request for the same user ends up going to server_2, it won't find the original session file.

The load balancers control how web requests are distributed across the backend web servers. These load balancers are configured to provide session persistence by tracking the IP address of the web browser and the backend web server to which a request is sent. The goal is to send all subsequent requests from a given web browser back to the same backend web server. As a result, normally the PHP session files in /tmp work as expected because the multiple requests for the same user is consistently sent to the same backend web server.

However, the load balancers can only cache so much data and eventually older data is overwritten by new incoming requests. This can cause the loss of persistence information. PHP allows you to specify the location of the session files (i.e., other than /tmp). In the Webserve environment, it is recommended that you create a session directory in the login directory of your account and write the session information there. Doing it this way will guarantee session persistence. You should do occasional cleanup of any unused session files that aren't deleted.

Return to Top


IV. Troubleshooting
When running your scripts, if you receive a 500 Internal Server Error message, please check the following:

1. Check to make sure the file permission is set to 700 (read, write, and execute for owner only).

2. Check the Directory (folder) permission. Setting it to 777 will give you a 500 - Internal Server Error.
Set it to 711.

3. When the file/directory permissions are set correctly, and you are still getting a 500 error, it is likely due to line compatibility between the program used to create/upload the file and the Linux server. To correct this, login to the account on Webserve via an SSH client that provides a command line interface and do the following:

  1. Move to the directory where your php file is using the 'cd' command (e.g., cd www).
  2. Open the file using an editor such as nano and save it by typing Ctrl + o
This simple steps may solve your problem because it converts Windows CR/LF (carriage return/line feed) to Unix LF (line feed) or Mac CR to Unix LF.

To prevent this problem, you need to change 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).

Return to Top


V. Sample PHP Code
Place a file containing the code below inside the www directory of your Webserve account and save it as info.php

<?php 
phpinfo(); 
?>
To view the file, you would then provide the web site address to the file. For example:
http://www.indiana.edu/~myaccount/info.php

It will output information about the current state of PHP, which includes information about PHP compilation options and extensions, the PHP version, server information and environment (if compiled as a module), the PHP environment, OS version information, paths, master and local values of configuration options, HTTP headers, and the PHP License. Because every system is setup differently, phpinfo() can be used to check configuration settings and for available predefined variables on a given system . For more information see php.net's entry for phpinfo.

Return to Top


VI. Connecting to a database using PHP

We provide sample code as an example of scripts connecting to MySQL database from your Webserve account when you use PHP or Perl. The code is located at

http://webmaster.iu.edu/tool_guide_info/mysql/connect_mysql.shtml.

Return to Top


VII. How do I increase the memory limit?
You can increase the memory limit with a php.ini file in the same directory as your php script. If you have multiple subdirectories, you will need to place the php.ini file in each subdirectory that has php scripts.

In that php.ini file you can specify the memory_limit you wish to set by using:

memory_limit = whatever integer you wish
You can verify the memory_limit has changed by creating a php info file.

Return to Top


VIII. Resources: Links to PHP Information