Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

When I include my PHP code in an HTML file, nothing happens any more

Status
Not open for further replies.

pmonett

Programmer
Sep 5, 2002
2,632
FR
Hello everyone,

I'm trying to get up to speed in PHP and I am encountering a problem with my php code.
On my webserver, I have a php code file that reads the contents of a parameter text file, parses it into table variables, and uses that to display a dynamic menu :

Code:
Contents of start.php :
<?php
	$options[0]="";
	$files[0]="";
	$arraynum=0;
	
	function getoptions(){
	global $options, $files, $arraynum;
		$input="";
		$ficlist=file("pagelist.dat");
		foreach($ficlist as $input){
			$tab=explode("-",$input);
			$options[$arraynum]=$tab[0];
			if($arraynum==0){
				$files[$arraynum]="home.php";
			}else{
			$files[$arraynum]=$tab[1].".php";
			}
			$arraynum++;
		}
	}

	getoptions();
	
	echo "<table>";
	echo "<tr><td colspan='2' style='width:796px;background:#3c6a50;'><br/><br/></td></tr>";
	echo "<tr><td style='width:200px;' valign='top'>";
	echo "<ul id='menu'>";
	$iter=0;
	for($iter=0;$iter<$arraynum;$iter++){
	    echo "<li class='selected'><a href='".$files[$iter]."' >".$options[$iter]."</a></li>";
	}
	echo "</ul>";
?>

This code works, I have thoroughly debugged it.

My problem is as follows : when I include this code into a regular HTML file, all of a sudden it doesn't work any more.

This is my HTML code :
Code:
<html>
<head>
<title>Mini projet NFA053</title>
<link rel="stylesheet" type="text/css" href="./style.css" title="fichier1">
</head>
<body>
<!--#include FILE="start.php" --> 
</body>
</html>

What am I doing wrong ?

I've got nothing to hide, and I'd very much like to keep that away from prying eyes.
 
<!--#include FILE="start.php" -->
This is not a valid PHP include.

Second PHP will not get parsed and executed in files that don't have a PHP extension unless the server is specificially set to do so.

So:
1. to Include a PHP file you must use a PHP include.
Code:
<html>
<head>
<title>Mini projet NFA053</title>
<link rel="stylesheet" type="text/css" href="./style.css" title="fichier1">
</head>
<body>
[red]
<?PHP
include("start.php");
?>
[/red]
</body>
</html>

2. Unless the server is configured to parse files with other extensions any file that is to have PHP code executed must have a PHP extension: myfile.php

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thank you for your answer.
I thought I needed to do an HTML include. My mistake.

So I corrected the include, but unfortunately there is no change.
Maybe I must reconfigure EasyPHP ?

I've got nothing to hide, and I'd very much like to keep that away from prying eyes.
 
Again, does the page you are including your PHP file into have a php extension?

If it doesn't, then the PHP include will not get executed. you'll likely just see the include displayed like regular text.

Also how are you opening the page? Like this: ?





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
The file included is indeed named "start.php". Isn't that the extension ?

And for the execution of start.php, yes, that is exactly how I debugged it manually.

Same for the home.html file.

Pascal.

I've got nothing to hide, and I'd very much like to keep that away from prying eyes.
 
Same for the home.html file.

I assume home.html is the file calling start.php correct?

If that's so, you'll need to rename home.html to home.php, so your include gets executed.

It won;t affect your HTML in any way, but will let the PHP be included and run.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
That is indeed the solution - home.php and everything works.

Curious, the simple rename takes the webserver in an entirely different direction I guess.

Thank you very much for your efforts.

Pascal.

I've got nothing to hide, and I'd very much like to keep that away from prying eyes.
 
Curious, the simple rename takes the webserver in an entirely different direction I guess.

Correct, the extension .PHP is what tells the server that the file needs to be parsed by the PHP interpreter. Otherwise it just delivers the file straight to the browser instead and the PHP code does not run.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top