-
PHP Required
Hi, in my site i am using PHP Require to show my Meta description and keywords, this gets the information from to external files, one for description and the other for keywords. Is it alright to use PHP Require to get information for the meta tags from an external file or is there any other better way to get the information for the tags?
-
By getting the tags do you mean like:
PHP Code:
<?php
echo '<meta tags="' . require("tags.php"); . '" />';
?>
If so that should work as long as the tags are alright, if it doesn't then you will have to do this:
PHP Code:
<?php
require("tags.php");
?>
and on tags would be the meta tag...
In case you don't know, if the file can't be found then the page will not load... so i suggest using include like this...
PHP Code:
<?php
include("tags.php") or echo("Could not load tags");
?>
Hope this helps...
-
yes the tags are
PHP Code:
<?php
require("tags.php");
?>
-
Code:
<?php
require("tags.php");
?>
It is ok to get any code, html, text, etc. from external files.
For the above code to work, tags.php must be in the same directory as the page that requires it, or, it is in the php include directory.
For something like meta tags and keywords, I would use include(); instead of require();
Use require when the file you are calling is CRITICAL to the function of your page, otherwise, use include. Database connection, a file with functions, etc. may be things you would want to use require(); for. You can also use require_once(); and include_once(); as their are usually no negative consequences to doing so.
Good luck.
-
Thanks, but im also wanting to know if the page title and the two meta tags, keywords and description. Can Be in one external php file for all the websites pages to get that information from.
-
Yes. Absolutely. You could even take it a step further and do this:
header.php:
Code:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta></meta>
<meta></meta>
<title><?php echo "$title"; ?></title>
<link href="css/reset.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/typography.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/forms.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/style.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/print.css" rel="stylesheet" type="text/css" media="print" />
</head>
<body class="<?php echo "$body_class"; ?>">
And in footer.php:
Code:
<div id="footer">
<p>My common footer.</p>
</div>
</body>
</html>
And then in index.php:
Code:
<?php
$title = "Home";
$body_class = "home";
include('header.php');
?>
<div id="content">
<h2>My index.php content</h2>
</div> <!-- end content -->
<?php
include('footer.php');
?>
And in about_me.php:
Code:
<?php
$title = "About Me";
$body_class = "about_me";
include('header.php');
?>
<div id="content">
<h2>My about_me.php content</h2>
</div> <!-- end content -->
<?php
include('footer.php');
?>
etc etc etc...