Adventure Game Studio

Community => General Discussion => Topic started by: Gregjazz on Wed 03/05/2006 06:50:51

Title: Quick PHP help needed...
Post by: Gregjazz on Wed 03/05/2006 06:50:51
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?
Title: Re: Quick PHP help needed...
Post by: Eigen on Wed 03/05/2006 07:58:18
How are you displaying the text file? It should work fine ..


-Eigen
Title: Re: Quick PHP help needed...
Post by: SSH on Wed 03/05/2006 08:22:01
Check your hosting's instructions on PHP. Some insist on certain file extensions, some insist on certain directories, some dont have PHP support...
Title: Re: Quick PHP help needed...
Post by: fred on Wed 03/05/2006 09:03:04
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:


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 :-)
Title: Re: Quick PHP help needed...
Post by: AGA on Wed 03/05/2006 09:09:40
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.
Title: Re: Quick PHP help needed...
Post by: Gregjazz on Wed 03/05/2006 09:30:08
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:


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.";
  }
Title: Re: Quick PHP help needed...
Post by: SSH on Wed 03/05/2006 09:49:03
Well, "echo" is not going to parse PHP for you. Does PHP have some kind of "include" command, maybe?
Title: Re: Quick PHP help needed...
Post by: Mr Jake on Wed 03/05/2006 10:53:58
It'd be much easier to make all your pages seperate .php files and use:

include($pagevariable . ".php");

Title: Re: Quick PHP help needed...
Post by: AGA on Wed 03/05/2006 10:59:34
Try changing:

$buffer = fgets($handle);
      echo $buffer;


to:


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.
Title: Re: Quick PHP help needed...
Post by: Ishmael on Wed 03/05/2006 12:57:17
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...
Title: Re: Quick PHP help needed...
Post by: juncmodule on Wed 03/05/2006 13:25:45
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
Title: Re: Quick PHP help needed...
Post by: 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.


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.
Title: Re: Quick PHP help needed...
Post by: Gregjazz on Wed 03/05/2006 17:15:09
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.


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!