index.php?p=whatever

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
User avatar
DJMiles
Forum Newbie
Posts: 5
Joined: Sat Apr 19, 2008 8:38 pm

index.php?p=whatever

Post by DJMiles »

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...
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: index.php?p=whatever

Post by aceconcepts »

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");
 
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: index.php?p=whatever

Post by markusn00b »

Something like:

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 beat me to it!
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: index.php?p=whatever

Post by aceconcepts »

hahaha

mine was rather crude though :D
User avatar
DJMiles
Forum Newbie
Posts: 5
Joined: Sat Apr 19, 2008 8:38 pm

Re: index.php?p=whatever

Post by DJMiles »

<<

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"
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: index.php?p=whatever

Post by aceconcepts »

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");
 
User avatar
DJMiles
Forum Newbie
Posts: 5
Joined: Sat Apr 19, 2008 8:38 pm

Re: index.php?p=whatever

Post by DJMiles »

I meant the file retrieving the includes was install.php :P

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");
And it works.
Simple :D

Thanks!
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: index.php?p=whatever

Post by markusn00b »

DJMiles wrote:I meant the file retrieving the includes was install.php :P

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");
And it works.
Simple :D

Thanks!
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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: index.php?p=whatever

Post by aceconcepts »

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.
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) :D
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: index.php?p=whatever

Post by Mordred »

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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: index.php?p=whatever

Post by aceconcepts »

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...
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.

However, I do like your idea and with time I think all programmers find new and more efficient ways of coding :D
Post Reply