Davka Posted November 26, 2006 Share Posted November 26, 2006 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. Quote Link to comment Share on other sites More sharing options...
trq Posted November 26, 2006 Share Posted November 26, 2006 [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. Quote Link to comment Share on other sites More sharing options...
Davka Posted November 26, 2006 Author Share Posted November 26, 2006 My bad - the link actually reads "href=container.php?content=SomePage" Quote Link to comment Share on other sites More sharing options...
Davka Posted November 26, 2006 Author Share Posted November 26, 2006 [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<?phprequire($_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. Quote Link to comment Share on other sites More sharing options...
Davka Posted November 26, 2006 Author Share Posted November 26, 2006 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. Quote Link to comment Share on other sites More sharing options...
Davka Posted November 26, 2006 Author Share Posted November 26, 2006 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! Quote Link to comment Share on other sites More sharing options...
trq Posted November 26, 2006 Share Posted November 26, 2006 [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] Quote Link to comment Share on other sites More sharing options...
Davka Posted November 26, 2006 Author Share Posted November 26, 2006 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:<?phprequire("$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? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 26, 2006 Share Posted November 26, 2006 [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 Quote Link to comment Share on other sites More sharing options...
Davka Posted November 26, 2006 Author Share Posted November 26, 2006 Nice.Thanks, Ken! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.