Why "inspect element" shows different code? - php

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>ΑΡΧΙΚΗ </li>
...
...
<li>ΕΠΙΚΟΙΝΩΝΙΑ </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

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).

Related

PHP NavBar works on its own but not when included in other PHP code

I'm dealing with small problem in my PHP code, when I try to use include on my mock website, to have the same navbar on all of my pages, it simply doesn't work, however it does work when I open it on its own.
I'm very new to coding so please bare with me and my complete lack of proper terminology.
This is my index.php :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>T-drip Home</title>
<link rel="stylesheet" href="css/home.css">
<?php include('inc/nav.php') ?>
</head>
<body>
<div class="fadeIn">
<img src="images/home-white-tee-1.jpg" alt="Fancy 1st Tee">
<img src="images/home-white-tee-2.jpg" alt="Provocative 2nd Tee">
<img src="images/home-white-tee-3.jpg" alt="Classy 3rd Tee">
</div>
<section class="prod">
<h3 class="prodCat">Always Popular</h3>
<div class="prodCont">
<!-- first product -->
<div class="prodCard">
<div class="prodJpg">
<img src="images/white-tee-1.jpg" class="prodTh" alt="Trendy, over-designed white T-shirt">
<button class="jpgBtn">Add to cart</button>
</div>
<div class="prodInfo">
<h4 class="prodName">Slightly<span style="color: #f28816"> White T-Shirt</span></h4>
<span class="price">£566.99</span>
</div>
</div>
<!-- second product-->
<div class="prodCard">
<div class="prodJpg">
<img src="images/white-tee-2.jpg" class="prodTh" alt="Common Mans White T-Shirt">
<button class="jpgBtn">Add to cart</button>
</div>
<div class="prodInfo">
<h4 class="prodName">Very<span style="color: #f28816"> White T-Shirt</span></h4>
<span class="price">£364.99</span>
</div>
</div>
</div>
</section>
<!-- js is needed for the aforementioned fade gallery -->
<script type="text/javascript" src="inc/script.js">
</script>
</body>
</html>
Index.php works as it should with an exception of the <?php include('inc/nav.php') ?> line
which just doesn't work at all, as if that code was not there to begin with.
This is my nav.php :
<nav class="navBar">
<link rel="stylesheet" href="../css/nav.css">
<div class="nav">
<h1 class="logo"> T-<span style="color: #f28816">Drip</span><br>Online<span style="color: #f28816"> Store</span></h1>
<div class="profBas">
<img src="../images/cart.png" alt="cart">
</div>
</div>
<ul class="linkCont">
<li class="linkItm">home</li>
<li class="linkItm">about</li>
<li class="linkItm">store</li>
</ul>
</nav>
Also a screenshot of the folder holding all the files just in case :
ss of the folder
I've been pulling my hair on this problem for quite some time now and I would greatly appreciate your help in this matter.
Edit 1:
Those files are not on any server currently, I'm just viewing them using firefox browser with web developer extension on it.
I should have said so sooner, sorry for that.
I've moved include line to the <body> as suggested but that didn't help. (I'll leave it there for all other suggested fixes since it's where it should have been to begin with).
I've also tried:
ini_set( 'error_reporting', E_ALL );
ini_set( 'display_errors', true );
include './members/reg.php';
and
<?php
ini_set( 'error_reporting', E_ALL );
ini_set( 'display_errors', true );
include './members/reg.php';
?>
but neither of those helped me.
Also to clarify a bit, nothing changes when I view the file regardless if I have the include line in the code or not.
Okay, I give up, I don't understand why the include is not working properly.
I'll just copy paste the nav in every page instead, thank you for your help though, I appreciate it.
The first reason could be that the navbar in the code is inside the <head> tag which should be outside the <body> tag. do this first then we find out next reason.
As correctly stated in #Ramkrishna's answer the HTML <nav> element should be inside the HTML <body> tag. Not the <head>.
As for not showing the nav.php file; you need to check that your filepath is correct. Does directory_containing_PHP_HTMLfile/inc/nav.php exist? Is it readable?
It is best practise to give an absolute filepath for the include, from the Server Document Root directory (typically the base directory of the web-accessible part of your server)
NOTE: $_SERVER['DOCUMENT_ROOT'] will not work when running only on the local(host) machine rather than a server machine. Use an alternative.
Also, include and require do NOT need the brackets.
All in all this brings us to:
<html>
<head>...</head>
<body>
<?php
include $_SERVER['DOCUMENT_ROOT']."/inc/nav.php";
?>
</body>
</html>
Finally
You should Read your PHP Error logs which will tell you if there is a problem with the included file.

Cannot get my pc built php site to work on the web

I have part built a php site on my pc. I do not want to have a blog style site, but do want to add a single header, single navbar and single footer to the site, and will start building a login area later.
I built it on my PC and it worked wonderfully when displaying it in IE, Chrome, Firefox and Safari, so I copied all the files into a test folder on the existing website, and this didn't work.
I got the error message
Fatal error: require(): Failed opening required 'includes/navbar.php' (include_path='.:/usr/lib/php5.4')
in /homepages/11/d95668464/htdocs/test/Projects.php on line 54
and haven't a clue what that means.
The required 'includes/header.php and required 'includes/footer.php works correctly and in the right place but the navbar doesn't show at all.
The navbar.php code reads:-
<?php
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>My Site's Title</title>
<link href="/Styles/Site.css" rel="stylesheet" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<section class="navigation" data-role="navbar">
<nav>
<ul id="menu">
<li>Home</li>
<li>News & Events</li>
<li><a href="projects.php>Projects</a></li.
<li>Contact Us</li>
<li>Join/Renew</li>
<li>Links</li>
</ul>
</nav>
</section>
The code on the page reads:-
<?php
include "includes/navbar.php";
?>
Try to link all of your href's to the correct place.
Right now:
<li>Home</li>
After:(Example: Desktop is whatever folder you may have index.php in. Im just assuming.)
<li>Home</li>
If your file is in a folder named test within your includes folder then you have to first resolve the
path to your includes by using the following function
get_include_path();
So now you can use this function along with your previous code. below is an example:
<?php
//get the include path
$pathToIncludes = get_include_path();
//include the file
include "$pathToIncludes/navbar.php";
?>
Here is a link to the PHP API http://php.net/manual/en/function.get-include-path.php
I hope this helps

when i try to include a php include function in a php page and runs it is showing that command as it is.. not fetching any contents

when the code is run it is displayed that
It is not displaying any contents of header.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>ADMIN PANEL</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- MetisMenu CSS -->
<link href="css/plugins/metisMenu/metisMenu.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/sb-admin-2.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="font-awesome-4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
</head>
<?php require("header.php"); ?>
<!-- -->
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Blank</h1>
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
</div>
<!-- /#page-wrapper -->
<!-- /#wrapper -->
<!-- jQuery Version 1.11.0 -->
script src="js/jquery-1.11.0.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
<!-- Metis Menu Plugin JavaScript -->
<script src="js/plugins/metisMenu/metisMenu.min.js"></script>
<!-- Custom Theme JavaScript -->
<script src="js/sb-admin-2.js"></script>
</body>
</html>
The contents of the require files are not shown
i tried require,include and all.
What is the problem with this...
It works fine if the contents of the header.php is pasted in the given section.. but it doesn't work when include or require commands are called
My header.php is as following
echo'
<ul class="nav navbar-nav navbar-right">
<li>HOME</li>
<li>SUPPORT</li>
<li class="dropdown">
PRODUCTS AND SERVICES <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>IT SERVICES</li>
<li>VOICE SERVICES</li>
<li>INFRASTRUCTURE</li>
<li>WEB & GRAPHICS DESIGN</li>
<li class="divider"></li>
</ul>
</li>
<li>CAREERS</li>
<li>INTERESTING INFORMATION</li>
<li>CONTACT</li>
</ul>';
?>
Have you checked that the file where you try to include your header.php is also a .php file, not .html or .htm? That's because html-files usually don't don't get checked for anything that needs to be processed by PHP - so there will be no PHP output.
Make sure you have PHP on your server.
make a file called 'phpinfo.html' with this content:
<?php
phpinfo();
?>
Put this file on your server. Then when you load this file, from your server, in your browser you should see a lot of information about PHP, not just the content.
After that check whether your PHP is used with the .php extension. Simply rename the file to 'phpinfo.php' and try again. If the first works, and this doesn't, then you have to make your server use the .php extension for PHP files. How that's done depends on your server configuration.
Make sure you have saved index file with .php extension, for example index.php
I found out the solution.. The code is perfect it is a problem with my chrome browser.. Whatever code i run it adds a tag before the php tags .. I changed the browser and works fine...
Thank you for your valuable support...

php virtual (''); works but php include''; does not

I'm trying to understand the difference between the following. All of the reading I've done says that 'include' should work. I'm wondering 'why' I have to use the 'virtual()' and/or WHAT I'm doing wrong with the 'include'.
<?php virtual('/path'); ?>
and
<?php include'/path'; ?>
I use both in the code below. The 'virtual()' works and is used for header.php. The 'include' (does not work) and is used for footer.php. The code for the entire page is below.
Link to live page
<head>
<title>Untitled Document</title>
<link href="/student_sites/2013/web_40/pages/css_includes/css_includes.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
<div id="header"><?php virtual('/student_sites/2013/web_40/pages/css_includes/assets/includes/header.php'); ?>
</div>
<div id="content_wrapper">Content for New CONTENT WRAPPER Div Tag Goes Here
<div id="side_bar">Content for New SIDE BAR Div Tag Goes Here</div>
<div id="content">Content for New CONTENT Div Tag Goes Here</div>
</div>
<div id="footer"><?php include '/student_sites/2013/web_40/pages/css_includes/assets/includes/footer.php'; ?></div>
</div>
</body>
</html>
<?php include'/path'; ?>
Doesn't work because of the type of file it's in. This statement must use the 'virtual()' which only works in php files. The 'include' only works in html or shtml files.
<!--#include virtual="/path" -->
for html files and
<?php virtual('/path'); ?>
for php files.
The html code still uses virtual but include as well, so I'm still not sure why it can't use include by itself.
-your 2ed period stutent

Can see the code in dreamweaver design view but not in browser

I have 3 files created in dreamweaver 1)mainfile.php 2)sidecolumn.php 3) style.css. Now I linked the css file with main file and called the sidecolumn file with the code
<?php include('sidecolumn.php');?>
I'm able to see the sidecolumn in the main file (in design view of dreamweaver) but the problem is when i tried to run in the browser, it is completely blank.
This is the code for mainfile.php
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" href="mystylesheet.css" media="screen" />
</head>
<body>
<div class="right"> <!----this the wrapper in stylesheet---->
<?php include('sidecolumn1.php');?>
</div>
</body>
</html>
This is the code of sidecolumn1.php
<div class="sidecolumn">
<div class="box_1b box_x">
<div class="name_1 round_corner_top">Video categories:</a></div>
<ul class="list_2">
<li>Site1</li>
<li>Site2</li>
<li>Site3</li>
</ul><!--/list_1-->
<div class="slideDown">More [+]</div>
</div><!--/box_1b-->
<div class="box_1b box_x">
<div class="name_1 round_corner_top">Video categories:</a></div>
<ul class="list_2">
<li>Site4</li>
<li>Site5</li>
<li>Site6</li>
</ul><!--/list_1-->
<div class="slideDown">More [+]</div>
</div><!--/box_1b-->
</div>
I'm testing with on my local pc. the url is d:\mydocuments\mysite\main.php
PHP is (in this context) a server side programming language.
It must be interpreted by a web server (which needs to support PHP).
You can't load the file directly into your browser from your file system.
You must set up a web server and access it via an http:// (or https://) URI.

Categories