Having trouble with including PHP file - php

I am trying to include header.php file inside my index.php file, both of them are in the same folder. However, it is not working.
Here's how I coded index.php file:
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="css/bootstrap.min.css" />
<link rel="stylesheet" href="css/main.css" />
<title>Bootstrap</title>
</head>
<body>
<? php include 'header.php'; ?>
<p>the header file should be above this line</p>
<div class="container">
<div class="row">
<div class="col-md-4">3</div>
<div class="col-md-4">2</div>
<div class="col-md-4">1</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="js/jquery.min.js"></script>
<script src="js/popper.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
And here's how I coded my header.php file:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<p>HEADER INCLUDED</p>
<p>HIP HIP HORRAH!</p>
</body>
</html>
I expected that when I open index.php in Chrome, I should see something like
HEADER INCLUDED
HIP HIP HORRAH!
the header file should be above this line
--- the column ---
But the first two lines are not showing up. I am confused what I am missing here.

Change
<? php include 'header.php'; ?>
To:
<?php include 'header.php'; ?>
As <?php means starting of PHP code. But, <? php does not mean anything and is a parse error.
As php is not a language construct like: for, foreach, while, etc...

Related

PHP Site not working after renaming/adding new page

On my site, I had an index.php with a navbar. Then, I needed to add another page, called second.php, and updated my navbar code accordingly (I have a nav.html file for the navbar, so that I can use JS to load it onto each page of the site). I also added the navbar code to the second.php. However, when I opened index.php, the link to second.php didn't appear.
I tried restarting my computer, restarting my MAMP server, reopening my code editor, reopening the browser, but nothing worked. Then, I tried renaming my files (for both the purpose of testing and because I needed better file names) and updated all instances of the name of the files accordingly. But then this appeared:
followed by a list of the files in my directory. In particular, the site wasn't displaying. Could someone please explain what's happening and how I can fix it?
nav.html:
<body>
<header>
<div class="logo">
<h1 class="logo-text">Welcome!</h1>
</div>
<i class="fa fa-bars menu-toggle"></i>
<ul class="nav">
<li>Home</li>
<li>About</li>
</ul>
</header>
</body>
home.php (formerly second.php):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie-edge">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css">
<link rel="stylesheet" href="css/style.css?v=<?php echo time(); ?>">
<!-- Font Awesome -->
<link rel="stylesheet" href="//use.fontawesome.com/releases/v5.0.7/css/all.css">
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Kalam&family=Pangolin&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
<!-- JQuery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<title>Welcome Home</title>
</head>
<body>
<div class="placeholder"></div>
<script>
$(function(){
$('.placeholder').load("nav.html");
});
</script>
hello world
<script src="js/main.js"></script>
</body>
</html>
about.php (formerly index.html) is basically the same as home.php except it has a couple of paragraph tags.
I'm not clear on what isn't working at this point, so I might as well share a working example I have locally. I'm accessing my site at http://localhost/so/, and going to that URL will load any index page I have. I have 3 files inside the /so directory:
nav.html (I removed your <body> tags because those already exist in the other files):
<header>
<div class="logo">
<h1 class="logo-text">Welcome!</h1>
</div>
<i class="fa fa-bars menu-toggle"></i>
<ul class="nav">
<li>Home</li>
<li>About</li>
</ul>
</header>
home.php (accessible at localhost/so/home.php):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie-edge">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css">
<link rel="stylesheet" href="css/style.css?v=<?php echo time(); ?>">
<!-- Font Awesome -->
<link rel="stylesheet" href="//use.fontawesome.com/releases/v5.0.7/css/all.css">
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Kalam&family=Pangolin&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
<!-- JQuery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<title>Welcome Home</title>
</head>
<body>
<?php include "./nav.html"; ?>
hello world home
<script src="js/main.js"></script>
</body>
</html>
about.php (accessible at localhost/so/about.php):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie-edge">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css">
<link rel="stylesheet" href="css/style.css?v=<?php echo time(); ?>">
<!-- Font Awesome -->
<link rel="stylesheet" href="//use.fontawesome.com/releases/v5.0.7/css/all.css">
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Kalam&family=Pangolin&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
<!-- JQuery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<title>Welcome About</title>
</head>
<body>
<?php include "./nav.html"; ?>
hello world about
<script src="js/main.js"></script>
</body>
</html>
If I rename home.php to index.php then I can go to either localhost/so/ or localhost/so/index.php to access that file.
Slightly related: you shouldn't necessarily use time() as your CSS cache busting variable because it won't cache at all then. You should use a variable that you manually adjust the value of (i.e. $css_cache_bust = "2020061001";) like href="css/style.css?v=<?= $css_cache_bust; ?>" so you can incrementally adjust this as needed.

PHP page blank in Iframe on Mobile but on PC fine

I have a Cutenews program in the iframe on the homepage where myself and others can add a blog, the page displayed in the iframe is a PHP file, when accessing from my mobile the rest of the site loads just not the php file inside the iframe, on PC its fine.
I dont know coding very well so is there a code i need to use to add so the mobile detects the page or something?
Here is my code:
<!doctype html>
<html lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Central Gaming</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><!-- Main CSS -->
<link href="css/style.css" rel="stylesheet" /><!-- Font Awesome -->
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
</head>
<body><!-- Header -->
<div class="header-wrap d-none d-md-block">
<div class="container">
<div class="row"><!-- Left header box -->
<header class="col-6 text-left">
<h1>Latest</h1><br>
<p>
</header>
</div>
<?php
$number = 5;
include("/home/centralg/public_html/cutenews/cutenews/show_news.php");
?>
</h1>
</div>
</div>
</div>
</body>

PHP include a file with an included file -> scripts and variables get included but seem echo'd

I am having troubles with including a file that has an included file with some js script library's in it.
index.php
<?php
include 'header.php';
?>
<section>
<div>
<p>misc. html</p>
</div>
</section>
<?php
include ($_SERVER['DOCUMENT_ROOT'].'/footer.php');
echo 'test ='.$testVar;
?>
footer.php
<?php
include ($_SERVER['DOCUMENT_ROOT'].'/includes/footerscripts.php');
?>
</body>
</html>
/includes/footerscripts.php
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<?php
$testVar = 'test';
?>
In that scenario, the index file does have this in the source code when ran:
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
test =
The scripts are making their way there but are not being executed and the variables aren't coming through either.
When I change the footer.php to include those scripts and variable directly, the variable makes it to index.php, so do the scripts but they don't execute (or not on time?).
Any ideas guys?
I have a weird php5.ini file as I didn't know what all to put in it, if that makes a difference. The include file path seems okay as it still works for the most part.
Thanks,
Matt
Edit: Added the include ('header.php') which works fine and pulls in my header.
That file looks like:
<!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="keywords" content="--">
<meta name="author" content="--">
<title>--</title>
<link rel="stylesheet" href="/css/mainstylesheet.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
<link rel="icon" href="/images/favicon.ico">
</head>
<body>
<header class="nav-top" id="top" role="navigation">
<div class="container">
<div class="home-link">
<img src="/images/logo/logo-p.png" alt="plc" class="logo" />
</div>
<nav class="nav-main">
<ul>
<li>
Home
</li>
<li>
Portfolio
</li>
<li>
Contact
</li>
</ul>
</nav>
</div>
</header>
Edit: Guess I just needed my jquery loaded from the header of the document, because it works that way. I still can't carry variables from grandchild to parent but oh well
According to your example the index.php file is missing something:
<?php
include ($_SERVER['ROOT'].'/footer.php');
echo 'test ='.$testVar;
?>
I assume that you didn't post the whole file, since here the output hmtl here is not valid - missing html opening tag, etc. Could you please provide the whole and/or simplified index file?
Replace all instances of $_SERVER['ROOT'] with $_SERVER['DOCUMENT_ROOT'].

php , include, inject code of de header in the base file

The body of the base file has a include ' slider.php '.
In de slider.php there are css and javascript files that need to be load.
Because of the include it will but all of that in the body where the include is posted.
My question is: Is it possible if you have a include the file. You can say in the include put css and javascript in the <header> tag and not where the include is made.
[index.php]
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="slider">
<?php require_once( INCLUDES . 'slider.php');?>
</div>
</body>
</html>
[slider.php]
<?php
<!-- slider -->
<link rel="stylesheet" type="text/css" href="/normalize.css" />
<link rel="stylesheet" type="text/css" href="/demo.css" />
<link rel="stylesheet" type="text/css" href="/component.css" />
<script src="/slider.min.js"></script>
<script src="scattered_slider/classie.js"></script>
<div class="row header">
etc...
No, it's not possible to do that using only a single include/require.
The fastest solution would be to use two different files "slider_header.php" containing the javascript/css tags and "slider_body.php" containing the actual slider html.
Then you could include them like this:
<!DOCTYPE html>
<html>
<head>
<?php require( INCLUDES . 'slider_header.php');?>
</head>
<body>
<div id="slider">
<?php require( INCLUDES . 'slider_body.php');?>
</div>
</body>
</html>
Maybe you should consider using a proper template engine like Twig, but this requires a little bit more effort to set it up in the first place.

Including a CSS file in a PHP file that is included in a PHP file

I have a php file that doesn't (for now) use any php code, that holds code for the header and main menu that will be used across all pages. The CSS file has no effect, even though I've created a style class for h1. The text "TEST" shows up, but the style is not applied. How do I properly include the CSS file?
mainMenu.php
<!--This code is included within the <head> of each page, and the stylesheet for the header and main menu are included here-->
<link href="styles/headerMenu.css" ref="stylesheet" type="text/css">
<!--Header and menu code-->
<div>
<h1>TEST</h1>
</div>
index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>untitled</title>
<?php include ('./includes/mainMenu.php') ?>
</head>
<body>
</body>
</html>
The CSS file is not found. Double check the link and correct it:
<link href="menu.css" rel="stylesheet" type="text/css">
^- REL not REF
Also to prevent additional problems, remove the start and end tags of <head> and <body> from the code. The way you output the HTML elements, you would create wrong HTML if you keep those tags. Remove them and your page will be valid HTML again.
index.php:
<!DOCTYPE html>
<html>
<meta charset="utf-8" />
<title>untitled</title>
<?php include ('menu.php') ?>
</html>
Valid HTML has the benefit that you can run it through a validator to spot errors early.
I think it may be because you have got your menu appearing inside your <head> tag.
The CSS needs to go inbetween the <head> and </head> but the rest needs to be inside the <body> tag
<link href="styles/headerMenu.css" rel="stylesheet" type="text/css">
This must be at <HEAD></HEAD>
<div>
<h1>TEST</h1>
</div>
This must be at <BODY></BODY>
You have to separate this file into 2 files and include them in Head and in Body..
Don't include your HTML code in HEAD part. Only include CSS and JavaScript files in HEAD section. and you need to prefix the css or images path in some php file.
E.g.
create new php file with name "conn_path.php"
<?php
define('SITE_PATH',$_SERVER['DOCUMENT_ROOT'].'siteName/');
define('SITE_HTTP_PATH','http://localhost/'siteName/');
define('CSS_PATH','http://localhost/siteName/styles/');
define('IMAGE_PATH','http://localhost/siteName/images/');
?>
And then you path will be like below:-
mainMenu.php
<?php include "conn_path.php" ?>
<link href="<?php echo CSS_PATH ;?>headerMenu.css" rel="stylesheet" type="text/css">
It will help you in whole project…
Create a template file, with your essential (and re-used) html. Also with <html>, <head> and <body> tags and anything you must have in all pages – As your stylesheets and menu.
Then add a content section with a single variable.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>untitled</title>
<link href="styles/headerMenu.css" rel="stylesheet" type="text/css">
</head>
<body>
<!--Header and menu code-->
<div>
<h1>TEST</h1>
</div>
<?php echo $page_content; ?>
</body>
</html>
This way, any page content shoud be assigned to $page_content instead of echoed.

Categories