0

I have a php page where I have the code posted below. My main problem was that in the header there was a line added at the top. This line was not added at the other .html files of my page that used exactly the same code. The only difference was the extension (html and php).

I tried "inspect element" feature to see what is going on. And I can see a different code in the <head> and the <body>.

My page code :

<!DOCTYPE html>

<?php
session_start();
include("conf.php");
$current="gallery.php"
?>

<html lang="en">
     <head>
     <title>.......</title>
     <meta charset="utf-8">
     <link  rel="stylesheet" media="screen" href="......css">

        <!-- JS -->
     <script src=".................."></script>

     </head>
<body>
<!--==============================header=================================-->

<header>
  <div class="container_12">
    <div class="grid_12">
        <h1><img src="images/logo.png">  </h1>
        <div class="menu_block">
          <nav>
            <ul class="sf-menu">
           <li><a href="index.html">ΑΡΧΙΚΗ</a> </li>
                    ...
                    ...
                   <li><a href="index-4.html">ΕΠΙΚΟΙΝΩΝΙΑ</a> </li>
            </ul>
          </nav>
          <div class="clear"></div>
        </div>
       </div>
  </div>
</header>

... 
...
</body>

What causes that ? I have the same html code (and css) in the other pages (different extension though) like this one : HTML version

deceze
  • 510,633
  • 85
  • 743
  • 889
Datacrawler
  • 2,780
  • 8
  • 46
  • 100

1 Answers1

2

Your webserver is set up to only interpret files with .php extension as PHP scripts. Whenever you give the page the .html extension, it will just send the file to the browser to interpret, without parsing it beforehand. So everywhere you use php code you should give the file the .php extension (or change the webserver's configuration, but I wouldn't do that now).

giorgio
  • 10,111
  • 2
  • 28
  • 41
  • Isn't possible to change my code in order to make it work ? I intent to upload my page in another server in some days. Is it worthy to change the code ? Or will it possibly work in the new webserver ? – Datacrawler Oct 28 '13 at 11:09
  • The code isn't the problem, the file extension is. And no, changing the code will have no effect at all. You should either change the file extension or the server configuration. And most webservers are setup by default to interpret .html files as HTML, and .php as PHP, which shouldn't surprise you. So if you want to be safe, and want the easy solution: rename your files! – giorgio Oct 28 '13 at 11:28
  • If i rename my .php to .html , it won't work as it has to. It has content taken from a database. – Datacrawler Oct 28 '13 at 11:44
  • if you look in the source of the link you've added to your question, you will see pure php code in the source. You want that to beinterpreted though, which your server will only do if the extension has the .php extension. I would advise you to take some time to learn about the [difference](http://programmers.stackexchange.com/questions/171203/what-are-the-difference-between-server-side-and-client-side-programming) between [Client side scripting](http://en.wikipedia.org/wiki/Client-side_scripting) and [Server side scripting](http://en.wikipedia.org/wiki/Server-side_scripting) – giorgio Oct 28 '13 at 12:06
  • It worked. I converted html files to php. I removed all the words (by finding them with ctrl+f) that contained "html". It worked for this page : [one](http://aasiskos.webpages.auth.gr/COSMOSET/gallery.php) but not for this one : [two](http://aasiskos.webpages.auth.gr/COSMOSET/index.php) . How is that possible ? Here is the php code of the page two : [JSFIDDLE](http://jsfiddle.net/ZjJvs/) – Datacrawler Oct 28 '13 at 19:51
  • could you define what "not working" means? Looks pretty ok here actually. Only thing I can advise you is to remove whitespace at the beginning of the file. – giorgio Oct 29 '13 at 09:45
  • Now it is ok. When I sent you the message no. I finally moved the at the start of my code before the "HTML DOCTYPE" thing etc. Was that the problem ? – Datacrawler Oct 29 '13 at 10:37
  • 1
    yes that was actually the problem :) The reason is that the `start_session()` function needs to add headers to the response (cookies) so it has to be in front of ALL output. Please take not that whitespace (space, tab, newline, whatever) also results in output and thus sending headers to the browser. – giorgio Oct 29 '13 at 10:41