I think there will be a simple solution to my problem but i'm new to PHP and i cant work it out.

basically my site only has 1 page index.php... and i have a small script that includes a page i set.

for example, if you go to ?p=contact, it will include the contact.php file into index.php

on a certain page i have a script that is initialized in the body tag, but i only want it to appear in the body tag when i go to that page so i created this script:

<?php
if ( $p == portfolio ) {
	echo "<body onload=\"javascript:startmenu()\">";
} else {
	echo "<body>";
}
?>

but the $p variable is defined after this further down the code, so its not reading it, well i think thats the problem.

the code i am using is this:

<?php

if (isset($_GET['p']) && $_GET['p'] != "") {

$p = $_GET['p'];

if (file_exists($p.'.php')) {

@include ($p.'.php');

} elseif (!file_exists($p.'.php')) {

@include ('404.php');

}

} else {

@include ('home.php');

}

?>

any ideas?

    You could do something like this:

    <?php
    
    $p = 'home';
    if (isset($_GET['p']) && $_GET['p'] != "") {
        $p = $_GET['p'];
    }
    
    // insert whatever code that needs $p before the include
    // ...
    
    if (file_exists($p.'.php')) {
        @include $p . '.php';
    } else {
        @include '404.php';
    }
    
    ?>

    Though I think that it usually makes sense to know whether $p.'.php' is valid before using $p. Also, it would be good to check if $p is valid based on a list of valid values so as to avoid the user trying any tricks with your filesystem.

      (Darn that Laserlight, he must have entered his reply just as I started typing mine. :p )

      Start of script:

      // get $p and add a little security, too:
      $p = '';
      if(!empty($_GET['p']))
      {
         $p = basename($_GET['p']); // prevents values like "../includes/db_config"
      }
      

      Then you can do the piece of code that populates the body tag, and later on:

      <?php
      if (!empty($p)) {
         if (file_exists($p.'.php')) {
            @include ($p.'.php');
         } elseif (!file_exists($p.'.php')) {
            @include ('404.php');
         }
      } else {
         @include ('home.php');
      }
      ?>
      
        Write a Reply...