Page 1 of 1
Beginner Q: Need help linking...
Posted: Sat Mar 05, 2005 2:35 pm
by majesticreality
I'm trying to make it so that when I click say the "contact" page, the URL stays as mysite.com/index.php . I was wondering if someone could tell me how to make the complete link to do that, my friend told me to do
Code: Select all
<?php
$thefile = "$page.php";
include($thefile);
?>
But I couldn't get it to work, maybe i'm retarded? Thanks for the help!
Posted: Sat Mar 05, 2005 2:47 pm
by John Cartwright
the most basic way to accomplish this is..
Code: Select all
include($_GETї'page'].'php');
and then you would have your.com/?page=pagename.
As you can tell pagename.php would have been executed. This is pretty bad practice though. You should generally validated any user input.
Code: Select all
$page = (empty($_GETї'page']) ? 'mainpage' : $_GETї'goto']);
This will check to see if 'page' is empty, if so assign it to 'mainpage' which is your welcome page (mainpage.php)
As for validating the field, you should generally have an array of all valid pages that you know of and check if the requested page exists and is approved
Code: Select all
$approved = array('mainpage','page423,'other','somepage');
and then check if it exists in the approved list
Code: Select all
if (in_array($page,$approved) && file_exists($page.'.php');
{
//this is where you insert the page
include($_GETї'page'].'php');
}
else
{
//include a file with an error
include('404.html');
}
Posted: Sat Mar 05, 2005 2:51 pm
by majesticreality
Thanks for the help Phenom, but if I use
Code: Select all
include($_GETї'page'].'php');
Where in the code do I put the text that i'm linking?
(who knew that adding a simple link would cause me the most trouble

)
Posted: Sat Mar 05, 2005 3:10 pm
by Todd_Z
You create files called for example
Home.php
Contact.php
News.php
Info.php
Then when you make a link like these:
<a href="
www.yoursite.com/?page=Home">Home</a>
<a href="
www.yoursite.com/?page=Contact">Home</a>
The value of home or contact, etc. will be stored in the variable $_GET["page"], so then when you include $_GET["page"].".php", you will include Home.php.
Posted: Sat Mar 05, 2005 5:28 pm
by Supremacy
wouldent it be more easy just to make a frameset on top of the site??
that way the url allways says
http://www.your-domain.com ?
Posted: Sat Mar 05, 2005 5:38 pm
by John Cartwright
Supremacy wrote:wouldent it be more easy just to make a frameset on top of the site??
that way the url allways says
http://www.your-domain.com ?
Short answer is, no. And why would you want to URL to be domain.com all the time...
Posted: Sat Mar 05, 2005 5:42 pm
by smpdawg
You need to be very careful when using a technique like this because all someone has to do is prepend some directory information and cause some arbitrary PHP script to execute.
For example:
I have just asked your script to run a script in an entirely different directory and its effects may be devastating.
But don't let that prevent you from using this technique, which some parameter handling you can ensure that script that is being requested is valid.
Posted: Sat Mar 05, 2005 5:44 pm
by John Cartwright
Phenom wrote:
and then check if it exists in the approved list
Code: Select all
if (in_array($page,$approved) && file_exists($page.'.php');
{
//this is where you insert the page
include($_GETї'page'].'php');
}
else
{
//include a file with an error
include('404.html');
}
Posted: Sat Mar 05, 2005 5:44 pm
by Supremacy
Phenom wrote:Supremacy wrote:wouldent it be more easy just to make a frameset on top of the site??
that way the url allways says
http://www.your-domain.com ?
Short answer is, no. And why would you want to URL to be domain.com all the time...
It's REALLY nice when your working with subdomains, that way you can hide your pages.

Posted: Sat Mar 05, 2005 5:46 pm
by smpdawg
I just wanted to reiterate that point because many people would write it off as trivial but it isn't.
Posted: Sat Mar 05, 2005 5:50 pm
by John Cartwright
and I was trying to explain one way to prevent it, as simple as possible..
What I was trying to get across was that you should validate.
Posted: Sat Mar 05, 2005 5:51 pm
by smpdawg
It is a very valid point and I wasn't trying to minimize the importance of what you said.