PHP include working, include header is not - php

So I've tried looking around in other questions, but can't seem to find a question consistent with my problem.
What's going on is that I have been working on a project using angularjs, and primarily working on pure html for most of the development, and am finally at a stage where I want to cut out the header/footer code and bring it in as php with includes.
However, include("../Includes/header.php"); is not working. Other includes with basic code seems to be working okay. Here's my code:
<?php
//require_once ("Includes/simplecms-config.php");
//require_once ("Includes/connectDB.php");
include("./svg.php");
include("../Includes/include.php");
echo "<i>This is an echo from the current file.</i><br />";
include("../Includes/header.php");
?>
This is the code from ../Includes/include.php:
<?php
echo "<b>This is an included echo from another directory.</b><br />";
?>
the .svg.php file is irrelevant, just some svg graphics I use. If I move the include header ABOVE the echo, then nothing shows up. If I have it below (like it is in the code above), the echo commands work, but everything stops after the include header.
There are no errors that I can seem to find, no warnings, no nothing...just not outputting anything past that specific include.
(Working with Microsoft WebMatrix if that is even remotely relevant)

This might be a shot in the dark but in general it's best practice to leave the closing ?> off of php-files. I don't see how that could affect including a file, but I also don't see anything else "wrong" with your code.

Related

php require_once modify the local variable?

In my web page, I wrote:
<?php
//define('__PUBLIC__', $_SERVER['DOCUMENT_ROOT'].'/public');
$doc_public = $_SERVER['DOCUMENT_ROOT'].'/public';
echo "Before include...<==============>$doc_public";
?>
<?php require_once($doc_public.'/inc/head.php'); ?>
<?php echo "After include...<==============>$doc_public"; ?>
And the page shows:
This firstly happened when I notice the fatal error in the footer, but the head is fine.
Although I can implement define or constant variable to avoid this, I am still curious how it happens.
P.S.: I run this under Apache with a port 8001. This is set in 【apache\conf\extra\httpd-vhosts.conf】. I am running more than one webapp under this site. I just share this information, as I am not sure this has anything to do with this case.
Thanks!
When you require a file, if a variable is modified it affects the original script as well, that's how it's designed. Require doesn't create a secondary environment separated from the including file, it just adds the PHP code in sequence, exactly like if you had written the code in the initial file.
Have a look at the official PHP documentation, the first example is exactly the same as your case
http://php.net/manual/en/function.include.php
(include is the same as require, the latter just throws an error. For more info about differences between include and require http://php.net/manual/en/function.require.php)

trouble including a class library in PHP on an ubuntu server

I'm trying to export some classes into a library to keep my code tidy
but I just don't know why this doesnt seem to work :(
the include file is definitely being loaded.
when I defined the class locally there was also no problem and testgerät was sent.
I always try to keep things simple at first so I'm using this small sample:
Gerät.php:
<?php
echo "loading Gerät<br>";
class Gerät{
function Gerät(){
$this->name = "testgerät";
}
}
?>
index.php
<?php
include "/var/www/html/aiberry/objects/Gerät.php";
echo "My first PHP script!<br>";
$test = new Gerät();
echo $test->name;
?>
the output of index.php
loading Ger�t
My first PHP script!
could this be a namespace issue?
EDIT: I'm sorry for causing confusion regarding the actual issue:
I am in fact expecting the output:
loading Ger�t
My first PHP script!
testgerät
If I knew how to turn the debugs on I would probably get a message telling me that there is no class "Gerät". The paradox for me though is that Gerät.php is definitely being loaded because the output "loading Gerät" is written within that file. a simple copy paste of the class into index.php delivers the desired output so i can only think of a name space issue or something alike where the class is just being lost after Gerät.php is done processing.
My guess is that you are having different charset encodings between the two files, can you verify with your text editor if Gerät.php was saved with the same character set encoding of index.php?
In general it's a good idea to avoid non-ascii character in PHP identifiers (class names, variable names, etc)

Including File PHP not work - work by using comment tag - bug?

ISSUE:
PHP Warning: ........ failed to open stream: No
such file or directory in
/home/.
I found many titles discussed related on the topic I pick now. I tried to look into the path and make sure everything went okay. I also checked the server connectivity, bandwidth or everything related to hosting or DNS stuff and tried it again. But it was still not working.
However, when I use php comment tag, as in #, unbelievable, it really really works! That's why I want to find out why it could be like that.
Here's the actual path:
/home/appacybe/public_html/in-grammar/user_validator.php on line 2
Previous (error) report codes:
<?php
include('../user_validator.php');
...
?>
<!DOCTYPE html>
...
</html>
Working codes:
index.php:
<?php
################# start ####################
############################################
################# end ####################
include('../user_validator.php');
...
?>
<!DOCTYPE html>
<!--bla bla
./end -->
Inside user_validator.php:
<?php
echo 'hallo';
?>
Successfully Output:
- Hallo
- other text below it.
Some suggests to use Magic Constant and many says that I should use require, require_once or others.
But when I use that php commentary tag, I found it works successfully.
What's wrong? Is this a bug? or I just don't know how to be on the right PHP programming?
Please let me know.
This is the simple thing for you perhaps but really I still question; what's wrong with it or "me" ?
NEW POSSIBILITIES:
I tested to VPS or Dedicated Server and it works by using common function. But always got failed in share hosting - but working with the comment tag.
What's going on? :(
Any help would be so great!
You can use dirname(FILE) to obtain the path of the script that line is called in. You can then reference relative paths from there e.g.
include (dirname(__FILE__) . '../user_validator.php');
According to me, In some cases where $_SERVER['DOCUMENT_ROOT'] is not set or is not what you would expect (i.e. not set in CLI or old IIS, or invalid in certain CGI setups) then server might not be able to find out related path of include file. So, it will give an error.

Including not working , is it too many includes?

Hi StackOverFlow members.
I need a favor regarding my programming script.
I'm stunned trying out different various ways to make my code more simpler.
Before I tried to make things simpler . The code works great and it able to do the function.
Here goes:
Detection PHP which worked on a HTML previously before I tried to tweak it a little:
<?php
include 'Mobile_Detect.php';
$detect = new Mobile_Detect();
if ($detect->isMobile()) {
header('Location: http://facebook.com');
exit(0);
}
?>
<b> Testing / This is not a supported page for mobile </b>
This code works like a charm. When I'd use the browser to access the following page.
It shows me the message "Testing / This is not a supported page for mobile ".
When I'd used a mobile phone to access it , it works like a charm and redirect me back to Facebook.
So this is the new problem that I faced .
I just removed the PHP from the HTML and put it in a file called "detection.php"
<?php
include 'detection.php';
?>
<b> hello </b>
<p> testing on anarchy </p>
But this time , when I used the web browser to access the site , it showed me the message
"Testing on anarchy" and when I used the mobile as well. It still redirect me to the same page
"Testing on anarchy".
How do we make it in a way where you just need to include the PHP files on the HTML header rather than writing the PHP code on the HTML page.
Any idea?
Try Using: you have to use the brackets ('page');, and you should not repeat any code as you are already calling the code from another page, so all you add is:
<?php
include('Mobile_Detect.php');
?>
and on the mobile_detect.php page add:
<?php
$detect = new Mobile_Detect();
if ($detect->isMobile()) {
header('Location: http://facebook.com');
exit(0);
}
?>
Always use "require" instead of "include".
"require" throws a Fatal Error if the function doesn't find the file, and the script is halted.
"include" throws a Warning if the function doesn't find the file and the script continue to run, which means that you might not notice this bug if you turned off the PHP errors.
Also rely on "require_once" to avoid problems such as "Cannot redeclare function" or "Cannot redeclare constant" etc.
Also, if you're using "header()" you need to make sure you're not using "print" or "echo" prior to the "header" function. Otherwise, you'll get the "headers already sent" error.
To debug your problem, you should use "require_once" and then you should check if it fails. If it fails, then you probably have a problem with your path.

PHP files not loading properly

I have a PHP file consisting of the following structure:
<html>... headers, scripts & styling
... some html here
<?php
if($_GET['v'] == 1)
{
?>
... html code here ...
<?php
}
else
{
?>
... html code here ...
<?php
}
?>
</html>
Sometimes the file just loads half, for example if v=1 what would load onto the screen (if I check with View Source also) is something like this: (relative to what I exampled above)
<html>... headers, scripts & styling
... some html here
... html cod
As you can see, the code just cuts off randomly. The is nothing obvious casing this such as a loop or anything. It happens in the middle of HTML code and not inside the <?php ?> tags.
It looks as if the server just decides to stop communicating right there-and-then for no reason. It also happens at a different and random place each time and sometimes loads perfectly fine.
It also only happens on my shared hosting account and not on my localhost.
Is there anything simples that might be causing this?
Did anyone experience this before?
Your code produces a warning (apparently silent) and fails here:
if($_GET['v'] == 1)
if no v parameter was given in the query string.
Do it like this:
if(isset($_GET['v']) && $_GET['v'] == 1)
If you're running an old version of PHP you'll have to make two separate if statements for each of the two conditions.
Make sure you have display_errors turned on.
ini_set('display_errors',1);
Just to make sure there's nothing going horribly wrong.

Categories