Jump to content

Running code from another page in constructor for a class


Xdega

Recommended Posts

So I have this class:

 

<?php
class page
{
private $title;

	function __construct(){

//note: This code snippet (below) does not appear to be having the desired effect of 
//running the PHP code on page declared by p

 if(!isset($_GET['p'])) :
        include("../content/home.php");
       	else : 
        include("../content/{$_GET['p']}.php");
        endif;
///////////////////////////////////////////////
}

public function setTitle($t) {
	$this->title=$t;
}?>

 

and I need the following from the content page (the page declared by $_GET['p']) to be ran in the constructor:

 

<?php 
global $page; 
$page->setTitle("About Us");
?>

 

this is in order to set the page title, so I can use it elsewhere.

 

I hope my explanation (although fairly vague), is somewhat clear.

 

 

Any help on this?  I was told that using <<<EOF would be a potential requirement? but I am completely unfamiliar with that method.

Link to comment
Share on other sites

I'm not sure exactly what it is you're trying to accomplish. However, setting this up this way can create a security risk. You are basically allowing anybody to include any file in your /content directory (or other malicious things) just by typing its name in to the URL line. You should re-think your include strategy.

Link to comment
Share on other sites

Yup, it isn't a good idea to include files directly from ANY source of input.

 

If you have to, make sure you validate it and ensure that the filename is "allowed" to be included. You can do this by maintaining an array of allowed files and using the in_array function.

 

On to your actual question, if you want to access the member function of the Page class, you should be able to do so from the included file, as if the contents of the file were within the constructor i.e. $this->setTitle("About Us"); should call the setTitle function.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.