3

I am using LAMP stack to develop a PHP application. I am using require_once to include class files. I need to use the functions in those class files in more than one PHP page. So, I am including those class files in all the required PHP pages using require_once. But, if I include those class files in more than one page, the PHP file goes blank. It displays nothing. View source also displays nothing.

Files: test.php, process.php and class.test.php

test.php has

<?php
    session_start();
    require_once 'classes/class.test.php';

    .
    Few more classes
    .
    .  


?>

<html>   
    <form name = "myForm" method="POST" action="process.php">
       <input type = "text" name="username" value=""/>
       <input type = "submit" value="Submit" />
    </form>   
</html>

process.php

<?php 
    session_start();
    require_once 'classes/class.test.php';

    $obj_test = new test();
    $obj_test->test();
?>

class.test.php

<?php
    session_start();
    require_once 'class.misc.php';
    require_once 'config.php'; //DB connection details

    function test()
    {
        $obj_misc = new misc();
        $id = $obj_misc->random_ID();
        $username = $_POST['username'];             

        $query = "INSERT INTO test_table VALUES ('$id','$username',NOW());
        mysql_query($query);
    }
?>

Now, it returns a blank page. If I comment out the require_once in process.php, the test.php page displays the form, but on submitting the form the process.php throws an error "class test not found".

I am struggling with this problem for the past 2 weeks. :( It was working fine before that. I don't understand what went wrong. Please help.

16
  • Maybe you should check the paths to the files where the classes are. They change depending on the location of the files that require them Commented Nov 22, 2012 at 7:25
  • Hi @sarghau, welcome to SO. +1 for a well formatted question. Now, why do you have two test.php snippets? Could the problem be you're calling the wrong test.php on accident? Commented Nov 22, 2012 at 7:26
  • Have you checked your error log? I normally suggest leaving out the closing ?>, as it can lead to headers being sent prematurely. Commented Nov 22, 2012 at 7:26
  • How commenting something in process.php can affect test.php, when you say that doesn't interact with test.php? You are misexplaining something Commented Nov 22, 2012 at 7:27
  • Enable php errors so you dont end up with blank page next time. Error will explain what is wrong and you will not need to post question in SO. Commented Nov 22, 2012 at 7:30

5 Answers 5

5

You have an error in the PHP code for process.php; you are missing a semicolon:

require_once 'classes/class.test.php'

should be:

require_once 'classes/class.test.php';

If that doesn't fix it, then there is probably some other error somewhere in your code. Without access to the full source, we won't be able to do much.

For future reference, if a page goes blank, there is usually a problem with the PHP source code (ie, some type of interpreting error). As part of good debugging tactics, look into display_errors and error_reporting

Sign up to request clarification or add additional context in comments.

6 Comments

Corrected it. It's a huge code involving lots of classes and other php files. So, I posted only the fragment where it throws an error.
in class.test.php $query = "INSERT INTO test_table VALUES ('$id','$username',NOW()); mysql_query($query); Is missing the other quote - " - at the end of the string. does that help?
@cegfault It was a typo ! But that's not the problem. If there is a syntax error, the IDE which I use will point.
Try running php -l on all the files, that will show the syntax errors (your IDE seems to have failed here...)
@BotondBalázs - That was a copy paste mistake. Not IDE's.
|
3

Try out with

error_reporting(E_ALL);

Comments

1

Sounds like your error reporting is turned off. You should check your error logs to see what exception is being thrown when it's failing silently (that will give you a little more insight).

Additionally, you may want to add this at the top of your process script:

error_reporting(E_ALL);

4 Comments

Yeah I did that. But still blank.
try to also use this with above code - ini_set('display_errors', 1);
check php.ini file. look for 'error'. It is probably turned off
@sarghau There should be display_errors and error_reporting parameters.
0

Require once is a function ...

try adding the parens ...

 require_once( 'classes/class.test.php' );    

2 Comments

require_once, require, include_once, and include are all like echo - you don't need parenthesis. The PHP Manual even has examples without parenthesis...
The problem was not with the code. I ran the code in my machine. It worked. There was some problem in the server which was resolved by the provider.
0

looks like you are missing a closing qoute on the sql query

$query = "INSERT INTO test_table VALUES ('$id','$username',NOW());
mysql_query($query);

should be

$query = "INSERT INTO test_table VALUES ('$id','$username',NOW())";
mysql_query($query);

1 Comment

It was a typo ! But that's not the problem. If there is a syntax error, the IDE which I use will point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.