Jump to content

Include page based on URL


whare

Recommended Posts

Hi all

 

not been playing with php over the past year and still a noob lol

 

i did have some code to include files based on the URL link

 

E.G:

http://www.yoursite.com/index.php?page=about

 

then i had some form of string that would access the URL

 

so it would have checked if the url was just index.php or had ?page=somepage after it

 

I can not seem to find any referance on the www or shift the headach i have got trying to remember the code

 

so thought some of you wondaful people would no what i am on about and point me in the rught direction of a tutorial for the information or have the code tohand so i can referance from it

 

 

thanx all

Link to comment
Share on other sites

Well, what you are doing can be a major security risk if done improperly.

 

Is yoursite.com your actual site that you are trying to do this on? If so the following would probably work:

 

$_GET['page'] = isset($_GET['page'])?$_GET['page']:'index'; // ternary operator acts as a short if / else
$validPages = array('about', 'home', 'members', 'index'); // etc etc

if (!in_array($_GET['page'], $validPages)) {
    $_GET['page'] = "index";
}

include('index.php');

 

Should be a secure way of doing it, if you are in-deed working on the site the URL points to.

Link to comment
Share on other sites

It is for my own site i want to use the index.php as a main template file and include the other page info into the main body are as requested

 

so on the main page i would have header menu footer and the include for the body

 

thankyou for the reply :)

 

 

EDIT:

 

How can i do it without the array ? i would like to just beable to place a file in the folder and URL link it without editing an array first

Link to comment
Share on other sites

Generate the array based on which files are available.

 

$page = 'pages/' . $_GET['page'] . '.php';
$pages = glob('pages/*.php');

if (!in_array($page, glob('pages/*.php'))) {
    die('invalid page'); // substitute with proper error handling
}

include $page;

 

Not tested, but should work.

Link to comment
Share on other sites

Thanx for the reply :)

 

After abit of playing i cam up with this myself after looking at premiso's post

 

<?php
if(isset($_GET['page'})) {
if(file_exists($_GET['page'].'.php')) {
include($_GET['page'].'.php');
} else {
include('main.php');
}}
?>

 

now that works sort of if i type in index.php?page=main it will show as it should if i type in index.php?page=nofile again it all works

 

However if i type in index.php i get no include it will not load the main.php

 

so i must be missing something just can not remember what lol

 

Thanx

Link to comment
Share on other sites

<?php
if(isset($_GET['page'})) {
if(file_exists($_GET['page'].'.php')) {
include($_GET['page'].'.php');
} else {
include('main.php');
}}
?>

 

Bad idea, just assume I crafted the URL:

 

index.php?page=../your/secret/config

 

you would get a 404 error hehe

 

but no i see what you are saying so have you an better code example that i would work with that has ease of use but still the function i require?

Link to comment
Share on other sites

<?php
if(isset($_GET['page'})) {
if(file_exists($_GET['page'].'.php')) {
include($_GET['page'].'.php');
} else {
include('main.php');
}}
?>

 

Bad idea, just assume I crafted the URL:

 

index.php?page=../your/secret/config

 

you would get a 404 error hehe

 

but no i see what you are saying so have you an better code example that i would work with that has ease of use but still the function i require?

 

Here you go: http://www.phpfreaks.com/forums/index.php/topic,300990.msg1424520.html#msg1424520

Link to comment
Share on other sites

Generate the array based on which files are available.

 

$page = 'pages/' . $_GET['page'] . '.php';
$pages = glob('pages/*.php');

if (!in_array($page, glob('pages/*.php'))) {
    die('invalid page'); // substitute with proper error handling
}

include $page;

 

Not tested, but should work.

 

Thanx for the reply however i did look over your code and see

if (!in_array($page, glob('pages/*.php'))) {

used for stopping code from running and see it as a problem however i will try it but sill see it as a pitfall

 

 

EDIT:

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/a6950394/public_html/index.php on line 150

 

used the code and that is the error going to have a bit of a play now

Link to comment
Share on other sites

god i forgot how many painkillers was needed for php

 

get the error in my abouve post

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/a6950394/public_html/index.php on line 150

 

and to be honist being a noob still lol i dont really inderstand the glob command so unsure how best to troble shoot this one

 

 

Link to comment
Share on other sites

  • 10 months later...

Drag up this old topic of mine :) its is related

 

right still want the same sort of format but looking to use it to pull data from the DB so new pages can be created/edit from site admin area something like what a forum would do but only edited and created by site staff to eliminate the need for FTP access to the server for editing

 

thanx

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.