[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 333
  • Last Modified:

Interpreting php in a variable?

Hi,

I want to store dynamic pages in a mysql database.

I can take a page of code that includes both php and html from a record/field in a database table and assign it to a variable ($var). How do I then get php to interpret the php code in that variable?

0
Frylock
Asked:
Frylock
  • 7
  • 3
1 Solution
 
Harisha M GCommented:
0
 
Richard QuadlingSenior Software DeverloperCommented:
It would be a better solution to cache the pages to hard disk and then use include/require. That way, you can also do all the error handling in the main page.
0
 
Richard QuadlingSenior Software DeverloperCommented:
The reasons for this for me is that the page is not really processable for a DB except for full-text searching. Which I doubt you are using on a source code file.

And a filing system for pages already exists! It is the disk!

You will be saving some processing time too.

Currently you have to connect to the DB to get the data. The data MAY be quite big! You can't sort or query the data in the DB. Using an include is a lot easier.

<?php
// Determine the page name based upon ID.
$s_page_name = md5($i_known_page_id . $s_magic_string);

// Determine if the file exists (just like seeing if you have any results from a query).
if (file_exists($s_page_name))
 {
 // Include the file.
 include $s_page_name;
 }
?>

You may only want the page once, so use

 include_once $s_page_name;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Richard QuadlingSenior Software DeverloperCommented:
The reasons for this for me is that the page is not really processable for a DB except for full-text searching. Which I doubt you are using on a source code file.

And a filing system for pages already exists! It is the disk!

You will be saving some processing time too.

Currently you have to connect to the DB to get the data. The data MAY be quite big! You can't sort or query the data in the DB. Using an include is a lot easier.

<?php
// Determine the page name based upon ID.
$s_page_name = md5($i_known_page_id . $s_magic_string);

// Determine if the file exists (just like seeing if you have any results from a query).
if (file_exists($s_page_name))
 {
 // Include the file.
 include $s_page_name;
 }
?>

You may only want the page once, so use

include_once $s_page_name;
0
 
FrylockAuthor Commented:
You make sense RQ - What does the MD5 thing mean? does that add the two things together?
0
 
Richard QuadlingSenior Software DeverloperCommented:
No.

What you don't want to have happen is people to guess the filenames.

Using md5 converts a known number into a random looking string.

Not really necessary ...

<?php
// Determine if the file exists (just like seeing if you have any results from a query).
if (file_exists($i_known_page_id)
 {
 // Include the file.
 include $i_known_page_id;
 }
?>

Is perfectly adequate.

0
 
FrylockAuthor Commented:
I am just wondering if I can put php into a CSS file. The problem is I hae a bunch of different templates for essentially teh same page and want to be able to flip all the modules in them about (and their different CSS pages) by using a single numeric variable
0
 
Richard QuadlingSenior Software DeverloperCommented:
I don't know the mechanism for determining the page to be shown.

If it is something like

http://www.site.com/showpage.php?page_id=1234

then as long as the pages are NOT in the webroot then you should be ok.

By this I mean ...

/public_html/showpage.php
/public_html/1234


If you want to be REALLY clever, then try this idea.

You need to use SESSIONS.

So, a page is loaded that requires other pages. Each real pages id is stored in the session.

Say we need pages 1, 5 and 434.

As you draw tha pages with the links to the subpages you store the real page id in the session

Start with clearing the stores pageids

unset($_SESSION['page_ids']);

Then store the page ids as you create the HTML with the links.

$_SESSION['page_ids'][] = 1;

then

$_SESSION['page_ids'][] = 5;

and later on/finally

$_SESSION['page_ids'][] = 434;


Now the HTML you create relates to the offset in the session..

http://www.site.com/showpage.php?page_id=0
http://www.site.com/showpage.php?page_id=1
http://www.site.com/showpage.php?page_id=2

In your showpage.php code you ...

<?php
define ('PAGE_CACHE', '/home/sites/my_site/page_cache/');

session_start();
if (
      isset($_GET['page_id']) && 
      isset($_SESSION['page_ids']) &&
      (intval($_GET['page_id']) >= 0) && 
      (intval($_GET['page_id']) < count($_SESSION['page_ids'])) &&
      file_exists(PAGE_CACHE . $_SESSION[$_GET['page_id']])
      )
      {
      include_once PAGE_CACHE . $_SESSION[$_GET['page_id']];
      }
?>

sort of thing.

Now, if the page ids are NOT numeric, but something sensible for you, the names are NEVER released in the outbound HTML. If the session has not generated them then the user cannot click on them.

They HAVE to come to a page which generates the session to be able to access the pages.

And as a side benefit, links don't work. Users can simply call up the direct page without first having come to a proper page.

0
 
Richard QuadlingSenior Software DeverloperCommented:
I use a script called css.php

This is requested by a small JS which supplies me with the screen resolution so I can get the correct background image to fit the display for the user. I also have it to use a different CSS for email. This is for intranet with IE6/FF1.5 and Outlook, so I know the things I can do.


The last few lines of my script ...

// Return the new Logo CSS with the correct formatting.
header('Content-type: text/css');
echo $s_built_css;
?>


NOTE: Supply the correct content type and you can output anything.

The caller to this is ...

<script type="text/javascript" language="JavaScript"><!--
document.write('<link title="StyleSheet" rel="stylesheet" type="text/css" href="/global/css.php?W=' + window.screen.width + '&amp;H=' + window.screen.height + '">');
//--></script>
</head>


0
 
FrylockAuthor Commented:
That seems way way way too complex.

Each of my templates starts with a number - 1 through 15.

My artist gave me files for each that are all named anylink.css

What i can do is rename them to 1anylink.css, 2anylink.css, etc.

Then, in the <head>, I can just do some php to echo out the right anylink.css by attaching my numeric variable to the front of it. Doesn't that make sense?
0
 
Richard QuadlingSenior Software DeverloperCommented:
<link title="StyleSheet" rel="stylesheet" type="text/css" href="/css.php?id=1" />

And then in css.php you can first make sure that $id has been supplied or default to 1.
Then you can check to see if the css exists or default to 1.
Then you can echo the content of the css with the appropriate header.

I think it depends upon how much work you have to do.

I had to create a mechanism which hid data from the browsers.

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now