Page 1 of 1
PHP Link
Posted: Tue Feb 05, 2008 3:17 am
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>
Re: PHP Link
Posted: Tue Feb 05, 2008 3:39 am
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?
Re: PHP Link
Posted: Tue Feb 05, 2008 11:22 am
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';
}
?>
Re: PHP Link
Posted: Tue Feb 05, 2008 7:04 pm
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> </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???