Urban75 Home About Offline BrixtonBuzz Contact

PHP and URLS

xenon

Culturally incoherent
Hello.
I'm trying to learn a bit of php and am using php includes on my site.

What I want to do is have a variable called page that will be pulled into the body section of the page. There will also be a php menu file included, that will reasign pages of content to the page variable when the links are activated.

I understand you can set a variable in the URL string. SO in the case where the first page of content that I want to be shown on my site is called. home.php. I'd have an url like
www.mysite.com/index.php?page=home

How will a visitor get to that URL to set the page variable, in the first place? Do I just create the index.php file then have that point at another page, where the variables and template for the site are set up. SO the index.php file would just be something like.

<?
header("Location:http://www.mysite.com/template.php?page=home");
?>

And then aall the includes, menu etc, are set up in template.php.

Sorry for longwindedness. Have googled but am not finding anything directly rellavent.
 
You could put this at the top of your index page. If the variable page isn't set to anything, it'll be sent to home and will reload the page.

Code:
if ((isset($_GET['page'])) == false) || ($_GET['page'] == "")) {
header("Location: http://www.mysite.com/index.php?page=home");
}
 
Or you could default to home if the page variable isn't set in the querystring:
Code:
$page = (isset($_GET['page'])) ? $_GET['page'] : 'home';

include $page . ".php";
 
Why not have it all in the index.php page which is where browsers will default to, and use BB's solution, which strikes me as the neatest.
Make sure you check whatever's passed in the $_GET param is a valid value as someone's bound to try and break your site eventually.
 
Thanks will try this later. Not sure what you mean by BB solution gnoriac.

Further reading on this alerts me to potential security problems with variables calling pages. i.e. users editing the URL string to make the page variable point to a remote php script. Will look at all this later. Damn floo thing's made my brain go mushy today.
 
You probably want to have all the valid values for $_GET['page'] in an array and check the value supplied by the user against that array before you go and do anything with it.

Use in_array() to do the checking.
 
Another solution would be to use a switch statement to set the page.

Code:
switch($_GET['page']) {
case "about":
$page = "about.php";
break;
case "links":
$page = "links.php";
break;
case "etc":
$page = "etc.php";
break;
default:
$page = "home.php";
}

This would take care of people trying to break your site, any page that's not in that list will be set to home. You could also set page to be all lower case so something like index.php?page=HoME would be set to home.

Code:
switch(strtolower($_GET['page'])) {
 
God gave us arrays so that we don't have to do multi-line switch statements.

Code:
$valid_pages = array('home', 'contact', 'about');

if (in_array($_GET['page'], $valid_pages)) {
include './' . $_GET['page'] . '.php';
} else {
echo "Ain't no-one here but us chickens.";
}
 
Spent ages wondering why it wasn't working. Then realised I'd mistakenly been using "\" in the paths, thinking as this is a windows machine I'm using. After much needless messing around with the code eventually spotted this.
:rolleyes:

At last, it works. Chose the array example. Thanks for the help.:)
 
Back
Top Bottom