Solved

Trouble calling functions in included PHP file

Posted on 2014-01-15
32
278 Views
Last Modified: 2014-01-20
I've run into a strange problem - probably compounded by my ignorance!

I'm trying to move a client web-site from their old host to my server (a VPS at WiredTree)
We will be building a new site for them but in the meanwhile I just need to make their old content work on my server - it doesn't!

In their old site each php file has an include statement like this:
<?
    include "../textblocks.php";
?>

and in the textblocks.php are defined a number of functions.  The textblocks.php file begins and ends with <?php and ?>

In their old environment everything works fine.  On my server - although the textblocks.php file is loaded (I tested that by changing the include to require)  When a function form that file is called I get an undefined function error.

What could be causing that?  Any ideas?  Any cures?
0
Comment
Question by:DMTrump
  • 13
  • 9
  • 8
  • +2
32 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 200 total points
Comment Utility
Is this the exact format?
<?
    include "../textblocks.php";
?>


... and not
<?php
    include "../textblocks.php";
?>


Could be you don't have short tags enabled on the new server, easily enabled in the php.ini
0
 
LVL 30

Assisted Solution

by:Marco Gasi
Marco Gasi earned 50 total points
Comment Utility
Do you have set error_reporting set to E_ALL? if not, do it. I also see that you include the file within short php tag: are you sure they are supported in your server? Sorry for stupid questions, but we have few elements to think about.

Anyway I suggest to put at the top of your file

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
include "../textblocks.php";
...

Open in new window

0
 

Author Comment

by:DMTrump
Comment Utility
Well, with the E_ALL set I get dozens and dozens of notices from some other includes in the index file and finally at the end of the page full of notices, still get the undefined function error
0
 

Author Comment

by:DMTrump
Comment Utility
I checked my phpinfo file and yes - short_open_tags is ON
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
What version of PHP was the old site and what is the new site?
What is on the line where the error occurs?
0
 

Author Comment

by:DMTrump
Comment Utility
BTW the notices are all from other included files and are of the type:

Notice: Use of undefined constant description - assumed 'description' in /home/aski/public_html/line/structure.inc on line 230

The web pages I'm trying to display are very old built entirely with tables (yuck!) and in a visual style that went out of fashion 15 years ago.
0
 

Author Comment

by:DMTrump
Comment Utility
My server is running 5.3.23 (I think that's the latest stable.  I don't know about the old server - I don't have direct access to it - I'll see if I can find out.
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
How are the variables declared? In a define block or just by setting a var.
Could be they are setting $description and then using description - but on the old site they had notices turned off so they never knew about it and PHP just guesses what you mean.
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
If you can attach some of the pages and relevant includes it might make it easier to see where it is going wrong.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
Comment Utility
The outpouring of notices is probably because the original programmer wrote array indexes like this:

$x = $arr[ndx];

The absence of the quotes causes PHP to look for a defined constant first.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12241-Quotation-Marks-in-PHP.html
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Curious... Can you put another "include()" file in the same directory and find it in a main PHP script?
0
 

Author Comment

by:DMTrump
Comment Utility
The line that produces the error is

          table_top("Products sorted by " . $type );

and is inside a php <? ... ?> section

inside textblocks.php, the function is defined:

function table_top($title) {
      echo
      (
            "<TABLE CELLSPACING=\"0\" CELLPADDING=\"4\" BORDER=\"0\" WIDTH=\"600\">\n".
                    "<TR VALIGN=\"BOTTOM\">\n".
                        "<TD WIDTH=\"16\">&nbsp;</TD>\n".
                        "<TD ALIGN=\"CENTER\" COLSPAN=\"2\">\n".
                              "<B>$title</B>\n".
                        "</TD>\n".
                        "<TD WIDTH=\"16\">&nbsp;</TD>\n".
                        "<TD WIDTH=\"16\">&nbsp;</TD>\n".
                   "</TR>\n"
      );
}
0
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 50 total points
Comment Utility
It could be that the old server had 'register_globals' ON which means that $_POST and $HTTP_POST_VARS (and other form variables) used to be automatically converted to regular variables.  'register_globals' are deprecated in PHP 5.3 and removed in PHP 5.4 because they were such problem.  If the code really is 15 years old, there is a good chance that it must be re-written.  A lot of things in PHP have changed since then.

http://us2.php.net/manual/en/ini.core.php#ini.register-globals
0
 

Author Comment

by:DMTrump
Comment Utility
If I comment out that call, other function calls later in the page produce the same error.

If I paste the contents of textblocks.php in in place of the include statement, everything works fine, but there are dozens of files where I'd need to do that - I'm hoping I don't have to.  In addition there are other includes that won't work either unless I can solve this problem.
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Is that file included before the function is called - I would assume so if it worked on the old server.  As is there is nothing wrong with the code.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Good point, Dave.  A couple of the dangerous things in old PHP scripts are Register Globals and Magic Quotes.

If you can determine the original version of PHP that was running, you might want to look over the migration guides.
http://php.net/manual/en/migration5.php
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 58

Expert Comment

by:Gary
Comment Utility
And that textblocks.php file is in the folder above the page calling the function?
Is it possibly loading a different file?
0
 

Author Comment

by:DMTrump
Comment Utility
Yes, of course it needs to be re-written  but of course I don't want to have to do that - because in plain words, the site is UGLY and reeks of "old fashioned" and I'm starting a whole new site for them - but meanwhile - they need to move from their old server because it is going to be shut down soon.  This move is just a temporary measure.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
And there is no message at all from the include() statement?
http://php.net/manual/en/function.include.php

You might want to scan the code for error_reporting to see if messages are being suppressed in different places.  Likewise for the @ symbol.
0
 

Author Comment

by:DMTrump
Comment Utility
I've established that the proper textblocks.php file IS being loaded  It's the only file of that name and when I change the "include" to "require" I don't get a "not found" error.  It's just that the functions in that file show up as undefined in the calling file.
0
 

Author Comment

by:DMTrump
Comment Utility
Yes, the file is being included before the functions are called.
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Can you attach the page and include.

The include file is not inside an IF statement or some other conditional?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Do you have other working PHP script directories on the same server?
0
 

Author Comment

by:DMTrump
Comment Utility
No, its not inside a conditional

I'm beginning to think I better beg the old host company to keep that site up for a while so I can get something modern in place for the client on my own server.  

I thought perhaps this was just a DOH! problem but it seems to be stumping experts way above my level of knowledge.  BG
0
 

Author Comment

by:DMTrump
Comment Utility
Do I have other working PHP script directories on the same server?

I have a dozen or so sites of my own or written by me for other clients on my server all running lots of PHP that I wrote or "borrowed"  This is the first time I've seen anything like this.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Yeah, it certainly sounds strange, especially if there are other working sites on the same server.  Have you checked the phpinfo() output from the old server and compared it to the new server?  Maybe there are some configuration settings in conflict.  PHP has about a thousand configuration settings that can screw things up.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Try this... Put these scripts into the root directory of the web site.  Expected output:

Hello from the main PHP scriptHello from the included PHP file
Here is a Notice message
Notice: Undefined variable: y in /home/websitet/public_html/RAY_temp_dmtrump_included.php on line 5
array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }

Then change the location of the included script to be the same location as the failing script, and change the include() statement to be the same as the failing script.  It would be interesting to see if this simple test can create the same error.  That would suggest something wrong with the paths.

Are you using the .php extension for all of the scripts?

<?php // RAY_temp_dmtrump.php

error_reporting(E_ALL);
ini_set('display_errors', TRUE);

echo "Hello from the main PHP script";

include('RAY_temp_dmtrump_included.php');

$thing = array( 1, 2, 3 );
dmtrump($thing);

Open in new window

<?php // RAY_temp_dmtrump_included.php

echo "Hello from the included PHP file";
echo "<br>Here is a Notice message";
$x = $y;

function dmtrump($var)
{
    var_dump($var);
}

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Just add an echo "Hello"; in the include file and see if it gets printed. Just to prove 100% that it really is being included.

Is the ../ pushing out of the web root and you have open_basedir on
0
 

Accepted Solution

by:
DMTrump earned 0 total points
Comment Utility
Okay - the problem is solved and I'm going to split the points according to the number of comments - that's the only thing I think is fair because although I learned a lot from the exchange, none of the "SOLUTIONS" solved the problem.

I think the whole issue perhaps was caused by a spurious illegal character in the original
textblocks.php file.  Someday if I have the time I may do a hex analysis to see if I can find out where.

What I did that solved the problem was in running a stub test I copied the functions one by one into a new file called testblocks.php and created a stub from the original index file that omitted most of the content and just called the new, renamed include.  It worked each time as I pasted in each function one after the other.  With all the functions in place the stub still worked.  Then I changed the call in the original index file to include testblocks.php and everything worked.  Then I removed textblocks.php from the server and renamed testblocks.php to textblocks.php, corrected the include statements and all is well!

Sheesh!  what a weird thing.  The problem may have been caused because I got the site code in a zip file packed up by the folks at the old host.  Perhaps there was some error induced by that.

Anyway, thanks to all of you.  I have to leave my desk for a few minutes to pick up my youngest granddaughter from school, but I'll come back and dole out the points.

You All tried your best and the solution is one none of us expected!

Thanks,

David Trump
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Just select your own comment as the answer.
Maybe a page encoding problem (e.g. with BOM) or similar.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Yeah, some text editors actually add a Byte-Order Mark to files, and that's totally unnecessary with UTF-8.  You can compare the string length of the new PHP files to the old. If the difference is 3 bytes, it's probably BOM.

Glad you're past the issue, ~Ray
0
 

Author Closing Comment

by:DMTrump
Comment Utility
Each contributer provided me with new knowledge and strengthened my understanding - and gave me useful ideas for solving future problems.  I've awarded points to everyone and I appreciate the help.  In the end I found the problem myself but everyone helped.

If anyone else experiences this problem I advise taking a close look for illegal characters!

Thanks to everyone who chimed in
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now