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 :)
Related
I am very new to PHP programming. I am trying to write this custom php code in Drupal and seeing this weird behavior. Basically I have two php files which users can hit and the first php file is showing output from the second one. I am not including (include) the second file in the first one.
Home.php - The first file (outputs 'why am i executing' at the end)
<?php include 'HomeView.BL.inc';
//Other links to access second file
?>
HomeView.BL.inc
<?php
include 'db.inc';
include 'dao.inc'; - has a class called IDA_Map
?>
FacultyInternshipDetail.php - The second file
<?php
include 'InternshipDetailView.BL.inc';
?>
InternshipDetailView.BL.inc
<?php
echo "why am i executing";
include 'db.inc';
include 'dao.inc';
?>
Apart from the output from the second file, I am also seeing this error - Cannot redeclare class IDA_Map.
I have read in other posts about 'include_once' but didn't expect re-declaration error since the class (IDA_Map) is declared once per request.
Thank you.
Flip all these to require_once.
<?php require_once'HomeView.BL.inc';
//Other links to access second file
?>
HomeView.BL.inc
<?php
require_once'db.inc';
require_once'dao.inc'; - has a class called IDA_Map
?>
FacultyInternshipDetail.php
<?php
require_once'InternshipDetailView.BL.inc';
?>
InternshipDetailView.BL.inc
<?php
echo "why am i executing";
require_once'db.inc';
require_once'dao.inc';
?>
include breaks when you try to include the same file twice.
Also look up composer and into using a php framework so you dont have to worry about any of this!
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).
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.
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'; ?>