PHP POST var to include

He has: 52 posts

Joined: Apr 2006

Hi again.

I am making my own layout, as I was suggested to before, and it's going great. I'm learing more than I could ever have done before, and the pride of doing it by myself is tremendous. *I'm a big boy now!*

Okay, but like every time I post here, I have a problem. I'm using a little script to pull a text file out of a lower directory, based on the post variables I get. Unfortunately, it's really not working.

Here's my code:

<?php
$article
= $_POST['articles'];
if (
$article == \"\" ) {
    echo \"The article you were looking for was not found.\";
} else {
    echo \"The if statement evaluated to false\";
    include(\"/articles/\".
$article\".txt\");
}
?>

I'm getting this error:

Quote: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in c:\appserv\www\abyss\pages\viewarticle.php on line 9

'Line 9' refers to my 'include' line.

Can you tell me why this isn't working? Also, can you find me a solution to my crafty users who plays with the POST variables and hits a wrong/invalid article (to a text file that doesn't exist)?

Thanks.

~Max

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

Not sure if this is the only thing, but you forgot a period

include("/articles/".$article".txt");

should be

include("/articles/".$article.".txt");

He has: 52 posts

Joined: Apr 2006

Works beautifully. Thanks.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

I would recommend also doing a check with file_exists before including it as well.

See http://us2.php.net/manual/en/function.file-exists.php for more info.

-Greg

He has: 52 posts

Joined: Apr 2006

Yes, after some researching, I have come to use that function, as well. Thank you.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.