PHP code not executing, but shown in HTML source code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

loweauto
Forum Newbie
Posts: 20
Joined: Sat Apr 10, 2010 2:23 pm

PHP code not executing, but shown in HTML source code

Post by loweauto »

I'm trying to include the contact-script.php file on this page of a site: http://www.karmera.de/index.php?go=shop

But it doesn't do anything, and instead, shows the PHP include in the source code. Do you have any ideas as to why?

To give you a better understanding of my site, index.php looks like this:

Code: Select all

<?php 

$go = "index" ; 

if ($_GET["go"])
        $go = $_GET["go"] ;

$body = "html/{$go}.html" ;
$head = "html/_{$go}.html" ;

if (!file_exists($body)) {      
        $body = "html/index.html" ; 
}

if (!file_exists($head)) {
        $head = "html/_index.html" ;
}

$CONTENT = implode('', file($body)) ;
$HEAD = implode('', file($head)) ;
        
include "layout.html" ;

?>
The index.php file opens an include to include the content depending on "index.php?go=contact", for example, and the contact.html resides in root/html/contact.html, and it's just the sheer content (in a div). It then includes a http://www.karmera.de/layout.html file, where there's the design, etc.

As you noticed, there are both .html and .php files, but the thing is - I don't think that should be a problem, as the master file is a .php - so code should execute. To be safe, I even modified htaccess:

Code: Select all

RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html
What do you think the problem is, and how can I solve it?

Also, any other advice is appreciated!

Thank you for your time.

P.S. When you go to: http://www.karmera.de/html/shop.html it gives the following errors:
Warning: include(./contact-app/common.php) [function.include]: failed to open stream: No such file or directory in /www/htdocs/w0086c88/karmera/contact-script.php on line 8

Warning: include(./contact-app/common.php) [function.include]: failed to open stream: No such file or directory in /www/htdocs/w0086c88/karmera/contact-script.php on line 8

Warning: include() [function.include]: Failed opening './contact-app/common.php' for inclusion (include_path='.:/usr/share/php:..') in /www/htdocs/w0086c88/karmera/contact-script.php on line 8
marty pain
Forum Contributor
Posts: 105
Joined: Thu Jun 11, 2009 5:32 am
Location: Essex

Re: PHP code not executing, but shown in HTML source code

Post by marty pain »

To be honest, your example is not very clear, at least not to me.

You are trying to include a HTML file that contains PHP?

Basically, if you want to include a PHP file (that can of course hold HTML)into your page, do this:

Code: Select all

<?php
  //location of files and default page
  define('FILE_LOCATION', 'html/');
  define('DEFAULT_PAGE','html/default.php');

  if(isset($_GET['page'])) {
    $page = $_GET['page'];
    $location = FILE_LOCATION.$page

    if(file_exists($location)) {
      include($location);
    }
    else {
      include(DEFAULT_PAGE);
    }
  }
  else {
    include(DEFAULT_PAGE);
  }
?>
if you want to include another HTML page do this:

Code: Select all

<?php
  //location of files and default page
  define('FILE_LOCATION', 'html/');
  define('DEFAULT_PAGE','html/default.html');

  if(isset($_GET['page'])) {
    $page = $_GET['page'];
    $location = FILE_LOCATION.$page

    if(file_exists($location)) {
      $pageHTML = get_file_contents($location);
    }
    else {
      $pageHTML = get_file_contents(DEFAULT_PAGE);
    }
  }
  else {
    $pageHTML = get_file_contents(DEFAULT_PAGE);
  }

  echo $pageHTML;
?>
Hopefully these ruff examples are helpful.
loweauto
Forum Newbie
Posts: 20
Joined: Sat Apr 10, 2010 2:23 pm

Re: PHP code not executing, but shown in HTML source code

Post by loweauto »

marty pain wrote:To be honest, your example is not very clear, at least not to me.

You are trying to include a HTML file that contains PHP?

Basically, if you want to include a PHP file (that can of course hold HTML)into your page, do this:

Code: Select all

<?php
  //location of files and default page
  define('FILE_LOCATION', 'html/');
  define('DEFAULT_PAGE','html/default.php');

  if(isset($_GET['page'])) {
    $page = $_GET['page'];
    $location = FILE_LOCATION.$page

    if(file_exists($location)) {
      include($location);
    }
    else {
      include(DEFAULT_PAGE);
    }
  }
  else {
    include(DEFAULT_PAGE);
  }
?>
if you want to include another HTML page do this:

Code: Select all

<?php
  //location of files and default page
  define('FILE_LOCATION', 'html/');
  define('DEFAULT_PAGE','html/default.html');

  if(isset($_GET['page'])) {
    $page = $_GET['page'];
    $location = FILE_LOCATION.$page

    if(file_exists($location)) {
      $pageHTML = get_file_contents($location);
    }
    else {
      $pageHTML = get_file_contents(DEFAULT_PAGE);
    }
  }
  else {
    $pageHTML = get_file_contents(DEFAULT_PAGE);
  }

  echo $pageHTML;
?>
Hopefully these ruff examples are helpful.
thanks for your help!

i'll look into your examples, but the main problem is that php code simply doesn't work on my site.. in the way i've designed it, that is
if you take a look here: http://www.karmera.de/index.php?go=shop

there's meant to be php code after the TBD, if you look in the source code of the page - it shows up there! so something isnt working.. not sure why.. maybe its because of the way my site is laid out? (see my first post for that)

let me know what u think, thanks!
roders
Forum Commoner
Posts: 68
Joined: Tue Oct 20, 2009 9:29 am

Re: PHP code not executing, but shown in HTML source code

Post by roders »

Normally if you're script is calling the correct file it should work. Are you sure that the the file is not being called as withing layount.html? I can't see anything that can go wrong. You might want to try a few static pages like for example where its calling the file just add include("contact-script.php"); see if it changes things.
loweauto
Forum Newbie
Posts: 20
Joined: Sat Apr 10, 2010 2:23 pm

Re: PHP code not executing, but shown in HTML source code

Post by loweauto »

roders wrote:Normally if you're script is calling the correct file it should work. Are you sure that the the file is not being called as withing layount.html? I can't see anything that can go wrong. You might want to try a few static pages like for example where its calling the file just add include("contact-script.php"); see if it changes things.
hi,
actually yeah, i made a seperate test page, without my design: http://www.karmera.de/test2.php

and it works.. so the problem must be with my design.. so something about how it's made, e.g.:

[text]/index.php[/text] calls [text]/html/index.html[/text] and includes [text]/layout.html[/text]

(the code of index.php is in my previous post..)
roders
Forum Commoner
Posts: 68
Joined: Tue Oct 20, 2009 9:29 am

Re: PHP code not executing, but shown in HTML source code

Post by roders »

In which 1 is contact-script.php being called??
loweauto
Forum Newbie
Posts: 20
Joined: Sat Apr 10, 2010 2:23 pm

Re: PHP code not executing, but shown in HTML source code

Post by loweauto »

roders wrote:In which 1 is contact-script.php being called??
that's inside: /html/index.html

and i have tried renaming it .php (actually all files to .php) - and that didn't work either
roders
Forum Commoner
Posts: 68
Joined: Tue Oct 20, 2009 9:29 am

Re: PHP code not executing, but shown in HTML source code

Post by roders »

In your .htaccess at this as well
[text]Addhandler application/x-httpd-php .html .php .htm [/text]
And where the file is being called you need to call it as such

Code: Select all

<!–#include virtual=”html/fileyouwanttocall.php” –>
loweauto
Forum Newbie
Posts: 20
Joined: Sat Apr 10, 2010 2:23 pm

Re: PHP code not executing, but shown in HTML source code

Post by loweauto »

roders wrote:In your .htaccess at this as well
[text]Addhandler application/x-httpd-php .html .php .htm [/text]
And where the file is being called you need to call it as such

Code: Select all

<!–#include virtual=”html/fileyouwanttocall.php” –>
i added the .php to htaccess, and put: <!–#include virtual="/contact-script.php" -> in the html/shop.html file, but nothing works - now even this shows in the source code

(should i have put <? tags around it?)
roders
Forum Commoner
Posts: 68
Joined: Tue Oct 20, 2009 9:29 am

Re: PHP code not executing, but shown in HTML source code

Post by roders »

Sound like a config problem to me. Does your server have htaccess enable?
loweauto
Forum Newbie
Posts: 20
Joined: Sat Apr 10, 2010 2:23 pm

Re: PHP code not executing, but shown in HTML source code

Post by loweauto »

roders wrote:Sound like a config problem to me. Does your server have htaccess enable?
yea, htaccess works..
roders
Forum Commoner
Posts: 68
Joined: Tue Oct 20, 2009 9:29 am

Re: PHP code not executing, but shown in HTML source code

Post by roders »

where is you htaccess file saved??
loweauto
Forum Newbie
Posts: 20
Joined: Sat Apr 10, 2010 2:23 pm

Re: PHP code not executing, but shown in HTML source code

Post by loweauto »

roders wrote:where is you htaccess file saved??
lol, in the root dir ;) don't worry
roders
Forum Commoner
Posts: 68
Joined: Tue Oct 20, 2009 9:29 am

Re: PHP code not executing, but shown in HTML source code

Post by roders »

did u fix the problem?
loweauto
Forum Newbie
Posts: 20
Joined: Sat Apr 10, 2010 2:23 pm

Re: PHP code not executing, but shown in HTML source code

Post by loweauto »

roders wrote:did u fix the problem?
yes, but by re-doing the html design of the site
Post Reply