Forum Moderators: coopster

Message Too Old, No Replies

Including php code within a php variable

giving me all kinds of errors

         

madmatt69

2:18 am on Mar 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

I use a file right now with several variables which contain some html, for example:

$h = '<link rel="stylesheet" type="text/css" media="screen" href="/css/styles1.css" />'

Some of the code blocks have a lot more html in them, and in one in particular, I need to call some php from the ad software 'openx'.

Here's that code:

 define('MAX_PATH', '/home/mysite/public_html/openx');
if (@include_once(MAX_PATH . '/www/delivery/alocal.php')) {
if (!isset($phpAds_context)) {
$phpAds_context = array();
}
$phpAds_raw = view_local('', 3, 0, 0, '', '', '0', $phpAds_context);
echo $phpAds_raw['html'];
}

I can't figure out how to include that in the html variables! Everything I try results in syntax errors.

What I'd like it to look like is something like:

 $ads = '<div id="ads"> ' . 
define('MAX_PATH', '/home/mysite/public_html/openx');
if (@include_once(MAX_PATH . '/www/delivery/alocal.php')) {
if (!isset($phpAds_context)) {
$phpAds_context = array();
}
$phpAds_raw = view_local('', 3, 0, 0, '', '', '0', $phpAds_context);
echo $phpAds_raw['html'];
} . '
</div>';

And that's what keeps giving me the errors..What am I missing?

Thanks for any help!

Vis3R

12:29 pm on Mar 4, 2008 (gmt 0)

10+ Year Member



You cannot store php code in variables, it would be a big security issue if you could. Immagine people injecting your GETs and POSTs with php code. All you can save in variables are results you get from the php code.

Now.. what you could probably do, is something like an include.php file, which would accept a GET/POST variable, and depending on that variable it would then output the code needed. Below is an example as i doubt i've explained it good enough ;)

include.php

<?

$phpcode1 = '
<?
define('MAX_PATH', '/home/mysite/public_html/openx');
if (@include_once(MAX_PATH . '/www/delivery/alocal.php')) {
if (!isset($phpAds_context)) {
$phpAds_context = array();
}
$phpAds_raw = view_local('', 3, 0, 0, '', '', '0', $phpAds_context);
echo $phpAds_raw['html'];
}
?>';

$phpcode2 = '
<?
echo "whatever";
?>';

switch ($_GET['php'])
{
case 1:
echo phpcode1;
break;
case 2:
echo phpcode2;
break;
}

?>

And then in your main php you could do include("http://yourserver/include.php?php=1");
to get the php codes (you could put some html in there too) stored in variables in include.php to be executed. (the code above probably won't work it's just an example)

It is not a good idea but is the only one that came to mind to be able to store php code in variables... i suggest you find some other method because storing php code in variables doesn't seem like a good idea :)

[edited by: Vis3R at 12:36 pm (utc) on Mar. 4, 2008]

madmatt69

4:39 pm on Mar 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the response, I'll see what I can do.

THe idea behind having code in the php variables is that then I just include one file "functions.php" with all my code. Then a page on my site will include functions.php and grab any chunks of code out of it that it needs and place them in the appropriate spots.

In theory, it's more efficient than calling multiple include files.

I'll let you know if I come up with a solution!

PHP_Chimp

7:51 pm on Mar 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you had a look at eval [uk3.php.net]?

However there is a reason that eval and evil and only 1 letter away...as said in the first reply allowing arbitrary execution of php code can be a very big mistake. However there are some very cool uses of evil, sorry eval, in the user comments section ;)

Just noticed at you are on 666 posts, so maybe this function is exactly what you want ;)