index.php?p=whatever
Moderator: General Moderators
index.php?p=whatever
Hi,
My pages look exactly the same except for some text. How do I base my site on index.php so the url for a page called "lol" would look like
http://mydoamin.com/index.php?p=lol
?
Thanks...
My pages look exactly the same except for some text. How do I base my site on index.php so the url for a page called "lol" would look like
http://mydoamin.com/index.php?p=lol
?
Thanks...
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: index.php?p=whatever
You could do something like:
Code: Select all
if(isset($_REQUEST['p']) && !empty($_REQUEST['p']))
{
$page=$_REQUEST['p'];
} else {
$page="home";
}
include("pages/" . $page . ".php");
- markusn00b
- Forum Contributor
- Posts: 298
- Joined: Sat Oct 20, 2007 2:16 pm
- Location: York, England
Re: index.php?p=whatever
Something like:
Aceconcepts beat me to it!
Code: Select all
$_Page = @$_GET['p']; # get the page
$_Dir = "views/"; # theoretical page where view pages are stored.
if(file_exists($_Dir . $_Page))
{
include($_Dir . $_Page) or die("Could not include file");
}
else
{
echo "File not found";
}
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: index.php?p=whatever
hahaha
mine was rather crude though
mine was rather crude though
Re: index.php?p=whatever
<<
I was messing around with this code so much and I can't get it to work.
The directory I want to get the includes from is "install" and the page is "install.php"
I was messing around with this code so much and I can't get it to work.
The directory I want to get the includes from is "install" and the page is "install.php"
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: index.php?p=whatever
Remeber the include will be relative to you index file
Code: Select all
//include without url variable
include("install/install.php");
//include with url variable
include("install/" . $page . ".php");
Re: index.php?p=whatever
I meant the file retrieving the includes was install.php 
But I used the code you gave and made a short one for the url.
And it works.
Simple
Thanks!
But I used the code you gave and made a short one for the url.
Code: Select all
$page = $_GET['page'];
include("install/" . $page . ".php");Simple
Thanks!
- markusn00b
- Forum Contributor
- Posts: 298
- Joined: Sat Oct 20, 2007 2:16 pm
- Location: York, England
Re: index.php?p=whatever
You'll hit an ugly warning if the file you include isn't found.DJMiles wrote:I meant the file retrieving the includes was install.php
But I used the code you gave and made a short one for the url.
And it works.Code: Select all
$page = $_GET['page']; include("install/" . $page . ".php");
Simple
Thanks!
You should always do some error handling - good practice and it makes things prettier; pretty is always good.
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: index.php?p=whatever
markusn00b is right. Whenever you have successfuly completed a segment of code etc... always think about how errors could arise from the way you have coded. i.e.what if a file you want to include does not exist on the server or has been moved for some reason. Try to cater for as many constraints as possible (within reason)markusn00b wrote: You'll hit an ugly warning if the file you include isn't found.
You should always do some error handling - good practice and it makes things prettier; pretty is always good.
Re: index.php?p=whatever
All cited solutions are vulnerable to local file includes.
The correct way:
Compare $_GET['p'] (after you're sure it exists and is string) to an array of possible values. Include the file only if it is in the array.
The correct way:
Compare $_GET['p'] (after you're sure it exists and is string) to an array of possible values. Include the file only if it is in the array.
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: index.php?p=whatever
With reference to DJMiles' intial thread it was pretty clear that he was new to url variables and crude examples often help with the understanding of the logic used.DJMiles wrote:Hi,
My pages look exactly the same except for some text. How do I base my site on index.php so the url for a page called "lol" would look like
http://mydoamin.com/index.php?p=lol
?
Thanks...
However, I do like your idea and with time I think all programmers find new and more efficient ways of coding