Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm not the best at coding PHP, HTML and CSS are more where my expertise lie.
I would like to have a PHP file included into the homepage of my site, but only the homepage and not on any other page within the site, as the element in question is based in a header.php file and would appear on every other page otherwise. Is this possible to do and if so how could it be done?
If anyone could help that would be great!
Use the server superglobal, and request URI to check for the home page.
<?php
if($_SERVER['REQUEST_URI'] == '/' or $_SERVER['REQUEST_URI'] == '/index.php')
include('somefile.php');
?>
EDIT:
Got ninja'd, keep in mind that your home page could be requested a couple ways if you are using index.php. Also: Fairly sure that there is always an opening / on URIs, as it is just the verbatim query string for the server.
As you didn't post any code. So, I assume one of the condition here.
You can try something like this:
<?php
if($_SERVER['REQUEST_URI']=="index.php" || $_SERVER['REQUEST_URI']==""){
// include your file..
include('somefile.php');
}
?>
P.S.: I assume that index.php is your default home page.
You should search more on $_SERVER['REQUEST_URI']
You will need to adjust the variables, but this should do the trick.
if (basename($_SERVER['PHP_SELF']) == "yourHomePage.php") {
include_once("yourinclude.php");
}
You may need to echo the basename of the page to get it exactly right. I cannot remember if it will have a / on the front.
echo(basename($_SERVER['PHP_SELF']));
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
So in order to minimalize code and optimize speed of my website I have used much PHP code. I used it to contain the entirety of the header and footer of my website.
<?php include('header.php') ?>
...Content...
<?php include('footer.php') ?>
In doing so I had trapped the title meta-tag within a PHP file, so in order to change the Title for each unique page I used a PHP variable placed above the header to adjust it like so:
<?php $title="Page Specific Title" ?>
<?php include('header.php') ?>
...Remainder of site.
I have tested this out and it clearly works within the browser. However, now that I am looking into SEO for my website, I am curious.
Does using PHP to populate SEO related meta tags have a negative impact on the SEO of the page?
If so, my follow up question would be what way can I optimize the speed of the website without negatively impacting the SEO of the individual pages? Would JavaScript work or am I confined to HTML?
Thank you in advance for any that can help.
Search engines visit sites almost like a user, by which I mean a page is prepared by the server and sent to them. PHP is whats running on your server to create that page, including setting the title to the value of the variable. So what the search engine get is the output of your PHP with the variable replaced with the value.
So no, using PHP will not effect your SEO.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have a web page which made up of three pages[Header, Content, Footer]. I have something like as follows in my code
<?php
if (session_status() == PHP_SESSION_NONE){
session_start();
}
if(!isset($_SESSION['username'])){
include('header.php');
include('body.php');
include('footer.html');
}else {
header('Location: users.php');
}
?>
Is there anything wrong with this approach? Are I am doing the right thing? I am not facing any problems since I am a newbie So I am not much aware of the issues. I just want to know good/bad practice to achive this?
I have this kind of set up because header & footer is common for all my pages. So I just modify the centre portion.
For what you're trying to achieve, this approach is perfectly fine.
Although as your files and directories will increase in number, sometimes it'll get frustrating to remember the location of those files and using include accordingly. i.e. include("../../static_pages/body.php").
So you should put $_SERVER['DOCUMENT_ROOT'] at the beginning and use it accordingly to use the same URLs in whole websites.
i.e. include($_SERVER['DOCUMENT_ROOT']."static_pages/body.php")
Oh, mind the difference between require and include. require will stop execution when file is not found whereas include will just throw a warning.
Both have their importance. :D
I think there's nothing wrong with your approach. However, I am not a professional or something but if it works it should be alright because I think it is a secure way of displaying your page.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am working on a PHP website, and don't want to have to write headers, footers, etc. for every page. I came up with the idea to have written headers and footers that could be retrieved with a function just like Wordpress. I found Wordpress's get_header(); function in /wp-includes/general-template.php but it loads through a template which I do not have being a hand written website. My question is "How do I write multiple PHP files such as header.php, and footer.php, etc. and compile them in to one document in the index.php?
Look into the include() function, along with the similar include_once(), require(), and require_once().
A. Make sure your index page is index.php and not index.html.
B. Write one header file, header.php, and use if/else statements to bring in the content you want for that particular page.
C. Use <?php include('header.php'); ?>at the top of your index.php and <?php include('footer.php'); ?> at the bottom of your page.
D. If this isn't working, make sure your server has PHP installed on it.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I've got a question about php and pages :
Is-it worth to split the code into many other files ?
So at the end, the index.php would be composed of :
include 'footer.php';
include 'header.php';
include 'home.php';
include 'load.php';
[...]
Or should I put all the code in an unique page ?
Other question, if I include a php page, could all the vars created in the included page be used in the main page, or should-I use $_SESSION ?
Thank you all for your help !
As for your main question it’s worth. In fact, it will be a pain for you to manage your web page if you don’t follow this method in the long run. I am not a php guy but I can tell you this because this is a universal truth. The idea here is to write once and use anywhere necessary. You don’t want to write the same thing again and again, do you? That’s why we produce function when programming. We define a function and use it as many times we want. Now consider you have suddenly found that you need some modification to do , as you have created a function you can make the change in just one place and it will be reflected everywhere without touching a single line of code outside the function’s code. In actual project change is a constant. That’s why we find many design pattern in the software industry like MVC.
In a web project, mostly (not always) the header, navigation, footer are same across all the pages. Therefore, you should consider making different file for these stuff and you should always put your content in your index.php file which are unique for index page.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I have a change_username.php which makes it possible for users to change their username. If they changed their username a page like "Your new username ist xxx" or "xxx is already used" will be displayed.
I don't want to use an echo and put all the HTML stuff in there.
My first idea would be to create another 2 PHP's which then get the username from the change_username.php and display a proper result with HTML.
Are there any alternatives / better ways to do that? Creating files for every result sucks as much as putting the HTML data in an echo.
If I understood your question correctly you want to include a page with in your PHP: try using this as reference http://www.w3schools.com/php/php_includes.asp
You can include the page using
include 'filename';
or
<?php include 'menu.php'; ?>
I hope this helps you
I would do it a little bit differently, I think the problem is with managing the code where large page is getting messy. What I do in these cases I just use functions with parameters. So I create a lib.php file, where I store function with HTML blocks of code being echoed. Then when I need I call these functions in the necessary files, just requiring lib.php at the top of my page.