SaranacLake Posted January 15, 2021 Share Posted January 15, 2021 I am working on a temporary mobile site while I try and fix my main site. As I do this, I am trying to look at better ways to code. Is there any problem/danger to including the HTML Head? Since this block of code repeats - except for the < title > - I figure why code it multiple times. I was thinking of using an INCLUDE on this... <head> <!-- METADATA --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width", height="device-height", initial-scale=1> <!-- TITLE --> <title>$title</title> <!-- STYLES --> <link type="text/css" rel="stylesheet" href="css/styles.css" /> </head> Thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/312011-including-html-head/ Share on other sites More sharing options...
guymclarenza Posted January 17, 2021 Share Posted January 17, 2021 (edited) I do this all the time. I don't like using the same code on every page, when I have duplication I use an include file, first started doing this in shtml. I don't see any speed issues on my sites. I have been coding for a while, but am not a great coder, someone with more experience may differ. If you want to see it in action, check out The site I am busy working on. I use very simple CSS on this one, you are welcome to view source and borrow. include ("../navigation/constants.php"); SQL here to get data //build metatags $indTags = "<title>$title </title>\n"; $indTags .= "<meta name='description' content='$metadesc' />\n"; $indTags .= "<link rel='canonical' href='$canon' />\n"; $indTags .= "<meta property='og:title' content='$title' />\n"; $indTags .= "<meta property='og:type' content='website' />\n"; $indTags .= "<meta property='og:url' content='$canon' />\n"; $indTags .= "<meta name='keywords' content='$tags' />\n"; $indTags .= "<meta property='og:image;' content='$ogimg' />\n"; } $indTags .= "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />\n<meta name='viewport' content='width=device-width, initial-scale=1.0'>\n\n"; //lots of includes include ("../navigation/head.php"); include ("../css/style.php"); include ("../navigation/seman.php"); include ("../navigation/top.php"); Edited January 17, 2021 by guymclarenza Quote Link to comment https://forums.phpfreaks.com/topic/312011-including-html-head/#findComment-1583813 Share on other sites More sharing options...
SaranacLake Posted January 17, 2021 Author Share Posted January 17, 2021 I wasn't sure if including my html_head.php file would work, because that file has this line of code... <title><?php echo (isset($htmlTitle) ? $htmlTitle : HTML_TITLE_DEFAULT) ?></title> So I wasn't sure if that would be considered "recursive" and break things, since you are including the file using PHP and then calling PHP from within that file to echo something. Quote Link to comment https://forums.phpfreaks.com/topic/312011-including-html-head/#findComment-1583828 Share on other sites More sharing options...
requinix Posted January 18, 2021 Share Posted January 18, 2021 2 hours ago, SaranacLake said: So I wasn't sure if that would be considered "recursive" and break things, since you are including the file using PHP and then calling PHP from within that file to echo something. Yeah, no. It just means you have to define a $htmlTitle variable in the code before it includes html_head.php. Quote Link to comment https://forums.phpfreaks.com/topic/312011-including-html-head/#findComment-1583833 Share on other sites More sharing options...
SaranacLake Posted January 18, 2021 Author Share Posted January 18, 2021 16 hours ago, requinix said: Yeah, no. It just means you have to define a $htmlTitle variable in the code before it includes html_head.php. In index.php I have... <?php $htmlTitle = "My page specific title here"; ?> <!DOCTYPE HTML> <html lang="en"> <?php require_once('../../coponents/html_head.php'); ?> and so on... And then in my "head_html.php" file I have... <head> <meta blah blah? <title><?php echo (isset($htmlTitle) ? $htmlTitle : "DEFAULT_TEXT_HERE") ?></title> <link blah blah> </head> So is that okay? Quote Link to comment https://forums.phpfreaks.com/topic/312011-including-html-head/#findComment-1583855 Share on other sites More sharing options...
requinix Posted January 18, 2021 Share Posted January 18, 2021 56 minutes ago, SaranacLake said: So is that okay? Does it work? Quote Link to comment https://forums.phpfreaks.com/topic/312011-including-html-head/#findComment-1583862 Share on other sites More sharing options...
SaranacLake Posted January 18, 2021 Author Share Posted January 18, 2021 10 minutes ago, requinix said: Does it work? It seems to work okay for the last few days, and my web pages pass the W3C validator. But when I staretd this thread, i wasn't sure if it was correct or not. (I hate writing crappy code?!) Quote Link to comment https://forums.phpfreaks.com/topic/312011-including-html-head/#findComment-1583864 Share on other sites More sharing options...
requinix Posted January 18, 2021 Share Posted January 18, 2021 Doing what you're doing is a dated practice. The industry has moved on to templates and such. But this method isn't inherently wrong. In time, you'll probably change to do something else. And that's fine. Quote Link to comment https://forums.phpfreaks.com/topic/312011-including-html-head/#findComment-1583865 Share on other sites More sharing options...
SaranacLake Posted January 18, 2021 Author Share Posted January 18, 2021 10 minutes ago, requinix said: Doing what you're doing is a dated practice. The industry has moved on to templates and such. But this method isn't inherently wrong. In time, you'll probably change to do something else. And that's fine. Again, this is for my *temporary* website so I can get content onine while I finish (and fix) my larger "real" website. The goal is to make my temporary website EASY to develop, and I have decided that having a half-a-dozen directories all with "index.php" is confusing. So how can I tell MAMP that if a directory doesn't have an "index.php" file in it, then that is okay? Quote Link to comment https://forums.phpfreaks.com/topic/312011-including-html-head/#findComment-1583867 Share on other sites More sharing options...
requinix Posted January 18, 2021 Share Posted January 18, 2021 There is a way, but it would be "crappy code". If the problem is seeing "index.php" everywhere then figure out a way to resolve THAT. For example, by not having a dozen tabs open. Or by seeing if NetBeans can give you more path information. Quote Link to comment https://forums.phpfreaks.com/topic/312011-including-html-head/#findComment-1583871 Share on other sites More sharing options...
SaranacLake Posted January 18, 2021 Author Share Posted January 18, 2021 1 hour ago, requinix said: There is a way, but it would be "crappy code". Well, for now, I'll just leave things as is - I have this thing for "pretty URLs", but whatever. 1 hour ago, requinix said: If the problem is seeing "index.php" everywhere then figure out a way to resolve THAT. For example, by not having a dozen tabs open. Or by seeing if NetBeans can give you more path information. If I hover, I think I can see the path, but its just a pain when you have 25 tabs open - which i usually do. Quote Link to comment https://forums.phpfreaks.com/topic/312011-including-html-head/#findComment-1583885 Share on other sites More sharing options...
SaranacLake Posted January 18, 2021 Author Share Posted January 18, 2021 1 hour ago, requinix said: There is a way, but it would be "crappy code". If the problem is seeing "index.php" everywhere then figure out a way to resolve THAT. For example, by not having a dozen tabs open. Or by seeing if NetBeans can give you more path information. @requinix, You posted in the wrong thread! (And I replied in the wrong thread too!) *LOL* Back to tis thread, it sounds like I included my < head > code the correct way. Quote Link to comment https://forums.phpfreaks.com/topic/312011-including-html-head/#findComment-1583886 Share on other sites More sharing options...
maxxd Posted January 20, 2021 Share Posted January 20, 2021 On 1/18/2021 at 1:10 PM, SaranacLake said: So how can I tell MAMP that if a directory doesn't have an "index.php" file in it, then that is okay? Use .htaccess and mod_rewrite - most if not all modern frameworks do it this way, and it gives you the pretty urls you're looking for. As you move forward with redoing your main site (if you continue to eschew an established full framework) you might want to check out Slim for your routing and dependency injection - I've heard good things about it, and the small amount of playing I've done with it I found enjoyable. I'm sure others with more direct experience can give you better and more advice on it. Quote Link to comment https://forums.phpfreaks.com/topic/312011-including-html-head/#findComment-1583916 Share on other sites More sharing options...
JacobSeated Posted January 27, 2021 Share Posted January 27, 2021 (edited) Just a few tips if you are coding vanilla PHP. As others have said, you can create "pretty URLs" with mod_rewrite; But a better way to go about it would be to point all requests to PHP, for non-existent files, since it is much easier to prettify your URLs from PHP than it is with the horrific syntax of .htaccess. Here is an example, for .htaccess: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^.*$ / [QSA] Then, from your index.php file: $parsed_url = parse_url($_SERVER['REQUEST_URI']); $routes = [ '/^\/(blog)\/([a-z0-1_-]+)$/', '/^\/(forum)\/([a-z0-1_-]+)$/' ]; foreach ($routes as $url_pattern) { if(false === preg_match($url_pattern, $parsed_url['path'], $matches)) { http_response_code(404); echo 'Page not recognised...'; exit(); } // If a requested path matched a pattern, try to call the related feature $requested_feature = $matches[1]; $feature_path = $matches[2]; if (is_callable('feature_'.$requested_feature)) { call_user_func('feature_'.$requested_feature, $feature_path); } else { http_response_code(404); echo 'Page not recognised...'; exit(); } } function feature_blog($feature_path) { http_response_code(200); echo 'Showing the blog'; exit(); } function feature_forum($feature_path) { http_response_code(200); echo 'Showing the forum'; exit(); } This is of course not an ideal way to handle it, but a pretty good starting point. Modern applications also uses templates, again, a pretty good starting point , if you do not use a template engine, would be to store your HTML in heredoc, inside separate .php files: <?php $template = <<<LOADTEMPLATE <!DOCTYPE html> <html lang="en"> <head> <title>{$tpl_content['title']}</title> <link rel="stylesheet" type="text/css" href="/my_css_file.css"> </head> <body> <article> <h1>{$tpl_content['title']}</h1> {$tpl_content['content']} </article> </body> </html> LOADTEMPLATE; // Comment to preserve required "\n" character after heredoc-end delimeter on editor saves You can easily load this file from whatever location you want, and then have it filled out automatically with the contents of the $tpl_content array; just remember to define the array elements to avoid undefined notices. To output the template, and have it filled out with contents, you could do like this: function feature_blog($feature_path) { // Define template content $tpl_content['title'] = 'Hallo World'; $tpl_content['content'] = '<p>Hallo World</p>'; // Include the relevant template, require_once('templates/default.php'); http_response_code(200); echo $template; exit(); } Keep in mind, this is just an example. But you could easily use this as a base for something more mature. A decent system would also allow you to set HTTP response headers, implement caching mechanisms, and allow you to restrict HTTP request methods on a per-feature basis. But, if you used a framework or a CMS like Wordpress, then some of this should automatically be handled. Edited January 27, 2021 by JacobSeated Quote Link to comment https://forums.phpfreaks.com/topic/312011-including-html-head/#findComment-1584051 Share on other sites More sharing options...
SaranacLake Posted January 29, 2021 Author Share Posted January 29, 2021 On 1/19/2021 at 11:30 PM, maxxd said: Use .htaccess and mod_rewrite - most if not all modern frameworks do it this way, and it gives you the pretty urls you're looking for. As you move forward with redoing your main site (if you continue to eschew an established full framework) you might want to check out Slim for your routing and dependency injection - I've heard good things about it, and the small amount of playing I've done with it I found enjoyable. I'm sure others with more direct experience can give you better and more advice on it. Obviously I'm not going to throw out my current code-base. And, as stated, I do want to learn MVC and OOP for v2.0 Quote Link to comment https://forums.phpfreaks.com/topic/312011-including-html-head/#findComment-1584104 Share on other sites More sharing options...
guymclarenza Posted January 29, 2021 Share Posted January 29, 2021 (edited) RewriteEngine On RewriteCond %{HTTP_HOST} ^XXXXX\.com [NC] RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://XXXXX.com/$1 [R=301,L] RewriteRule ^([^/\.]+).html$ pages.php?furl=$1 [L] .htaccess file The first rule redirects everything to https The second grabs the part of the url between / and .html and creates a variable to use in your code. $ident = $_REQUEST["furl"]; $query = $pdo->prepare("SELECT * FROM pages WHERE page_website = :site AND page_url = :purl ORDER BY page_id DESC LIMIT 0,1"); $query->bindParam(":site", $site); $query->bindParam(":purl", $ident); $query->execute(); php file I hope this helps. Edited January 29, 2021 by guymclarenza Quote Link to comment https://forums.phpfreaks.com/topic/312011-including-html-head/#findComment-1584117 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.