Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

PHP Custom Error Handler

Hello again, need some more help with my school project.

Basically I have a text area that the use can type PHP code into. On submit it gets saved out to a text file and then reloaded back into an iframe so the use can see what the PHP code does. here is the process page.

<?php

if(isset($_POST['submit'])){ $area=$_REQUEST['code']; $fd=fopen("test.php","w+"); fwrite($fd,$area); fclose($fd); $file_contents=file_get_contents("test.php");

}

However I need to include customer error messages. For example if the user doesn't type the php tags, I need it to echo back "dont forget to include php tags" insted of the normal php error.

I can hardly code as it is. Does anyone have any examples I can play around with or point me in the way of a good tutorial?

Thank You Carl

8 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

(Before I answer the question, I want to say that you need to be very careful when doing something like this. I don't know the exact context, but you are letting people you may not even know execute code on your server. The thought of what someone malicious might be able to do to your server terrifies me. Hopefully you are running this in some kind of secure context.)

It's tricky to test for syntax errors in PHP code using PHP. (I have the same trouble with code challenges here at Treehouse; if someone enters invalid PHP in a code challenge, there's not much I can do to provide a helpful error message.) There's a PEAR package called PHP_CodeSniffer that would probably be able to help here, but I haven't ever worked with it personally: depending on your hosting environment, you may not even be able to use it.

Thanks,

I'll take a look. Its only going to be localhost =)

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

"Its only going to be localhost"

Whew! Now I'll be able to sleep tonight. :~)

I did a little some more Google searches, and PHP used to have this function:

php_check_syntax()

I think that's more or less what you are wanting to do. It's been deprecated, but the PHP manual recommends running a commandline php -l. That could work, if you ran that from the PHP file with an exec() command.

As i'm basic building a mini code challenge interface. Wouldn't it be simpler to just check the user input like form validation?

If I'm expecting echo "hello world"; I could check for double " " and , before I past the input to be displayed? or what it be better to do the command line route?

Arr ok. playing around with php command line for the first time. Seems a better way to learn to code. So i need to write a script that

1)runs php -1, 2)prints out to a file. 3)runs some if statements that turns the errors it into plain English 4)Prints them back to the user.

] <?php

if (isset($_POST['code']))

{ $errors = array();

$code = $_POST['code'];

If (empty($code)) { $errors[] = 'You Must Enter Some Code'; }

else {

$words = array('PHP');

$words = join("|", $words); $matches = array(); if ( preg_match('/' . $words . '/i', $code, $matches) ){ echo "Words matched:"; print_r($matches); }

    }       


        if(!empty($errors)) {
            foreach ($errors as $error){
                echo $error;

            }

        }
        else {

        echo 'boom';

        }

}

?>

<form method="post" action="new.php">

<textarea id="code" name="code">


</textarea>

<input type="submit" name="submit" value="Send" id="submit"/>

</form>

Spend 12 hours on this =( So i came up with this to check for words before I printed it out. Its abit of a mess and doesnt work well.

How was a check for words that are missing instead of looking for words?

I have no idea. If anybody wants to take a look Iv uploaded my www folder.

https://docs.google.com/file/d/0BwNdBjk1PWXyd2hIeGJacmdNb2s/edit?usp=sharing

the file is interface.php

night night