Xdega Posted March 29, 2012 Share Posted March 29, 2012 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 https://forums.phpfreaks.com/topic/259976-running-code-from-another-page-in-constructor-for-a-class/ Share on other sites More sharing options...
batwimp Posted March 29, 2012 Share Posted March 29, 2012 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 https://forums.phpfreaks.com/topic/259976-running-code-from-another-page-in-constructor-for-a-class/#findComment-1332525 Share on other sites More sharing options...
seanlim Posted March 29, 2012 Share Posted March 29, 2012 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 https://forums.phpfreaks.com/topic/259976-running-code-from-another-page-in-constructor-for-a-class/#findComment-1332528 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.