I am not an expert but I am no noob at PHP, yet for whatever reason I am stomped as to why my document will not load. Here is my code.
<?php include 'header.php'; ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<p>Hello everyone</p>
</body>
</html>
When I pull out the PHP portion the HTML loads fine. Here is the code in my header.php file.
<?php
<href="index.php">Home</a>
?>
I have tried this on two different hosts, both of which are hosting other PHP websites and still getting issues. I have also validated it with W3Schools and another online PHP validator. Both didn't find any errors. Any help would be greatly appreciated.
Enable errors to see errors, this way:
ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);
This code is a PHP error:
<?php
<href="index.php">Home</a>
?>
Try change to:
<?php
echo '<href="index.php">Home</a>';
?>
This:
<?php
<href="index.php">Home</a>
?>
Is no valid PHP. This would, however work:
Home
Inside of the PHP-tags you can only use PHP - no HTML. Also, <href> is no HTML tag.
Look at this question How to get useful error messages in PHP? to find out, how to enable error messages in PHP.
Related
I have some problems when I include a PHP file. I am trying this for the first time so I don't really know how to get this is working. The PHP file doesn't respond anymore when I include something. Here is some code:
<html>
<head>
<title>Testing opus class</title>
</head>
<body>
<?php
include('eedce5ed141dd72b552b7abdeb48ede3ddebc0c4.php');
$opus = new opus();
print "Test"; // It isn't printing "Test" over here
Check if you get any errors with error reporting:
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
?>
Also make sure you don't have any error's in the file which you include!
<html>
<head>
<title>Location redirect test page</title>
</head>
<body>
<?php
header('Location: http://www.google.com');
?>
</body>
</html>
I uploaded this code to my server(1 and 1). Its not redirecting. I used the same code in XAMPP and it worked fine.
What am I doing wrong? Why is it working on xampp and not on real (1and1) server? I appreciate any help.
Thank you
You can not call header function after output. it shows warning as "header already sent...".
you must write it before tag.
<?php
header('Location: http://www.google.com');
?>
<html>
<head>
<title>Location redirect test page</title>
</head>
<body>
</body>
</html>
*may be your server setting for showing warning is off so it wont display warnings to you on related server.
If by some reason you can't place the php snippet before the html code (like Mayur said) you can use JavaScript for redirects
window.location.href = "http://stackoverflow.com";
Remember that when setting headers from php or setting cookies no text must be out putted before the php command!
When I create file index.php in directory /public/ my source code
<html>
<meta name="ROBOTS" content="NOARCHIVE">
<head><NOFOLLOW><noindex>
<?php echo '123' ?>
</NOINDEX></NOFOLLOW>
</head>
<body>
</body>
</html>
from browser looks like
<html>
<meta name="ROBOTS" content="NOARCHIVE">
<head><NOFOLLOW><noindex>
123
</NOINDEX></NOFOLLOW>
</head>
<body>
</body>
</html>
but if I create folder (no matter where) and place file index.php into it when source code become
<html>
<meta name="ROBOTS" content="NOARCHIVE">
<head><NOFOLLOW><noindex>
if I make some correction in this file, source code stay the same, not changed in browser view.
Why I cant create and run php code where I want, except /public/ folder?
Is that the exact code you are using in both index.php files?
If so, they should both fail as there is a syntax error. You are missing the ; at the end of the php code.
You posted: <?php echo '123' ?>
Should be : <?php echo '123'; ?>
If that´s not the case, you could add the following to the top of index.php to make sure the errors are showing on the page:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
If any error appear after that, please post them back here.
As of right now, the way I use includes is to bring the header, footer, and some content for other pages.
This leads to more includes then I really want, because I need to add more content for the includes.
For example:
<!DOCTYPE html>
<?php include('header.php'); ?>
<body>
<?php include('body-top.php');
custom html
</?php include('footer.php');
</body>
It would be nice if I could add variables to the includes and on the pages I want the includes to show.
I am not good at PHP at all, so is there a better way to use Includes?
This can be easily done:
index.php
$title = 'Hello World!';
include 'content.php';
content.php
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body></body>
</html>
The problem with this approach is, you'll soon run into problems keeping track what went where, so using functions as suggested in other answers might be a good idea. However, for small projects it's IMHO good enough.
sounds like a job for Smarty
It looks like this
<?php
require 'Smarty/libs/Smarty.class.php';
$smarty = new Smarty;
$smarty->assign('title','Hello World');
$smarty->assign('hello','Hello World, this is my first Smarty!');
$smarty->display('test.tpl');
?>
test.tpl
<html>
<head>
<title>{$title}</title>
</head>
<body>
{$hello}
</body>
</html>
Or even better way, use some of the PHP MVC frameworks, which will give you even more stuff (not just template system)
Your includes are already very few, no need to optimize them.
Also don't pay attention to people suggesting Smarty or MVC's because that will increase dramatically the number of includes (in exchange for other benefits, of course)-
You can turn your included files into functions. PHP has a neat trick where anything between curly-brackets (i.e. { and }) is only executed when that part of the code is reached. This includes the HTML code outside of your PHP tags.
This could be our 'header.php' file, where we wrap our current code in a function.
<?php function doHeader($title) { ?>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<?php } ?>
Then we make a tester for it. Whatever our tester/caller chooses to pass as $title shows up in our output.
<?php
// All included here
include_once('header.php');
?><!DOCTYPE html>
<?php doHeader('My page title'); ?>
<body></body>
</html>
This produces the output,
<!DOCTYPE html>
<html>
<head>
<title>My page title</title>
</head>
<body></body>
</html>
I have two scripts that I call with two PHP include() calls. The first starts a session / sets cookies, the second loads one of two JavaScript scripts. To keep things valid, I've been using the two calls but I'd like to just combine them into one.
Current setup (simplified):
<? include "session.php" ?>
<!DOCTYPE HTML>
<html>
<head>
<? include "scripts.php" ?>
...
What I'd like:
<? include "session_and_scripts.php" ?>
<!DOCTYPE HTML>
<html>
<head>
...
But it's invalid markup. Now if it really doesn't matter, I'd like to do it this way. If there are serious repercussions, then I'm thinking of just echoing a DOCTYPE in the included PHP file, which I'd rather not do.
So which is better: echo the DOCTYPE, use include() twice, or use include() once and have invalid markup?
EDIT - The whole script (session and javascript) should ideally be fully implementable with one line of code (e.g. the one include())
Use ob_start at first to avoid problems with session_start
<?php ob_start();?>
<!DOCTYPE HTML>
<html>
<head>
<?php include "session_and_scripts.php"; ?>
A way that uses only 1 1file and no additional instructions:
<?php include "session_and_scripts.php" ?>
<!-- more head-stuff-->
</head>
<body>
<!--more content-->
session_and_scripts.php should do the following:
<?php
//do the session stuff
?>
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
//some javascript
</script>
(But I would'nt say it's a good approach)
But it's invalid markup. Now if it really doesn't matter, I'd like to do it this way. If there are serious repercussions, then I'm thinking of just echoing a DOCTYPE in the included PHP file, which I'd rather not do.
Assuming that you do not want to have a valid markup, there is no problem, the only restriction is that session_start is called before any kind of "echo"...
Assuming you want a valid markup using only one include and without echoing the DOCTYPE from the included file, you can save the script text into a php variable and echo it in the main page after the inclusion
//main page
<? include "session_and_scripts.php" ?>
<!DOCTYPE HTML>
<html>
<head>
<?php echo $script;?>
// session_and_scripts.php
<?php
session_start();
$script = '<blablabla>';