Jump to content

PHP Require Problem...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
14 replies to this topic

#1
gakattack

gakattack

    Newbie

  • Members
  • Pip
  • 6 posts
So I have a script that starts off like this:
<?php
print 'test1';
require ('somefile.php');
print 'test2';

somefile.php starts off like this:
<?php
print 'test3'


When I run the main file, the output is 'test1'.... and the rest of the page is blank... Any ideas why? It worked before on the server, and my scripts also work on my localhost machine running the same version of php as the server...

I copied an old copy of the site onto the server, and it doesn't work either. It looks like a server problem. Anyone know the cause?

it's a linux server and the permissions on the files have been set

Edited by John, 04 November 2008 - 12:35 PM.


#2
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Are you sure somefile.php is in the same directory as the script?
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Please use [php] tags when posting php code. According to the php manual, if somefile.php cannot be found, the the script will halt execution. Which is what your code is doing, thus implying somefile.php cannot be found. Try using the absolute path to the file. Also keep in mind, on a linux server SomeFile.php is not the same as somefile.php (in other words, case matters)

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
To add to what John stated, "require" will cast a fatal error while using "include" will not. You used require. Since you don't see the fatal error you may have error_reporting turned off in your php.ini file. Add this line to the top of your PHP file.

error_reporting(E_ALL ^ E_NOTICE);

So your script would be:
<?php
error_reporting(E_ALL ^ E_NOTICE);

print 'test1';
require ('somefile.php');
print 'test2'; 


#5
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
To add to what Jordan stated, don't use include(). Using require() forces you to confirm the file does indeed exist, instead of just being lazy. Your problem is almost definitely that the file cannot be found.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#6
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
To add to what I, Jordan, and Xav said, in most instances, it is better to use require_once() rather than require().

#7
Guest_Jordan_*

Guest_Jordan_*
  • Guests
To add to what John, Me, Xav, John said - it is good to use include when creating a plugin or template system. This way your system will not halt. Also, just because you use require_once() doesn't mean you *should* add the same require_once('file') to every PHP file - that is duplicate code and thus bad coding methods.

#8
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
To add to what Jordan, John, me, Jordan and John said - you can eliminate the duplicate code in every file by calling it once from an external script - but that uses another require_once() again! You can therefore create a single page which then loads the common file, and then the normal page. This way you can control the page using a GET request.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#9
Guest_Jordan_*

Guest_Jordan_*
  • Guests
That doesn't make sense. You are going to require_once() the file you need in another file and then require_once() that file?

#10
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Nope.

You have a single index.php file, which can load any scripts from there. It can then load the content page(s), all from the same index.php file (for example). To control the content, a GET request can be parsed with a switch statement.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#11
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Nope.

You have a single index.php file, which can load any scripts from there. It can then load the content page(s), all from the same index.php file (for example). To control the content, a GET request can be parsed with a switch statement.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#12
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
that's exactly my cup of tea, i use this mwthod in my web application i'm making, but i have a database table with the names of the include files instead of a switch.
some pages uses the same file and there switches instead, to gather pages tightly liked together into one file, and reduce number of files, almost working as a module system.

example:
id, page, file
0, home, home.php
1, login, login.php
2, logout, login.php