Solved

Trouble calling functions in included PHP file

Posted on 2014-01-15
32
283 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
ID: 39783285
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 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 50 total points
ID: 39783293
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
ID: 39783362
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

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

Expert Comment

by:Gary
ID: 39783390
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
ID: 39783393
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
ID: 39783413
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
ID: 39783419
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
ID: 39783422
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 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 39783427
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 109

Expert Comment

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

Author Comment

by:DMTrump
ID: 39783450
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 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 50 total points
ID: 39783454
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
ID: 39783487
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
ID: 39783488
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 109

Expert Comment

by:Ray Paseur
ID: 39783504
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
 
LVL 58

Expert Comment

by:Gary
ID: 39783506
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
ID: 39783513
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 109

Expert Comment

by:Ray Paseur
ID: 39783524
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
ID: 39783527
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
ID: 39783535
Yes, the file is being included before the functions are called.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39783550
Can you attach the page and include.

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

Expert Comment

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

Author Comment

by:DMTrump
ID: 39783611
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
ID: 39783621
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 109

Expert Comment

by:Ray Paseur
ID: 39783651
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 109

Expert Comment

by:Ray Paseur
ID: 39783685
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
ID: 39783730
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
ID: 39783844
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
ID: 39783859
Just select your own comment as the answer.
Maybe a page encoding problem (e.g. with BOM) or similar.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39783871
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
ID: 39793768
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

770 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