PHP 4 to PHP 5

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vwduud
Forum Newbie
Posts: 10
Joined: Tue Mar 23, 2010 1:50 pm

PHP 4 to PHP 5

Post by vwduud »

I tried a few searches on this topic as I am sure it must be covered already somewhere. Blocked key words in the search made the results very limited.

We have a PHP application that dates back to the days when PHP 4 was hip. I maintain this app, but am a MS desktop application developer. I am now faced with the task of upgrading PHP from version 4.XXXX to the latest version 5.XXXX.

The method I am currently using involved downloading the ZIP file of version 5.3.2. I unzipped it and placed it in C:\PHP-5 on the server. I set php-cgi.exe as the mapped EXE for php files, placed the new version of php.ini in c:\windows and set cgi.force_redirect = 0 in the php.ini file. These steps were followed as it is how we have always set up PHP 4.XXXX.

Although the logon page to my web app displays under these settings, I've had to make changes to get through the code:

Change 1:
Original Code:if($REQUEST_METHOD == "POST")
New Code: if($_SERVER['REQUEST_METHOD'] == "POST")

Change 2:
Original Code: while(list($key, $value) = each($form))
New Code: while(list($key, $value) = each($_POST['form']))


This is slowly getting me through my code. The keyword there is "slowly". There must be a migration setting or something I can do rather than modify every instance of situations like these. Can someone please point me in the right direction?

Thanks - Jim
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP 4 to PHP 5

Post by requinix »

You should really, really, really be making those changes.

With that said: the "migration setting" is register_globals. It's a security risk so avoid it if you possibly can.
Note that it's deprecated in 5.3 and will be completely removed in 6.0.

If you have some sort of framework in place there is a nicer (but not any smarter) solution.
vwduud
Forum Newbie
Posts: 10
Joined: Tue Mar 23, 2010 1:50 pm

Re: PHP 4 to PHP 5

Post by vwduud »

Thanks for your reply. Agreed, the changes should be made rather than using that setting. This will be presented as the best solution ultimately. Just trying to get past these hurdles to at least understand the scope of work needed to make the migration "right" (correct).

That did solve that problem.

Next problem:

I have another php file containing a function that is reached in the current php file using the "require_once" function. There is a function in db_func.inc.php (db_open()) that needs to be called from logon.php. In PHP 4, this function was reachable. In PHP 5, I now get:

"Call to undefined function db_open() in C:\Webserver\smarts97\logon.php on line 37"

Is there yet another setting that I need to change to set my include paths? I have compared the INI's from PHP 4 and 5 to see if there was something obvious, but nothing specific was set in PHP 4's INI concerning my include pathing. Both of these files are in the same folder.

Thanks - Jim
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP 4 to PHP 5

Post by requinix »

It wasn't spelled out so I'll ask:

logon.php does include/require db_func.inc.php, right? Or does so by including some set of intermediate files?
Be sure you're using require_once(), not include/_once or vanilla require().
vwduud
Forum Newbie
Posts: 10
Joined: Tue Mar 23, 2010 1:50 pm

Re: PHP 4 to PHP 5

Post by vwduud »

tasairis wrote:It wasn't spelled out so I'll ask:

logon.php does include/require db_func.inc.php, right? Or does so by including some set of intermediate files?
Be sure you're using require_once(), not include/_once or vanilla require().
Yes. At the top of logon.php:

require_once("dbfunc.inc.php");

At line 37 in logon.php:

db_open();

The function db_open() exists in dbfunc.inc.php. Both of these files exists in the same folder. "This" worked in PHP 4.XXX. It is not working in PHP 5.3.2

Thanks - Jim
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP 4 to PHP 5

Post by requinix »

Can you post the code for both places?
vwduud
Forum Newbie
Posts: 10
Joined: Tue Mar 23, 2010 1:50 pm

Re: PHP 4 to PHP 5

Post by vwduud »

tasairis wrote:Can you post the code for both places?
logon.php:

Code: Select all

<?php
 
require_once("config.inc.php");
require_once("dbfunc.inc.php");
require_once("session.inc.php");
require_once("user_options.inc.php");
 
include("version.inc.php");
 
// Initialize $message
$message = "";
 
 
       //echo "before post, request method: " .  $_SERVER['REQUEST_METHOD'] . "<BR>";
 
// Has the form been posted?
if($REQUEST_METHOD == "POST")
//if($_SERVER['REQUEST_METHOD'] == "POST")
{
    global $form;
       echo "trying to logon" . "<BR>";
    $login_ok = false;
 
    $form1 = $_POST['form'];
    var_dump($form1);
 
    // Trim all submitted data
    while(list($key, $value) = each($form))
    //while(list($key, $value) = each($_POST['form']))
    {
        $form[$key] = trim($value);
    }
    
    if ( isset( $form["name"] ) && isset( $form["pass"] ) )
    {
       //echo "trying to open DB" . "<BR>";
        db_open();
        $query = "select EmployeeId from users where EmployeeID=\0|".$form["name"]."\0| and Password=\0|".$form["pass"]."\0|";
 
        $sql_result = smarts_query( $query );
 
        // check make sure employee is not inactive.  IF they are do not allow logon
        $query2 = "Select tblhier4.ActiveInd FROM tblhier4 inner join users on users.EmployeeID = tblhier4.Level4ID WHERE users.employeeID =\0|".$form["name"]."\0|";
 
        $sql_result2 = smarts_query( $query2 );
 
        $row = mysql_fetch_row($sql_result2);
 
        if ( $sql_result && $sql_result2)
        {
            $num_rows = mysql_num_rows ( $sql_result );
 
            if ( $num_rows == 1 && $row[0] == 1 )
            {
               $login_ok = true;
            }
        }
        if ( $row[0] != 1 )
            $flag = 1;
    }
 
   // Can the form be processed or are there any errors?
   if( $login_ok ) {
       //echo "logon ok";
      session_start();
      
      $query = "select EmployeeId from users where EmployeeId=\0|".$form["name"]."\0|";
      $result = smarts_query($query);
      $logon_id = mysql_result($result, 0);
 
      session_register("logon_id");
 
      // setup user options
      get_user_option("EmployeeFilter");
      get_user_option("ProspectFilter");
      get_user_option("ProspectDate");
 
      if ($form["pass"] == "") {
         header( "Location: password.php" );
         exit;
      }
 
      header( "Location: salesmenu.php?logon=1" );
 
      exit;
    }
    else {
        if ( $flag == 1 )
            $message = "Inactive user name.<br>Contact your administrator for help.<br><br>";
        else
            $message = "Invalid user name or password.<br>Please try again, or contact your administrator for help.<br><br>";
    }
}
 
// clear out any previous logon id's this has the effect of logging out the user
session_start();
session_register("logon_id");
$logon_id = "";
 
include ("class.FastTemplate.php");
$tpl = new FastTemplate( "./templates" );
$tpl->define( array( "logon" => "logon.template" ) );
 
// note, we can't use the global one here because we aren't logged in yet
$tpl->assign( array(
               "TITLE" => "SMARTS",
               "VERSION" => $smarts_version,
                    "MESSAGE" => $message,
                    "NAME" => isset( $form["name"] )? $form["name"] : ""
                    )
            );
 
$tpl->parse( "LOGON", "logon" );
$tpl->FastPrint( "LOGON" );
 
 
?>
dbfunc.inc.php:

Code: Select all

<?
   function db_open () {
      global $sql_conf;
 
      mysql_connect( $sql_conf[ "host" ], $sql_conf[ "user" ], $sql_conf[ "pass" ] )
         or die ("Could not connect to MySQL database on " . $sql_conf[ "host" ] );
 
      mysql_select_db( $sql_conf["db"] ) 
         or die ('Could not connect to database ' . $sql_conf[ "db" ] ); 
   }
 
   # replaces " with ' and \0| with "
   # this way, the query won't break when the user types "
   # the user cannot type null|, so we use that
 
   function smarts_query($query){
      global $message;
      $query = str_replace("\"", "'", $query);
      $query = str_replace("\0|", "\"", $query);
 
      $result = mysql_query($query);
      if (!$result){
         echo "Error -- $query\n";
         echo mysql_error();
      }
      return $result;
   }
?>
 
Post Reply