logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Coding & Scripting Corner View previous topic :: View next topic
Reply to topic   Topic: Page IDs Page Previous  1, 2
Author
Steffen
Moderator


Joined: 15 Oct 2005
Posts: 3049
Location: Hilversum, NL, EU

PostPosted: Tue 14 Nov '06 20:33    Post subject: Reply with quote

All the lines have ; in front, exepct line 5 which says:

- display_errors = On [Security]

Which is not a correct line.

7th line:

; scripts will no longer be displayed as a part of the script output, and thus,
Back to top
sb.net



Joined: 22 Sep 2006
Posts: 120
Location: USA

PostPosted: Tue 14 Nov '06 21:31    Post subject: Reply with quote

Brian, I can do that. But I am still working on it.

I might do this:
Code:
<?php

$pages = array(
   'index'    => array(
      'dir'   => '/dir1/dir2/',
      'file'   => 'page.php',
   ),
   'index2'   => array(
      'dir'   => '/dir1/dir3/',
      'file'   => 'page.php',
   ),
   'index3'   => array(
      'dir'   => '/dir1/dir2/',
      'file'   => 'page2.php',
   ),
   'index4'   => array(
      'dir'   => '/dir1/dir4/',
      'file'   => 'page.php',
   ),
);

// assuming GET here, and set to 'index' if not provided
// this is basic error correction
$page = ( $_GET['page'] ) ? strtolower( $_GET['page'] ) : 'index';

// if it's not in the array, let's go to the default which
// is 'index'
if( !in_array( $page, $pages ) )
   $page = 'index';

// now include the correct page from the appropriate dir
include( $pages[$page]['dir'] . $pages[$page]['file'] );

?>


it would read it off an xml file.
Back to top
Brian



Joined: 21 Oct 2005
Posts: 209
Location: Puyallup, WA USA

PostPosted: Tue 14 Nov '06 22:04    Post subject: Reply with quote

So you want to read and parse an XML file to populate the Array(s). It can become complicated for sure, but this is a common way to manage large web sites. I would tend to start by hard coding the info for dev and testing purposes at first, populate the arrays from a config file, then introduce the XML parsing when you are okay with the hard coded approach first.

Just keep in mind, I am just one small fish in a big pond, not an expert by any stretch of the imagination. But I just like to offer my views based on my experience. Start with the basics, then grow from there.

As you build models that work, set them aside for use in future projects.

Also, Steffen pointed out that you have what technically appears to be a parse error, or what would result in a parse error.
Back to top
sb.net



Joined: 22 Sep 2006
Posts: 120
Location: USA

PostPosted: Tue 14 Nov '06 23:17    Post subject: Reply with quote

I tried the script you made, and it does not work. Sad

Code:
<html>
<body>
<?php

$pages = array(
   'index'    => array(
      'dir'   => '/includes/',
      'file'   => 'hi.php',
   ),
   'index2'   => array(
      'dir'   => '/new/',
      'file'   => 'hi2.php',
   ),
   'index3'   => array(
      'dir'   => '/dir1/dir2/',
      'file'   => 'page2.php',
   ),
   'index4'   => array(
      'dir'   => '/dir1/dir4/',
      'file'   => 'page.php',
   ),
);

// assuming GET here, and set to 'index' if not provided
// this is basic error correction
$page = ( $_GET['page'] ) ? strtolower( $_GET['page'] ) : 'index';

// if it's not in the array, let's go to the default which
// is 'index'
if( !in_array( $page, $pages ) )
   $page = 'index';

// now include the correct page from the appropriate dir
include( $pages[$page]['dir'] . $pages[$page]['file'] );

?>

Hi

</body>
</html>


http://data-base-web.com/tester/index.php?page=index2

it is not opening up and including the file.
Back to top
Brian



Joined: 21 Oct 2005
Posts: 209
Location: Puyallup, WA USA

PostPosted: Tue 14 Nov '06 23:56    Post subject: Reply with quote

It's opening up some file, here is the HTML output when tested today by me:

Code:
<html>
<body>

Hi

</body>
</html>


Was this not the output I should have seen for the given URL?

I should also note that in the test script I gave you, it's just an example that may be more complex than what you need. Also, it offers no error correction for the second element of the array, it was only to serve as an example.

I would also incorporate HTTP REFERER checking, User Agent checking, and a number of other standards (maybe session management). But it does look like you got at least part of it working.

EDIT: I see that you added this to your code, so no it does not appear to be working. Did you get error reporting / logging enabled?

I don't see any parse, notice, or warnings so I will assume no on that, but you could easily have error log reporting up and running.
Back to top
sb.net



Joined: 22 Sep 2006
Posts: 120
Location: USA

PostPosted: Wed 15 Nov '06 0:46    Post subject: Reply with quote

I got logging to work.

Code:
[14-Nov-2006 17:44:39] PHP Warning:  include(/includes/hi.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in C:\var\www\html\data-base-web\tester\index.php on line 34

[14-Nov-2006 17:44:39] PHP Warning:  include() [<a href='function.include'>function.include</a>]: Failed opening '/includes/hi.php' for inclusion (include_path='.;C:\php5\pear') in C:\var\www\html\data-base-web\tester\index.php on line 34


EDIT: It does not work at all. i gave this
Code:
   'index'    => array(
      'dir'   => 'C:/var/www/html/data-base-web/tester/includes/',
      'file'   => 'hi.php',
   ),
and it showed what it should show, but no matter what page i gave it it showed what was in "index".
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7288
Location: Germany, Next to Hamburg

PostPosted: Wed 15 Nov '06 11:03    Post subject: Reply with quote

Failed opening '/includes/hi.php'

This tries to include C:/includes/hi.php'

If you want to include set the dir 'includes/' instead of '/includes/' Wink
Back to top
Brian



Joined: 21 Oct 2005
Posts: 209
Location: Puyallup, WA USA

PostPosted: Wed 15 Nov '06 16:07    Post subject: My apologies Reply with quote

My apologies, I just whipped up code yesterday, did not fully test and just threw it out there as an example, though not the best example. Generally I would have actually tried it out, but sometimes just for the purposes of illustration, I suppose I just get in a hurry. That being said, here is a tested example of code that works:

Code:
<?php

// Let us show all warnings, notices, and errors
error_reporting( E_ALL );

$br     = '<br><br>'; // For testing purposes
// clear the error flag
$error  = false;
 
// grab the variables
$cat  = ( isset( $_GET['cat'] ) )  ? strtolower( $_GET['cat'] )  : false;
$page = ( isset( $_GET['page'] ) ) ? strtolower( $_GET['page'] ) : false;

/**
 * I am using this array as follows:
 *
 * $files['category']['path to include']
 * $files['category']['page']'name of include']
**/
 
// This array holds paths and included file names
$files = array(
   'animals' => array(
      'path'  => '/includes/animals/',
      'page'  => array(
         'dogs'         => 'dogs.inc.php',
         'cats'         => 'cats.inc.php',
         'birds'         => 'birds.inc.php',
      ),
   ),
   'foods' => array(
      'path'  => '/includes/foods/',
      'page'  => array(
         'fruit'         => 'fruit.inc.php',
         'vegetables'   => 'vegetables.inc.php',
         'bread'         => 'bread.inc.php',
         'meat'         => 'meat.inc.php',
      ),
   ),
   'places' => array(
      'path'  => '/includes/places/',
      'page'  => array(
         'london'      => 'london.inc.php',
         'newyork'      => 'nyc.inc.php',
         'tokyo'        => 'tokyo.inc.php',
         'sydney'      => 'sydney.inc.php',
      ),
   ),
);
 
// now I am just testing to see if these values are good or not
if( !array_key_exists( $cat, $files ) || $cat == false ) {
    echo $cat . ' is not a valid category!' . $br;
    $error = true;
} else
    echo $cat . ' is a valid category' . $br;
   
if( $error || !array_key_exists( $page, $files[$cat]['page'] ) || $page == false ) {
    echo $page . ' is not a valid page to view!' . $br;
    $error = true;
} else
    echo $page . ' is a valid page to view.' . $br;
 
// display the path if it's an acceptible file
if( !$error ) {
    echo 'The included file would be: ';
    echo $files[$cat]['path'] . $files[$cat]['page'][$page];
}

/*
  examples:
 
     http://someurl.com?cat=places&page=london  Results:  valid
    http://someurl.com?cat=place&page=london  Results: Not valid
    http://someurl.com?cat=foods&page=meat  Results:  valid
*/
 
?>


In this example I have error detection for the lack of the necessary Array Keys since that is how I structured this mult-dimensional array.

So you can try this on my own little testing server at:

http://brian.webbywarehouse.com/temp/al/testscripts/array_test1.php?cat=foods&page=meat
Results: Valid

http://brian.webbywarehouse.com/temp/al/testscripts/array_test1.php?cat=food&page=meats
Results: Not valid

http://brian.webbywarehouse.com/temp/al/testscripts/array_test1.php?cat=fake_info&page=
Results: Not valid

So you can see that each array is set up as follows:

array[category][path] => path to include
array[category][page][file] => file to include from the path
Back to top
sb.net



Joined: 22 Sep 2006
Posts: 120
Location: USA

PostPosted: Wed 15 Nov '06 16:55    Post subject: Reply with quote

Wow Thank you. Very Happy I am making a software site so I wanted the categories. Again thank you.

The way that you organized that will make my job a whole lot easier.

BTW James, Thank you for working with me in the beginning.

EDIT: what would be the include code to use?
Back to top
Brian



Joined: 21 Oct 2005
Posts: 209
Location: Puyallup, WA USA

PostPosted: Wed 15 Nov '06 19:41    Post subject: Reply with quote

In my code I placed this for evaluating at testing:
Code:
// display the path if it's an acceptible file
if( !$error ) {
    echo 'The included file would be: ';
    echo $files[$cat]['path'] . $files[$cat]['page'][$page];
}


Instead of echoing this value, this would be the include, as in:
Code:
// load the appropriate include file
if( !$error ) {
   include( $files[$cat]['path'] . $files[$cat]['page'][$page] );
}


This is assuming the paths are complete, correct, and the entire nine yards.

But more importantly, you have an opportunity here to provide redirection if they have either manipulated the data in the submitted query string, or if they have somehow com across a link that was not properly written.

Another componant you could add would be to check that the desired include file and its path actually exist.

In my view it would be so much easier to manage this via a db table. That being said, if you used XML and an adminstrative backend to generate and update static HTML pages, you would have an even faster and more secure site.

Me? I am lazy, I prefer to place the burden on the PHP engine for data processing, and on MySQL for data storage and retreival, I just don't want to mess with creating a bunch of static HTML pages, even though they would use far less memory, require virtually no CPU load, and are immune to MySQL and PHP bugs on the front end side of things.

Hope all this helps.
Back to top
sb.net



Joined: 22 Sep 2006
Posts: 120
Location: USA

PostPosted: Wed 15 Nov '06 22:28    Post subject: Reply with quote

Thanks, that was what i did do but it would not work unless on the path you did not put a / before every path.
Back to top
Brian



Joined: 21 Oct 2005
Posts: 209
Location: Puyallup, WA USA

PostPosted: Wed 15 Nov '06 23:59    Post subject: Reply with quote

I am glad it is working for you
Back to top


Reply to topic   Topic: Page IDs View previous topic :: View next topic
Post new topic   Forum Index -> Coding & Scripting Corner Page Previous  1, 2