My problem is that I try to include a class using include_once(); but instead of including the desired file, it prints the code in the file straight into the source html so the code appears on my webpage.
This is the code:
<?
include_once('php/class.loadClients.php');
$client = new loadClient();
$client->connect();
?>
I'm not sure why its doing this as I've used include_once(); elsewhere in this page and it works fine:
include_once('php/class.register.php');
Thanks in advance!
Open php/class.loadClients.php, and add <? to the beginning of it.
To add to Paulpro's answer: if his solution does not work your server might not have short tags enabled ( <? vs <?php ). Use the full <?php tag.
Related
I want to use include straight in define in php
for e.g.
<?php define('panel' , 'include "../panel/admin.php";'); //in a saperate php page ?>
and include this file
Now I want use
<?php panel ?> //in another page
you cant use include() in define
you must do this
<?php define("panel" , "../panel/admin.php"); ?>
and then include it
<?php
include(panel);
?>
mohade's answer is correct.
However you can do the same with your existing code by below way.
define('panel' , 'include "/panel/admin.php";');
eval(panel); // eval — Evaluate a string as PHP code
Hope it will help you :)
If I have a piece of code that reads a chunk of HTML from a txt file and then echos that html onto the page, how can I accomplish the same task, but when there is PHP inside of the txt file?
ex:
this is the file being read:
<?php
$filecontent = // read some other file
echo($filecontent);
?>
and this is the page that is reading the file:
<?php
$code1 = //reading the above file
?>
<html>
<?php echo($code1); ?>
</html>
When you want to process files containing PHP code you need to use include instead of echo.
<?php include('your_php_file_name'); ?>
If you have the contents of the file in a string you are in a tough spot because the only way to process the code is eval, and in addition you have to properly set up any environment that the code requires. eval itself should be avoided, and the latter is impossible to do in the general case.
Use include instead of echo:
<?php include($file_that_contains_php); ?>
you need to include the first file and echo statement in the first file will get executed.
<html>
<?php require_once("firstfile.php"); ?>
You need to echo htmlentities($code1), because when you echo then browser will not show it contents, because it try to parse it as a html tag, but htmlentities will encode to safe html output this characters.
If you want to evaulate the code, then you need eval($code1) or include it.
I have a .php file, say "index.php" . This file is being generated dynamically and I want to add the following code at the beginning of the file.
<?php
session_start();
require_once('login/auth.php');
require_once('config.php');
?>
Intention is to force user to sign up or sign in before they see this file. But I wasted a whole day and turned into nothing as resultant file is blank. what I found is , its facing problem with <?php and ?>
if I replace them with their equivalent entities <?php and ?> then they gets added easily but its not what I wanted.
So , question is how to add this original code to the beginning on file dynamically. Can someone help please?
<?php
$preamble = <<<EOL
<?php
session_start();
yada;
yada;
yada;
?>
EOL;
file_put_contents('index.php', $preamble);
How about the following:
<?php
file_put_contents('index.php', " <?php
session_start();
require_once('login/auth.php');
require_once('config.php');
?>");
Edit: Marc's idea to use heredoc syntax may be better in your situation, given the variety of text you may want to write to a file.
Is it possible to include a php file without including its contents? I just want to access the functions and variables in that file without displaying any content. I tried this
<?
ob_start();
include('$file');
ob_end_clean();
?>
But this will hide only contents in php tag. I want to know how to hide others as well.
How to hide? Redesign your solution and separate your concerns! Do not mix logic with UI and so on.
Maybe you should apply the MVC or similar pattern(s).
While I totally agree with Peter's answer
I just tried this because I've never tried it before..
File toinclude.php:
<p>Loads of text</p>
<?php
function my_test()
{
echo 'Hello';
}
?>
Ooh a link
File includer.php:
<?php
ob_start();
include('toinclude.php');
ob_end_clean();
my_test();
?>
And it does work!
Output:
Hello
No you can't.
Include is meant to execute everything inside the file, there are no ways to prevent execution of some part of the file.
The only way is to edit the included file.
If your file is not printing any output You may try eval($fileContent).
I have a php page that generates all the html and echo's it. Now I want to write a php script that I can use include to handle the footer code. that way if I need to update the footer on all the pages I can just edit the code in the included page.
But when I use include("footer.php"); and the footer page contains the footer code that works if it exists on the page it breaks and prints the code. Im very confused as too why?
if(isset($_SESSION['user_cart']) && count($_SESSION['user_cart']) > 0)
Starts writing the code on the page if I include from 0)
Please help?
EDIT: The code in the footer is wrapped in <?php ?>
You need to wrap that code in footer.php with php tags.
<?php
if(isset($_SESSION['user_cart']) && count($_SESSION['user_cart']) > 0) ...
?>
Dont your short hand notation of php tags <? ?>, use <?php ?>instead...
It sound like you didn't start php file with
<?php
Please make sure that your footer file has <?php in the beginning.
I'm not sure if you stated your question very clearly.
If you are echoing code out it will appear as soon as you include the file. Your file should look like this:
<?php require_once 'header.php';
// content goes here
require_once 'footer.php'; ?>