Quick PHP help needed...

Started by Gregjazz, Wed 03/05/2006 06:50:51

Previous topic - Next topic

Gregjazz

Here's the deal:

The content of my website is stored in a text file and uses PHP to display the text file in the webpage. I can use HTML code in the text file and it displays that all correctly and everything.

Now here's the deal, I want to be able to put PHP code within the text file.

Even if inside the text file I put a simple:

<?PHP
echo "test.";
?>

It doesn't run the PHP code.

How can I make this work?

Eigen

How are you displaying the text file? It should work fine ..


-Eigen

SSH

Check your hosting's instructions on PHP. Some insist on certain file extensions, some insist on certain directories, some dont have PHP support...
12

fred

#3
Well, I'm new to PHP, but just yesterday had a similar (I think) problem. PHP is enabled on my server, but in order for it to execute, it needs to be in files with the .php extension. Now I didn't want to rename all my .html files to .php just in order to enable the php nested inside them, and I found this solution somewhere on the web:

1. Creat a txt-file with the following content:

Code: ags

AddType application/x-httpd-php .htm .html


2. Copy it to your server (the public root probably) and rename it to ".htaccess"

Ths enables php within the .htm and .html files without having to rename them (and renaming all links on the site - which might be a pain). I don't know, but maybe you could add ".txt" to the code-line above and have php work inside the .txt-files you are reading from? I haven't tried it myself, but it's so easy I think it's maybe worth checking out :-)

AGA

I don't understand why you would want to use a text file for PHP code anyway...

However, yes, what fred suggested should work, assuming your hosting is set up to allow overrides.

Gregjazz

Here's a more detailed description:

My webpage is all based on an index.php

All the sections are built around it, like index.php?page=links, for example.

Now I have it set up so that the content for these pages are stored in text files with the same name as the page, so if you go to index.php?page=links, it will attempt to read links.txt for the content to put on the page. When I log into admin, all this content is displayed in a big text box, so I can change anything on the website right there.

Now in a particular case, I need to have PHP within those text files, or some way of running PHP code from the text files.

I tried putting that line of code in my .htaccess file, and it didn't help. I actually uploaded this website on my other webspace which I already have a custom .htaccess thing set up, and it got the same results.

So yes, maybe it is how I am displaying the text file on the webpage--this is basically what I've got:

Code: ags

if (file_exists("pagedata/" . $_GET['page'] . ".txt")) {
  $handle = fopen("pagedata/" . $_GET['page'] . ".txt", "r");
  if($handle) {
    while (!feof($handle)) {
      $buffer = fgets($handle);
      echo $buffer;
      }
    fclose($handle);
    }
  }
else {
  echo "This section is temporarily blank.";
  }

SSH

Well, "echo" is not going to parse PHP for you. Does PHP have some kind of "include" command, maybe?
12

Mr Jake

It'd be much easier to make all your pages seperate .php files and use:

include($pagevariable . ".php");


AGA

Try changing:

Code: ags
$buffer = fgets($handle);
      echo $buffer;


to:

Code: ags

include($handle);


Or something along those lines. There's no need for them to be .php files. They could be called .thisreallyisntphp for all the difference it'll make.

The reason it doesn't work properly now is because all it's doing is echoing the contents of the files, not executing them. Include will actually run the code that's being gotten.

Ishmael

I've just gone with includes, just calling the filename if it's .txt (which have some html code in them) or adding .php to the call name if it's a file with php code in it. Works flawlessly...
I used to make games but then I took an IRC in the knee.

<Calin> Ishmael looks awesome all the time
\( Ö)/ ¬(Ö ) | Ja minähän en keskellä kirkasta päivää lähden minnekään juoksentelemaan ilman housuja.

juncmodule

#10
require_once 'textfile.txt';

will most likely solve your problem.

Include is the right idea but will actually include the contents of the text file I believe. I'm assuming that you have the actual content of the pages declared as variables within the text file. If this is the case then require_once should do the trick. I once had my website set up this way. There is one other alternative to require_once that will work, I can't remember what it is.

Hope that helps.

oh...I just looked it up and require and include are nearly the same, except in how they handle errors. Just ignore me.

later,
-junc

Oneway

Replace your previous code with this.  This code uses include as suggested above and added a check for the actual existence of $_GET['page']. Depending on the server settings, an error or warning may be printed if you make a call to a nonexistent array-item.

Code: ags

if(isSet($_GET['page']) {
  if (file_exists("pagedata/" . $_GET['page'] . ".txt")) {
    $file = "pagedata/" . $_GET['page'] . ".txt";
    include($file);
  }
  else {
    echo "This section is temporarily blank.";
  }
}
else echo "No page specified.";


Good luck.
Almost intentionally left blank.

Gregjazz

Quote from: Oneway on Wed 03/05/2006 16:40:00
Replace your previous code with this.  This code uses include as suggested above and added a check for the actual existence of $_GET['page']. Depending on the server settings, an error or warning may be printed if you make a call to a nonexistent array-item.

Code: ags

if(isSet($_GET['page']) {
  if (file_exists("pagedata/" . $_GET['page'] . ".txt")) {
    $file = "pagedata/" . $_GET['page'] . ".txt";
    include($file);
  }
  else {
    echo "This section is temporarily blank.";
  }
}
else echo "No page specified.";


Good luck.

And now it works!!!

Thanks! Using "include" actually makes much, much more sense, now that I think of it. Thank you for your help!

SMF spam blocked by CleanTalk