PHP Link

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shak
Forum Newbie
Posts: 2
Joined: Tue Feb 05, 2008 3:09 am

PHP Link

Post by shak »

Hi Folks! I m kinda new to PHP. Can anyone tell me how to load different page while using index.php as page container. For example, lets say I have a page index.php and I want to load another page called contact.php in the browser.

In fact, I dont know how to implement the following:

index.php? <what should be written here so that my index page wont be changed>
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: PHP Link

Post by aceconcepts »

Hi,

When you use the url to change pages you are in fact passing variables via the url.

i.e.

Code: Select all

site.com/index.php?page=contact_us
The above link will pass a variable named 'page' with a value of "contact_us".

In your index page you will need to get the 'page' variable from the url in order to display the page "contact_us". This is how you do it:

Code: Select all

 
if(isset($_REQUEST['page']) && !empty($_REQUEST['page']))   //CHECK WHETHER A URL VARIABLE HAS BEEN SET AND IS NOT EMPTY
{
   $page=$_REQUEST['page'];   //SET LOCAL VARIABLE EQUAL TO URL VARIABLE
}
   else
{
   $page="home";   //IF NO URL IS PASSED VIA URL THEN SET DEFAULT PAGE
}
 
   include"directory/to/your/files/" . $page . ".php";   //THIS LINE WILL DISPLAY THE $page
 
Does it make sense?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PHP Link

Post by RobertGonzalez »

If you know the variable is coming by way of the query string use the $_GET superglobal value.

Code: Select all

<?php
if (! empty($_GET['page']) {
  $page = $_GET['page'];
 
  //... validate the crap out of the variable to make sure it is safe
  include "$page.php";
} else {
  include 'default.php';
}
?>
shak
Forum Newbie
Posts: 2
Joined: Tue Feb 05, 2008 3:09 am

Re: PHP Link

Post by shak »

Everah & aceconcepts, Thanks for your help but I am still confused. aceconcepts, how would I add the pages. I wont have to have their extension end up with .php?
<?php
if (!defined('WEB_ROOT')) {
exit;
}
?>
<p>&nbsp;</p>
<div align="center"><img src="images/headerImagefront2-final.png" alt="Header Image"></div>
<div align="center"><font color="#FFFFFF"><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Home</a> | Services | About | Contact | Shipping</font></div>
<br />

<?php

if(isset($_REQUEST['page']) && !empty($_REQUEST['page'])) //CHECK WHETHER A URL VARIABLE HAS BEEN SET AND IS NOT EMPTY
{
$page=$_REQUEST['page']; //SET LOCAL VARIABLE EQUAL TO URL VARIABLE
}
else
{
$page="home"; //IF NO URL IS PASSED VIA URL THEN SET DEFAULT PAGE
}
include "other_files/" . $page; //THIS LINE WILL DISPLAY THE $page
?>

How would I add pages for Services, About, Contact_us etc???
Post Reply