Обсуждение: migration problem

Поиск
Список
Период
Сортировка

migration problem

От
PJ
Дата:
I have a couple of LAN
servers set up for learning & developing. One is FreeBSD 4.10
running apache 1.3 with php4 module and postgresql 7.3 ; the 7.0 is
running apache22, php5 and posstgresql 8.3.
Here's the problem: Everything is fine on the older machine. I
transferred one "WebSite" to the newer machine; did the pg_dumpall to
the new machine and all appears ok. I changed the include
$DOCUMENT_ROOTs to ($_SERVER["DOCUMENT_ROOT"]...... and all that is
well. However, there seems to be a problem with the sql code which was
done by another party. There is a function "checkuser" which is invoked
upon login from index.php. The instruction is:
checkuser(array(0,1,2,3,4,5,6));...... and there it stops.

function checkuser($group_id) {
    global
$sid,$ttl,$login_prompt,$user,$password,$REMOTE_ADDR,$HTTP_POST_VARS,
    $HTTP_GET_VARS,$SCRIPT_NAME,$HTTP_USER_AGENT,$HTTP_REFERER,
    $REQUEST_URI,$loglevel;

    if(!isset($user)) {
    $user='';
    }
    if(!isset($password)) {
    $password='';
    }
    if(!isset($sid)) {
    $sid='';
    }
    if(is_array($group_id)) {
    $group_id=join($group_id,',');
    }
    /*
     * Statistics: check if such page exists in database
     * If not, add it
     */
    if($loglevel>=1) {
    list($page_id)=sqlget("
        select page_id from pages where name='$SCRIPT_NAME'");
    if(!$page_id) {
        $page_q=sqlquery("insert into pages (name) values
('$SCRIPT_NAME')");
        $page_id=sqlinsid($page_q);
    }
    }

    /*
     * Get user ID by session ID
     */
    list($user_id)=sqlget("
    select \"user\".user_id from \"user\",groups,user_group,session
    where hash='$sid' and
        \"user\".user_id=session.user_id and
        user_group.group_id=groups.group_id and
        \"user\".user_id=user_group.user_id and
        groups.group_id in ($group_id) and
        end_time>".(time()));

    /*
     * No such session, or session is expired
     */
    if(!isset($user_id) || $user_id=='') do {
    /*
     * Handle POSTs
     * Check password and group; anonymous access also
     */
    list($user_id)=sqlget("
        select \"user\".user_id from \"user\",groups,user_group
        where \"user\".user_id=user_group.user_id and
        user_group.group_id=groups.group_id and
        groups.group_id in ($group_id) and
            ((\"user\".name='$user' and
            \"user\".password='".(md5($password))."') or
            groups.anonymous='Y')");

    /*
     * yeah, authorized
     */
    if(isset($user_id) && $user_id!='' &&
$user_id>=0) {
        list($md5)=sqlget("
        select hash from session where user_id='$user_id' and
            ip='$REMOTE_ADDR' and end_time>".(time())."
        order by end_time desc");
        if(isset($md5) && $md5!='') {
        sqlquery("
            update session set end_time=".(time()+$ttl).",
            visited_pages=visited_pages+1
            where hash='$md5' and user_id='$user_id'");
        }
        else do {
        mt_srand((double)microtime()*1000000);
        $rnd=mt_rand(0,(double)microtime()*1000000);
        $md5=md5("$rnd$REMOTE_ADDR$user_id$password");
        $result=sqlquery("
                insert into session
(hash,user_id,start_time,
            end_time,ip,visited_pages,useragent)
                values
('$md5','$user_id',".(time()).",".(time()+$ttl).",
            '$REMOTE_ADDR',1,'$HTTP_USER_AGENT')");
        } while (strcmp($result,'error')==0);

        setcookie('sid',$md5);
        $sid=$md5;
        break;
    }

    /*
     * Unauthorized; prompt to login
     * Save POST and GET variables, except user/password
     */

    setcookie('sid','-1');
    $vars='';
    while(list($name,$value)=each($HTTP_POST_VARS)) {
        if($name!='user' && $name!='password') {
        $vars.="\n<input type=hidden name='$name'
value='$value'>";
        }
    }
    while(list($name,$value)=each($HTTP_GET_VARS)) {
        if($name!='user' && $name!='password') {
        $vars.="\n<input type=hidden name='$name'
value='$value'>";
        }
    }
    $login_prompt=eregi_replace('<!-- INFERNO -->','<!--
INFERNO -->'.$vars,$login_prompt);
    echo $login_prompt;
    exit();
    } while (0);
    /*
     * Update existing session to prevent expiration
     */
    else {
    sqlquery("
        update session set end_time=".(time()+$ttl).",
        visited_pages=visited_pages+1
        where hash='$sid'");
    }

    /*
     * Statistics: write page view
     */
    if($loglevel>=1) {
    sqlquery("
        insert into visits (session_id,page_id,when_stamp,path,referer)
        select session_id,$page_id,'now','$REQUEST_URI','$HTTP_REFERER'
        from session
        where hash='$sid'");
    }

    return $user_id;
}

The Web page does not load. If I remove the line checkuser (array....) 
from the php code, things come up fine.  Sooooo, I'm a little lost.
Could it be that the sql code should be different for the current psql?
The database is fine, I can access it and view it (SELECT * FROM
....etc...etc.) from the command line.

Could someone please steer me as to what to look for and where to find
possible correcections?
Thanks much in advance.

PJ

Re: migration problem

От
Craig Ringer
Дата:
PJ wrote:

> ($_SERVER["DOCUMENT_ROOT"]...... and all that is well. However, there seems to
> be a problem with the sql code which was done by another party. There is a
> function "checkuser" which is invoked upon login from index.php. The instruction
> is: checkuser(array(0,1,2,3,4,5,6));...... and there it stops.

That function hits the database repeatedly. If the problem is in fact
with a database query that it's generating you need to determine which
one. You could step through the function with a PHP debugger or just
insert log/printing statements in the function so you can see its progress.

Alternately, run the problem checkuser() function then in psql run:

   select * from pg_stat_activity;

and see if there are any long-running queries in there that correspond
to SQL generated by the checkuser() call.

Once you know which part of the function is causing a problem, you can
perhaps discover something useful. If you track it down to a particular
query that the function is generating, and you can't figure out what's
wrong by looking at the query, you can try running it with EXPLAIN in psql.


--
Craig Ringer

Re: migration problem

От
Tom Lane
Дата:
Craig Ringer <craig@postnewspapers.com.au> writes:
> That function hits the database repeatedly. If the problem is in fact
> with a database query that it's generating you need to determine which
> one. You could step through the function with a PHP debugger or just
> insert log/printing statements in the function so you can see its progress.

Even easier, turn on query logging (log_statement = all), run the
function, and then look in the postmaster log to see what happened.

7.3 is a long time back, and I wouldn't be surprised if you are hitting
some compatibility issues in trying to make the jump to 8.3 in one step.
It's hard to tell what though ... I don't find that PHP code very
readable.

            regards, tom lane

Re: migration problem

От
"Scott Marlowe"
Дата:
On Fri, May 16, 2008 at 7:23 PM, PJ <af.gourmet@videotron.ca> wrote:
> I have a couple of LAN servers set up for learning & developing. One is
> FreeBSD 4.10 running apache 1.3 with php4 module and postgresql 7.3 ; the
> 7.0 is running apache22, php5 and posstgresql 8.3.
> Here's the problem: Everything is fine on the older machine. I transferred
> one "WebSite" to the newer machine; did the pg_dumpall to the new machine
> and all appears ok. I changed the include $DOCUMENT_ROOTs to
> ($_SERVER["DOCUMENT_ROOT"]...... and all that is well. However, there seems
> to be a problem with the sql code which was done by another party. There is
> a function "checkuser" which is invoked upon login from index.php. The
> instruction is: checkuser(array(0,1,2,3,4,5,6));...... and there it stops.

The php code gives me a bit of a headache, but I'll make a couple of
suggestions and observations.

Older PHP servers often have global variables turned on.  They are
turned off by default.  This means that an url like:
http://myserver.com/mypage.php?var1=val1 would set a variable called
$var1 equal to val1 on a server with global vars set to on.  With it
off, you'd get no such auto assinment and would have to pull it from
$_GET['var1'].

Also, older php servers usually were set up to print php errors to the
screen.  Mostly now they are defaulted to NOT outputting errors to the
screen, and you might do well to turn them on if they're off.  Then
the page should give you useful info when it fails.

Re: migration problem

От
PJ
Дата:
Thank you Tom & co. for your most helpful suggestions.
If I may, I'll give a quick resume of my "adventure":
In trying to debug this upgrade from php4 to php5 and postgresql 7.4 to
8.3 I have learned much.
First trying to set up various debuggers like PHPed, Eclipse with
phpeclipse, DEBUG and XDEBG is generally a horrendous nightmare.
After reading all I could find and trying all suggestions and
instructions, I found that the DBGbar plugin for Firefox found the
errors far better than all the others; it got as far as the login page
which normally comes up when the user is anonymous from the $sid
I did finally manage to set up XDebug - but not the way of the
instructions: use only the zend_extension=/where/the/file/is/xdebug.so
the php.ini file. The extensions.ini file should have any & all
dbg.so, dbg.so.x.x.x and xdebug.so commented out or just out for xdebug
to be loaded as an xdebug extension and a zend_extension.
I just could not get phpeclipse to work right; kept getting errors for
include files (= no such file at .:/usr/local/share/pear) And PHPed as
I tried it just mucked up the files. Luckily I had made copies. :)
But, I must say, Eclipse seems like a great tool to program in java.

Apparently, the programmers used tcl functions like pg_exec to work
with postgresql and it works fine in pg7.4 but not in 8.3.
What could I do to either link or use the tcl commands as it would seem
that rewriting all the instances of these instructions might just be
too much to bother?

Thanks in advance.
Philip



Tom Lane wrote:

  PJ <af.gourmet@videotron.ca> writes:


    Normally, accessing the test site the main page should come up without
a login based on the sid&nbsp; ($sid) which should come from a cookie. But
it looks like on this newer server there is no cookie set for, if it
were, then the opening page should come up. From the log output I might
think that the error is in the formatting of the null string $sid=' ' .
This I am not sure of. <br>



What it's complaining about is the places where an inet value is
assigned or compared with an empty string.  I'm quite certain that
that didn't work in 7.3 either, though, so I'm a bit baffled how
your application ever worked at all.  You should go look at where
the app is getting its IP addresses from.  Maybe it is trying to
parse them out of some log output or something that has changed
format since 7.3?

            regards, tom lane