PHP's include() function not working xampp [duplicate] - php

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 1 year ago.
Please bear with with me as I am new to web dev. I am attempting to use the include function in php to display a header on a web page.
I am using two very simple files, placed in the same directory and am using XAMPP.
index.php
<html>
<body>
<h1>Hello</h1>
<?php
include('header.php');
?>
</body>
</html>
header.php
<p>Hello</p>
When I open index.php, the statements from header.php never show up. All I see is one "Hello", as printed in index.php.
I have attempted to follow many tutorials online similar to this (ex https://www.w3schools.com/php/php_includes.asp, https://www.tutorialrepublic.com/php-tutorial/php-include-files.php), but none of them work for me.
Please let me know if you have any advice.

For security reasons you should use the following statements instead of include, unless you have a good reason to not do so:
Either
require_once 'header.php';
or
require 'header.php';
This will cause an E_ERROR in case the file was not found or you don't have permissions to include it. Otherwise you run into the risk not executing code which is mandatory for your program.

Related

How to make sites unavailable to user PHP?

I´m using a lot of including while coding pages to keep the code a little bit more beutiful and avoid typing code multiple times.
example of index.php:
<?php
include 'header.php';
?>
<p> random HTML </p>
<?php
include 'foo.php';
?>
The 'problem' now is that clients are able to navigate to www.page.de/foo.php and see this content. Is there a PHP-way to solve this without playing on .htaccess?
Don't place php files you don't want users to see in the public/ directory.
Edited out mention of .htaccess since you edited that into your question specification

if __name__ == main PHP to protect my code [duplicate]

This question already has answers here:
Prevent direct access to a php include file
(33 answers)
Closed 7 years ago.
I am somehow a noobie in PHP and I want to learn. I am making a proyect, in which I use require('parts/header.php') statements to include functions and templates. My pages look like this:
<?php
include('core/checklogin.php');
//This will check if the user is logged in and can see this page or not
include('parts/top.php');
//This loads the <head> tags and the header, including the navbar
?>
<section id="mainArea">
<h1>Hello <?php echo getUserNickname()?></h1>
<p>Some stuff</p>
</section>
<?php
include('parts/bottom.php');
//This loads the <head> tags and the header, including the navbar
?>
The problem is if someone enters myproyect.com/parts/top.php he would see the top part, and that file is going to be executed. I don't want that. I was thinking doing some stuff in a .htaccess file like:
#.htaccess inside parts directory
dont_serve_anything_inside_this_directory_and_return_forbidden();
But I don't know how without affecting the server side code.
Another alternative is to use the equivalent of if __name__ == 'main': of python, and do like:
//parts/top.php
if(__name__ == 'main'){
header('Location: /index.php');
exit();
}
What could I do?
You need to create file .htaccess inside your so-called protected from the outside direct access folder and put the following content there:
Deny from all
This will prevent users from being able to access your files using http://example.com/parts/bottom.php
Just add .htacccess to parts folder.

PHP include is not working? [duplicate]

This question already has an answer here:
Can't include or require an absolute path
(1 answer)
Closed 8 years ago.
I remember trying this plenty of times and it working but for some reason it isn't working for me now. I am using a local wamp server just to test this. It is not a public site. It isnt including the simple h1 tag that's located in the "/includes/header.php" for some reason.
My index.php code:
<html>
<head>
<title>Weekend Warrior Squad - Homepage</title>
<link href="styles/styles.css" rel="stylesheet">
</head>
<body>
<?php include("/includes/header.php"); ?>
</body>
</html>
My header.php code:
<header>
<h1>WWS - Weekend Warrior Squad</h1>
<p>
We are community based paintball team make the game of paintball
more enjoyable for ourselves and others.
</p>
</header>
All recommendations / answers are very much appreciated.
<?php
include "include/header.php";
?>
I think the reason for this is that you do not need the brackets. Try it and see if it works.
It's been a while since I've used wamp but I'm guessing the urls look something like localhost/project_name/index.php when you reference the include file as /includes/ it's actually looking for the file in localhost/includes/ and the file is not there.
Depending on where your file location is you can step out from it's folder and locate the includes folder.
include('../includes/');
This will go one folder up from where the file that is calling it. You can use .. as many times as you like to go to a new level, eg ../../includes
Try using
include($_SERVER['DOCUMENT_ROOT']."/includes/header.php");
Or else you could use require instead of include.
Firstly, are you certain the relative path you set in your include is correct? Temporarily set it to an absolute path to be certain.
Secondly, you are missing the closing tag for your h1 in header.php, not sure whether this would affect your current issue but it could cause others.
Edit: I fail.

In a PHP webpage, reading text from an external PHP to make updating content across pages easy

I'm writing a small website that has multiple pages. I'd like to have the same footers on each page, but I don't want to manually update 10 pages of HTML everyday. I'd like to put a PHP call to an external file in each HTML page (now .php pages, thanks to #br14np) so that when I update the PHP file, all the pages - when loaded - will show the same footer text.
<p><?php footertext.php ?></p>
is my wild guess at loading the content in the file of the afformentioned name but to no avail. (In footertext.php the code is: <?php print("Test numba one") ?>).
How can I go about doing this? I'd prefer an answer involving PHP.
UPDATE:
This is the exact code I'm using. Everything is in the same directory.
Main File:
<html>
<head>
</head>
<body>
<p> Content: <?php include "footertext.php ?></p>
</body>
</html>
Footer Content:
echo 'Test numba TWO!';
Use the include function. Just give it the path to your file. Example:
<?php include "footertext.php"; ?>
There are a few other functions that do similar things, such as require_once(). You can read more about that here.
Response to update
You're missing closing quotation marks after "footertext.php. Another tip that may help this situation is to turn on php error reporting. This will display any syntax or other errors on your page. Just insert the following code at the very top of your pages:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
?>
Also make sure you have opening and closing php tags (<?php ... ?>) in your footertext.php file.
The best fit solution for your query is include or require functions of php. now you need to identify which one out of those are your choices based on their functional behavior.
PHP include and require Statements
In PHP, you can insert the content of one PHP file into another PHP file before the server executes it.
The include and require statements are used to insert useful codes written in other files, in the flow of execution.
Include and require are identical, except upon failure:
require will produce a fatal error (E_COMPILE_ERROR) and stop the
include will only produce a warning (E_WARNING) and the script will continue
So, if you want the execution to go on and show users the output, even if the include file is missing, use include. Otherwise, in case of FrameWork, CMS or a complex PHP application coding, always use require to include a key file to the flow of execution. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.
Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.
Syntax
include 'filename.ext';
or
require 'filename';
You may like to go through the details of
Include,
Require,
Require_once &
Include_once.
Enjoy!
Anand Chavan

Include in included file doesn't work PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get useful error messages in PHP?
I have the following html list:
<ul id="ulGroups">
<?php
include('organizer_picturearchive_groups.php');
?>
</ul>
I am trying to include this file, which also includes another file:
<?php
include('lib/organizer_functions/picture_archive.php');
echo select_all_picture_category_groups();
?>
Basically, what I am trying to do is to call a function that is in the picture_archive.php file. I do that in the organizer_picturearchive_groups.php file.
I am including this file in the first file where the html list is. I am not pasting the whole code from there, because it is a lot, but it works.
However the include thing doesn't, because when I tried to make a test echo statement in the "organizer_picturearchive_groups.php" file it works and I can see the text I am printing.
Do you have ideas what may cause this problem ?
Double check the path. If organizer_picturearchive_groups.php is in the same directory as the code, then try configuring the path as relative:
<?php
include('./organizer_picturearchive_groups.php');
?>

Categories