Page 1 of 1

index.php?p=whatever

Posted: Sun Apr 20, 2008 2:05 pm
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...

Re: index.php?p=whatever

Posted: Sun Apr 20, 2008 2:41 pm
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");
 

Re: index.php?p=whatever

Posted: Sun Apr 20, 2008 2:46 pm
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!

Re: index.php?p=whatever

Posted: Sun Apr 20, 2008 3:15 pm
by aceconcepts
hahaha

mine was rather crude though :D

Re: index.php?p=whatever

Posted: Sun Apr 20, 2008 6:40 pm
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"

Re: index.php?p=whatever

Posted: Sun Apr 20, 2008 7:10 pm
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");
 

Re: index.php?p=whatever

Posted: Sun Apr 20, 2008 7:31 pm
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!

Re: index.php?p=whatever

Posted: Mon Apr 21, 2008 4:36 am
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.

Re: index.php?p=whatever

Posted: Mon Apr 21, 2008 4:46 am
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

Re: index.php?p=whatever

Posted: Mon Apr 21, 2008 7:46 am
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.

Re: index.php?p=whatever

Posted: Mon Apr 21, 2008 7:52 am
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