By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,695 Members | 1,651 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,695 IT Pros & Developers. It's quick & easy.

New to PHP, need help with basic page

P: 7
Hi. I'm trying to create a basic template for a page, so that I can have the headers and footers stay the same, and only change the center content.

I've already figured out how to do the includes part, but that requires I do that on every page on the site I am making.

Is there a way I can have a single index page, which loads different content into the center depending on what link is clicked, rather than clicking a link taking it to a different page?

This is what I have so far, and it works to load the current page, but I don't know how to tell it that :

1) IF there's no ?page=somepagehere after the index.php, then just load the "content" page

2) I want clicking on a link to keep you on index.php, but load "someotherpage" into where "content" is, so that the url is http://etcetc.etc/index.php?page=someotherpage

so how do I make it so that there can be different values for $page, or am I going about this all wrong? I don't need anything complicated, just a page with a header and footer, which I already have working, but that's able to change the content in the middle

I'm not sure if I'm making sense...

Here's what I have so far...

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  4.     <title>test page title</title>
  5. <link href="style.css" rel="stylesheet" type="text/css">
  6. </head>
  7. <body>
  8. <!--mainWrapper encloses the whole page --->
  9. <div id="mainWrapper">
  10. <!--headerWrapper encloses the header and navigation code --->
  11. <?php
  12. require($DOCUMENT_ROOT . "/2007test/header.html");
  13. ?>
  14. <!--content encloses EVERYTHING between the header and the footer --->
  15.  
  16. <?php
  17. $page="content";
  18. require($DOCUMENT_ROOT . "/2007test/$page.html");
  19. ?>
  20.  
  21.  
  22. <!--footer encloses the footer information and bottom navigation -->
  23. <?php
  24. require($DOCUMENT_ROOT . "/2007test/footer.html");
  25. ?>
  26. </div><!--this tag closes the mainWrapper div-->
  27. </body>
  28. </html>
Jul 26 '07 #1
Share this Question
Share on Google+
6 Replies


kovik
Expert 100+
P: 1,044
What you are after is using the GET variable "page" in the URL (index.php?page=foo). Like so:

[php]require($_GET['page'] . '.php');[/php]

But before you go off using this, STOP. DO NOT -- I repeat -- DO NOT TRUST USER INPUT! Anything sent through a GET or POST request is considered user input, as all of it can be altered. If I were to go to index.php?page=path/to/malicious/code.php, I could inject my own code into your website, or open files that would normally be off-limits. This a method that a lot of programmers that are new to templates like to do (I've done it before as well), but they don't think about the security holes.

What you want to do is specify all valid values, and ignore anything else.

[php]if(!empty($_GET['page']))
{
switch($_GET['page'])
{
case 'index':
case 'foo':
case 'bar':
case 'doo':
case 'otherpage':
require($_GET['page'] . '.php'); // Include a *valid* page
break;
default:
require('error.php'); // Display some error page
}
}[/php]

This will only allow pages that meet one of the cases to be considered valid, and all others not.


I'm planning on writing a basic templating engine tutorial (beyond this kind), and then a more advanced one farther in the future after I complete my current project. If you leave me a PM, I can notify you with the first tutorial is complete.
Jul 26 '07 #2

P: 7
Ok, thank you... I have a couple questions based on your response...

the part that says " if empty, get page"... do I have to define $page somewhere, like a variable? or is page just a label here that will be applied?

also, the individual "cases" do those names have to be defined somewhere so that it knows what page name to look for? or does it look for that name in the directory and add ".php" after it?

where your comment says //get a valid page... do I need to do anything with that line? or do I only need to change the default page line, to get my default page to show up when no other page is selected?

and when a different "page" is selected from a link, will it still load only in the center section of the index page? or am I still going to have to add the code to call the header and footer in all the pages?

Ideally, the "pages" that are listed for what you are showing me, shouldn't have to have any header or footer tags, right? but should be able to be straight content?




What you are after is using the GET variable "page" in the URL (index.php?page=foo). Like so:

[php]require($_GET['page'] . '.php');[/php]

But before you go off using this, STOP. DO NOT -- I repeat -- DO NOT TRUST USER INPUT! Anything sent through a GET or POST request is considered user input, as all of it can be altered. If I were to go to index.php?page=path/to/malicious/code.php, I could inject my own code into your website, or open files that would normally be off-limits. This a method that a lot of programmers that are new to templates like to do (I've done it before as well), but they don't think about the security holes.

What you want to do is specify all valid values, and ignore anything else.

[php]if(!empty($_GET['page']))
{
switch($_GET['page'])
{
case 'index':
case 'foo':
case 'bar':
case 'doo':
case 'otherpage':
require($_GET['page'] . '.php'); // Include a *valid* page
break;
default:
require('error.php'); // Display some error page
}
}[/php]

This will only allow pages that meet one of the cases to be considered valid, and all others not.


I'm planning on writing a basic templating engine tutorial (beyond this kind), and then a more advanced one farther in the future after I complete my current project. If you leave me a PM, I can notify you with the first tutorial is complete.
Jul 26 '07 #3

kovik
Expert 100+
P: 1,044
the part that says " if empty, get page"... do I have to define $page somewhere, like a variable? or is page just a label here that will be applied?
$_GET is not a command, it is a variable. $_GET is a array consisting of GET requests, which are defined in the query string of a URL. So, if you were to go to a page who's URL was page.php?foo=1&bar=somevalue, then $_GET['foo'] would equal '1', and $_GET['bar'] would equal 'somevalue.'

If you are not familiar with the superglobals ($_GET, $_POST, $_SESSION, $_COOKIE, etc.), then I suggest that you look into the PHP manual and read some tutorials.

also, the individual "cases" do those names have to be defined somewhere so that it knows what page name to look for? or does it look for that name in the directory and add ".php" after it?
The cases are all defined by you. You should know exactly what files you have. The cases define what $_GET['page'] should equal in order for the include to be performed.

If you are not familiar with switch...case statements, I'd suggest you look into that as well.

where your comment says //get a valid page... do I need to do anything with that line? or do I only need to change the default page line, to get my default page to show up when no other page is selected?
You need to customize the entire script to work under your specific circumstances. What I have given you is a very simple example.

and when a different "page" is selected from a link, will it still load only in the center section of the index page? or am I still going to have to add the code to call the header and footer in all the pages?

Ideally, the "pages" that are listed for what you are showing me, shouldn't have to have any header or footer tags, right? but should be able to be straight content?
I'm not sure that you are understanding how this code works. All of your links must look like:

index.php?page=somePage

Then, that would fill $_GET['page'] with whatever value you put in place of 'somePage.' Then, that value would be passed into the switch...case statement, and checked against the cases that you have defined to be valid.

I'd really suggest you read farther into the workings of the language before you concern yourself with programming templates. A lot of us get into PHP after already learning a high-level language such as C++, so we already know these operations.
Jul 27 '07 #4

P: 7
$_GET is not a command, it is a variable.
lol... I know that... I was being lazy and not typing out the whole line that I was asking about

The cases are all defined by you. You should know exactly what files you have. The cases define what $_GET['page'] should equal in order for the include to be performed.
ok, I didn't explain what I was asking very well... When I asked, I was wondering if I had to define something like "foo" equals "/directoryname/foo.php" before the cases would know that it was foo.php and not foo.htm... But I already figured that part out... I was getting myself confused with remembering some script I used once that had me define every little detail... lol


I'm not sure that you are understanding how this code works. All of your links must look like:

index.php?page=somePage
I understand this part perfectly... what I was having trouble with was what to do if someone simply typed in index.php, without the ?page=somePage after it, I wanted it to have a default page to load, and you helped me with that, by using an "else" statement... I had tried the else statement before, but had part of the syntax wrong (I forgot the brackets around it)...

Thanks again for all your help... I just needed a nudge in the right direction, and you gave me just what I needed so that I could research and figure out the rest of the customizations I needed...
Jul 27 '07 #5

kovik
Expert 100+
P: 1,044
Yeah, that post was made before I checked my PMs, at which point I realized that you knew what you were doing.
Jul 27 '07 #6

P: 7
Yeah, that post was made before I checked my PMs, at which point I realized that you knew what you were doing.

*winks* I appreciate the help... it's working perfectly now..
Jul 30 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.