Current location: Hot Scripts Forums » Programming Languages » PHP » help with links to sub folders


help with links to sub folders

Reply
  #1 (permalink)  
Old 04-07-05, 11:51 AM
fraggle fraggle is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
help with links to sub folders

Hi
This is probably really basic but I need help please.
I have links on a site e.g pagename.php?page=newpage.php and this works fine if newpage.php is in the root folder. but newpage is in folder/newpage.php

I got the link to work with pagename.php?page=folder/newpage.php but i am sure there must be a neater way........

any help please?
__________________
~Fraggle is Frazzled~
Reply With Quote
  #2 (permalink)  
Old 04-07-05, 12:03 PM
ben.periton ben.periton is offline
Wannabe Coder
 
Join Date: Oct 2004
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
If all your pages are in the same seperate folder, then on your include statement just use
include('folder/'.$_GET['page'].'.php');
__________________
Ben Periton
http://ben.periton.co.uk
Reply With Quote
  #3 (permalink)  
Old 04-07-05, 12:22 PM
fraggle fraggle is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
folders

I will have at least 6 folders initially????
__________________
~Fraggle is Frazzled~
Reply With Quote
  #4 (permalink)  
Old 04-07-05, 02:04 PM
perleo perleo is offline
Coding Addict
 
Join Date: Jul 2003
Location: Ireland
Posts: 269
Thanks: 0
Thanked 0 Times in 0 Posts
well you could do it this way, set your URL to be like script.php?page=page&folder=folder

so you PHP would be

PHP Code:

<?php

$page 
$_GET['page'];
$folder $_GET['folder'];

include(
$folder.'/'.$page.'.php');

?>
or if you know what the folders are, you could use switch() , so you url would be script.php?page=page

PHP Code:

<?php

 $page 
$_GET['page'];

 switch(
$page)
   {
           case 
'page';
             include(
'folder/page.php');
           break;
   
            
/* repeat the above and change the case and folder path */
    
}
 
?>
Reply With Quote
  #5 (permalink)  
Old 04-07-05, 05:55 PM
gevorgkhc gevorgkhc is offline
Newbie Coder
 
Join Date: Feb 2004
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
not very efficent but w/e
PHP Code:

<?php

if(!$_GET['folder']) {
$folder ".";
} else {
$folder "$_GET['folder']";
}

if(!
$_GET['page']) {
$page "index.php";
} else {
$page "$_GET['page'].php";
}
if(include(
"$folder/$page")) {
echo 
"<H1>Error, page does not exist!</H1>"
}
?>
Reply With Quote
  #6 (permalink)  
Old 04-07-05, 06:23 PM
moronovich moronovich is offline
Junior Code Guru
 
Join Date: Oct 2004
Posts: 460
Thanks: 0
Thanked 0 Times in 0 Posts
some people may try:
Code:
//assumption: magic_quotes_gpc is off
browser: http://www.yoursite.com/pagename.php...=\etc\passwd\0
the reason: in C, a string (char *) is terminated by a null character
__________________
just an ignorant noob with moronic solution...
Reply With Quote
  #7 (permalink)  
Old 04-07-05, 06:37 PM
Acecool's Avatar
Acecool Acecool is offline
Aspiring Coder
 
Join Date: Nov 2003
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
All of those examples are insecure.

PHP Code:

if (isset($_GET['page']))

{
     switch (
$_GET['page'])
     {
          case 
'123':
               include(
'123.php');
          break;

          case 
'456':
               include(
'456.php');
          break;

          default:
               include(
'default.php');
          break;
     }
}
else
{
     include(
'default.php');


Another way would be:

PHP Code:

// Make the allowed pages array...

$pages = array();
$pages[] = 'default';
$pages[] = '123';
$pages[] = '456';

// Set a simple page variable, change 'default' to the default page
$page = isset($_GET['page']) ? $_GET['page']: 'default';

// If the page is in the allowed pages array, do it to it..
if (in_array($page$pages))
{
     include (
$page);

__________________
Check Acecoolco.com for PHP Tutorials, and other tuts
If you plan on contacting me, please read this: Legal Terms & Conditions
Reply With Quote
  #8 (permalink)  
Old 04-08-05, 05:15 AM
fraggle fraggle is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks

so with...

// Make the allowed pages array...
$pages = array();
$pages[] = 'default';
$pages[] = '123';
$pages[] = '456';

if i had over 100 pages, i would need to list them all here?
__________________
~Fraggle is Frazzled~
Reply With Quote
  #9 (permalink)  
Old 04-08-05, 09:58 AM
Acecool's Avatar
Acecool Acecool is offline
Aspiring Coder
 
Join Date: Nov 2003
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
Yep..

You could also use:
$pages = array('default', '123', '456');


but the other way is easier to read / manage, as you can comment them:

// Default page...
$pages[] = 'default'

// Some page...
$pages[] = '123';


You could include($_GET['page']); - however this allows anyone to execute unfriendly scripts on your server, so its best to verify it.
__________________
Check Acecoolco.com for PHP Tutorials, and other tuts
If you plan on contacting me, please read this: Legal Terms & Conditions
Reply With Quote
  #10 (permalink)  
Old 04-11-05, 12:56 PM
Pixelpyro Pixelpyro is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
hi Guys,

Just started looking into PHP as a way to better structure my sites using includes etc, so very much a newbie. I came across this thread as the topic similar to something I am looking to do. I have been working with the switch technique which works fine but one of the issues I had was using it with a larger nuber of pages. That is why Acecool's solution of using an array caught my attention.

I have tried to implement the code but get the following errors.

Warning: main(default): failed to open stream: No such file or directory in /hsphere/local/home/copious/pixelpyro.co.uk/structure/array.php on line 22

Warning: main(default): failed to open stream: No such file or directory in /hsphere/local/home/copious/pixelpyro.co.uk/structure/array.php on line 22

Warning: main(): Failed opening 'default' for inclusion (include_path='.:/hsphere/shared/apache/php/lib/php') in /hsphere/local/home/copious/pixelpyro.co.uk/structure/array.php on line 22

I have created the relevant files and the array.php is the main file with the page array code within it. Any ideas what I am doing wrong? Could it be how I am calling the page via a link (http://www.pixelpyro.co.uk/structure...p?page=default)

I should mention that all of the files 123.php, 456.php and default.php are all there.

Thanks for any help..

Last edited by Pixelpyro; 04-11-05 at 01:09 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Rotating Text Links & Images daily... J-Deak Script Requests 5 02-13-05 05:06 PM
links exchange with scripts, hosting, SEO, design, and other webmasers related sites cms-master.com Traffic Exchange 1 01-24-05 05:08 PM
Link Exchange: Design, Promotion, Domain, Hosting, Webmaster links wanted vano Traffic Exchange 0 11-12-04 08:00 AM
How can I open links in original window? GS300 JavaScript 3 05-30-04 08:37 AM
Problem with java links Kingfishsj JavaScript 0 08-01-03 08:09 AM


All times are GMT -5. The time now is 05:40 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.