PHP equivalent to jsp:include

PHP has include and require_once which are equivalent to JSP's include directive(<%@ include ..%>) JSP also has a jsp:include which only includes the output from the included file, keeping the included file in a servlet of its own.

I am looking for something similar in PHP, so that the main page's variables and other content don't mess with those of the included files. Does one exist?

-------------Problems Reply------------

An easy solution is to include the file inside a function to prevent the scope of the file from littering the global namespace.

function jsp_include($file) {
include($file);
}

Doing it in OOP manner should also do the trick, in a more neatly manner.

servlet.php

class Servlet{

private $servletVar1 = "Some string";
private $servletVar2 = 2150;

public function html(){
echo "<p>Hello World!</p>";
}

}

main.php

include("servlet.php");

class MainPage{

private $title = "Page Title";

public function html(){
echo "<!DOCTYPE html>";
echo "<html>";
echo "<head>";
echo "<title>".$this->title."</title>";
echo "<head>";
echo "<body>";
$servlet = new Servlet();
$servlet->html();
echo "</body>";
echo "</html>";
}

}

$page = new MainPage();
$page->html();

You could always do a file_get_contents() and call the URL of that PHP script on your server, and then echo the results. I would caution you though that this is very bad from a security standpoint. If your DNS records get changed and what not, someone could really mess with things. It is best to avoid this problem altogether by using OOP as "thephpdeveloper" suggested. You can also use namespaces.

There's Runkit_Sandbox

Instantiating the Runkit_Sandbox class creates a new thread with its own scope and program stack. Using a set of options passed to the constructor, this environment may be restricted to a subset of what the primary interpreter can do and provide a safer environment for executing user supplied code.

But I've never used it and therefore cannot say how reliable it is.

Category:php Views:0 Time:2010-08-06
Tags: php include

Related post

  • JSP Including A Private Servlet 2011-12-12

    I have a servlet that responds to a URL and then forwards to a JSP in a typical MVC pattern. Many pages share the same page head so in the JSP there is an include to head.jsp head.jsp is placed inside WEB-INF so that it cannot be accessed directly. N

  • parameterised jsp:includes of stripes actions? 2008-10-06

    I've been trying to solve this, and have been getting stuck, so I thought I'd ask. Imagine two ActionBeans, A and B. A.jsp has this section in it: ... <jsp:include page="/B.action"> <jsp:param name="ponies" value="on"/> </jsp:include

  • How to Include a file outside the application (war) using jsp include 2009-05-06

    I am using "jsp:include" to include a static file in one of my jsp files. It works fine when the static html file is located inside the application folder. However if its kept outside the application folder it is not included in the JSP file. Note: I

  • How Do I Control Handling of Exceptions Thrown Inside jsp:include Includes? 2009-05-13

    I have a JSP, which includes another JSP via <jsp:include>. I also have (what I think is) a standard error page setup. The problem is that when an exception occurs inside the included file, it doesn't "bubble up" to the including JSP. Instead,

  • jsp:include in netbeans 2009-06-24

    I'm using JSF in NetBeans. All I want to do is to include a page within another page. But whatever I tried, and when I run the main page, I get no error but I can't see my included page in a main page. Why? My main page is: <jsp:root version="2.1"

  • Netbeans, jsp:include 2009-06-24

    I'm using JSF in NetBeans. All I want to do is to include a page within another page. But whatever I tried, and when I run the main page, I get no error but I can't see my included page in a main page. Why? My main page is: <jsp:root version="2.1"

  • HTML output with jsp:include and json-taglib 2009-09-25

    I'm using jsp:include inside json-taglib's json:property element. The problem with this is that all HTML elements from included JSP page gets stripped at some point and only plain text remains. I have already stripped all newlines so the result shoud

  • Possible to jsp:include a jsp from another package instead of from WebContent/jsp folder? 2009-12-09

    Due to a design decision, our project contains JSP files that include other JSP files like so: <jsp:include page="/jsp/controls/InputControl.jsp"/> These JSP files exist under the WebContent/jsp folder of the project. Is it possible to move the

  • Redirection from jsp:include page 2009-12-21

    I want the user to be redirected to Login.jsp if the "role" stored in Session Scope doesn't match with the Request Parameter "accessRole" HomePage.jsp <jsp:include page="Header.jsp"> <jsp:param value="d" name="accessRole" /> </jsp:incl

  • Logging every jsp:include 2010-01-08

    I want to log every <jsp:include> tag. Does the JavaServer Pages Standard Tag Library (JSTL) support logging and if so, how do I enable it? --------------Solutions------------- You can implement a Filter and configure its mapping as following:

  • WAMP equivalent for JSP 2010-01-09

    I already have WAMP server installed in my system which makes life much easier :-). So can anyone tell me what is the WAMP server equivalent for JSP? --------------Solutions------------- WAMP basically stands for "Windows, Apache HTTP Server, MySQL D

  • How do I get Eclipse to look in the build path for JSP includes instead of the web source? 2010-02-27

    We have a portion of our Dynamic Web Application that gets unpacked from a WAR, and placed in the build directory during compile. Because some of the files included in the web source are references to files from that WAR, then Eclipse can't find them

  • weird error in IE in central error page using jsp:include J2EE app 2010-03-09

    I dont have exact words how to explain this error in IE but will try best...here goes. I've following in web.xml <error-page> <exception-type>javax.servlet.ServletException</exception-type> <location>/errorpages/Error.jsp</

  • JSP request parameter is returning null on a jsp include with Weblogic 2010-05-12

    I am having trouble with the jsp:include tag. I have code like the following: <jsp:include page="./Address.jsp"> <jsp:param value="30" name="tabIndex"/> <jsp:param value="true" name="showBox"/> <jsp:param value="none" name="displ

  • Why would I use a templating engine? jsp include and jstl vs tiles, freemarker, velocity, sitemesh 2010-07-02

    I'm about to choose to way to organize my view (with spring-mvc, but that shouldn't matter much) There are 6 options as far as I see (though they are not mutually exclusive): Tiles Sitemesh Freemarker Velocity <jsp:include> <%@ include file=

  • Passing EL expressions or managed bean instance in jsp:include 2010-07-21

    I have a generic buttons jsp: <wow:button id="addButton" iconClass="add16 icon16x16" action="#{managedbean.addNew}" type="submit" immediate="true" value="#{lblMsg.label_add }" /> <wow:button id="deleteButton" iconClass="iconCancel" action="#

  • jsp:include , performance, modularization, alternatives and best practices, Part 96 2010-07-28

    This is a follow-up to the 'overhead of jsp include' question below: http://stackoverflow.com/questions/1479218/jsp-performance-using-jspinclude In our application, developers have 'modularized' jsp fragments by heavy use of "jsp:includes" for "commo

  • php's Header equivalent in JSP 2010-07-29

    in php i used to authenticate whether a user was logged in or not by checking the session for a member id, if set ok, else the page would be redirected via Header to the login page. this auth script was in auth.php and i used to include it in any pag

  • jsp:include not working with Expression Language (JSP version 2.5, EL enabled) 2010-08-17

    I've read: http://stackoverflow.com/questions/2168832/expression-language-in-jsp-not-working but it's slightly different. Using EL for referencing file works fine like the one below. <link rel="stylesheet" type="text/css" href="${pageContext.reque

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.134 (s). 11 q(s)