Jump to content

[Resolved - sort of] Pulling variable from required include?


Davka

Recommended Posts

Ok, I've got a page we'll call container.php

There are a number of includes on this page. The one I'm concerned with is this:

<?php require ("$content.html"); ?> . . . which I'm calling via a link in a navbar (which is also required in container.php) - the link sez "href=container.php?SomePage"

So far so good. Simple as can be. So here's my question: I want to drop a variable into SomePage.html, like so:

<?php $pagename="Some Page"; ?>

. . . and call this from the head of container.php, like so:

<Title>My Website <?php echo "| $pagename"; ?></Title>

This should be giving me 'My Website | Some Page' in the title bar, afaik. But instead I just get 'My Website |' and the $pagename variable is ignored. I'm guessing the problem is that the SomePage.html include is loading [i]after[/i] the header in container.php, so the variable simply doesn't exist as far as container.php is concerned. Any ideas?

And yes, I am an ultimate php newb.
Link to comment
Share on other sites

[quote]
<?php require ("$content.html"); ?> . . . which I'm calling via a link in a navbar (which is also required in container.php) - the link sez "href=container.php?SomePage"[/quote]

This whole approuch is VERY insecure, I hope your validating the page exists before trying to include it. Otherwise anyone can run there own php script on your server.

As for your problem. Is your server setup to parse html pgaes as php? Seeems a little odd. And yes... you would need to set the variable before calling require_once() to include the page.



Link to comment
Share on other sites

[quote]This whole approuch is VERY insecure, I hope your validating the page exists before trying to include it. Otherwise anyone can run there own php script on your server.[/quote]

Hmm - okay, would something like

<?php
require($_SERVER['DOCUMENT_ROOT'].'resources/$content.html');
?>

be better?

And is there any way to pull specific information from a required/included file without including the entire contents of that file? I don't want the same HTML in two places on my page - especially not in the document head.
Link to comment
Share on other sites

OK, I've dealt with the security issue. The code now verifies that the requested page is resident on my server. (if you're interested, I just added a variable $verify="http://www.myserverURL.com" to the head and changed the require to to $verify/somedirectory/$content.html)

I am still left with the original problem, which is this: Can I pull a variable and ONLY that variable from another file, without including the entire file?

I want to get the variable $pagename from the file $content, and drop it into the title tag: <Title>My Website <?php echo [$pagename from my current $content page] ; ?></Title> where [$pagename from my current $content page] is the, um, yeah . . . pretty self-explanatory.
Link to comment
Share on other sites

Well, I came up with a solution, although not to precisely the question I originally posed.

First I figured I'd just put the name of the document in the title, like so:

<title>My Website
<?php
$pagename = $_GET['content'];
echo "| $pagename";
</title>

. . . except my filenames are all lower case, so I used ucfirst() to capitalize the first letter:

<?php
$pagename = $_GET['content'];
$pagename = ucfirst($pagename);
echo "| $pagename";
?>

This worked nicely, except that some of the pages have two-word names with an underline in between, like this_page.html - That meant that the title bar would display My Website | This_page. Not what I wanted, so I exploded the string and used ucfirst to capitalize the first letters of the resulting array.

Then I just echoed the two resulting capitalized strings:

<?php
$pagename = $_GET['content'];
$words = explode("_",$pagename);
$first =  ucfirst ($words[0]);
$second = ucfirst ($words[1]);
echo "| $first $second";
?>

Perfection. page.html gives me "My Website | Page," while this_page.html gives me "My Website | This Page."

Any corrections to syntax or basic concepts would be appreciated.

So - IS it possible to pull only [b]part[/b] of the content of another page via an include or require tag? Seems like it would be extremely useful.

Thanks to all!
Link to comment
Share on other sites

[quote]So - IS it possible to pull only part of the content of another page via an include or require tag? Seems like it would be extremely useful. [/quote]

No. I usually wrap any output within a function, that way you can just call what you need.

I still don't really understand what your doing to check a page exists before including it, bevery carefull dynamically including pages, this really is one of the easiest security traps to fall into. Try something like...

[code]
<?php
  if (isset($_GET['page'])) {
    $page = $_GET['page'];
    if (file_exists($_GET['page'].'.php')) {
      include $_GET['page'].'.php';
    }
  }
?>
[/code]
Link to comment
Share on other sites

I'm not really checking to see if a page exists, just adding the URL of my site to the include tag.

In the head : <?php $checksource="http://www.domain_name.com"; ?>

And the include reads:

<?php
require("$checksource/$content.php");
?>

Seems to me this would stop someone from using href="MyPage.php?content=http://www.HackerWebsite.com/Gotcha" to run their php from my server, no?
Link to comment
Share on other sites

[quote]This worked nicely, except that some of the pages have two-word names with an underline in between, like this_page.html - That meant that the title bar would display My Website | This_page. Not what I wanted, so I exploded the string and used ucfirst to capitalize the first letters of the resulting array.

Then I just echoed the two resulting capitalized strings:

[code]<?php
$pagename = $_GET['content'];
$words = explode("_",$pagename);
$first =  ucfirst ($words[0]);
$second = ucfirst ($words[1]);
echo "| $first $second";
?>[/code]
[/quote]

There is a much easier way of doing the above. Use the [url=http://www.php.net/str_replace]str_replace()[/url] and [url=http://www.php.net/ucwords]ucwords()[/url] functions:

[code]<?php
$pagename = $_GET['content'];

echo '| ' . ucwords(str_replace('_',' ',$pagename));
?>[/code]

Ken
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.