473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

New to PHP, need help with basic page

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
6 1785
kovik
1,044 Expert 1GB
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
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
1,044 Expert 1GB
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
$_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
1,044 Expert 1GB
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
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

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

Similar topics

1
by: JaNE | last post by:
Hello, I have made my cms... and is working, but have some, let me say "bugs"... And I don't know all reasons, please allow me slightly longer and most probably confusing post (that "confusing" is...
4
by: Steve Richfie1d | last post by:
I wrote my first BASIC compiler when Bill Gates was going to Lakeside School, and am believed to be the original inventor of the ON ERROR statement. Now, my son wants to learn Visual Basic, but...
35
by: Boobie | last post by:
I need to escape HTML chracters so <test> --> &lt;test&gt; Looks like there is no built-in JS function...anyone got one handy ? thanks
0
by: Nigel | last post by:
I successfully create a .NET Component (Visual Basic .NET) that would print, unfortunately when used within a web browser it appears that .NET security doesn't allow you to run code that interacts...
17
by: freemann | last post by:
Can anyone provide example code showing how to send form results to a results page, email and a comma delimited file? Notice that I need it going to all three locations. Details: I have forms...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
2
by: notAclueabouthtml | last post by:
Hi all, Well, I think that I have bit off more than I can chew. I believe that I would benefit from a private tutor of sorts. but here goes. I am in charge of my kids middle school "giving fund"....
10
by: paulie | last post by:
Hi, I have been experiencing an issue when trying to use AJAX to reload a DIV area using a timer of 2000ms, which contains a html page with another DIV and javascript. Scenario -------------...
2
by: swethak | last post by:
hi , i write the code in .htm file. It is in cgi-bin/searches/one.htm.In that i write a form submitting and validations.But validations are not worked in that .htm file. I used the same code in my...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.